1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <template>
- <view class="hc-img-view"
- :style="{
- 'background-image': srcs,
- 'width': widths,
- 'height': heights,
- 'aspect-ratio': aspectRatio
- }"></view>
- </template>
- <script setup>
- import {onMounted, ref, watch} from "vue";
- const props = defineProps({
- 'src': String,
- 'width': [String, Number],
- 'height': [String, Number]
- });
- //初始变量
- const srcs = ref('');
- const widths = ref('');
- const heights = ref('');
- const aspectRatio = ref('');
- //加载完成
- onMounted(() => {
- setStyleData(props.src, props.width, props.height);
- });
- //监听变化
- watch(() => [
- props.src,
- props.width,
- props.height
- ], ([src, width, height]) => {
- setStyleData(src, width, height);
- })
- //设置样式
- const setStyleData = (src, width, height) => {
- srcs.value = 'url(' + src + ')';
- widths.value = (width * 2) + 'rpx';
- heights.value = (height * 2) + 'rpx';
- aspectRatio.value = 'auto ' + width + ' / ' + height;
- }
- </script>
- <style lang="scss" scoped>
- .hc-img-view {
- background-position: 0% 0%;
- background-size: 100% 100%;
- display: inline-block;
- }
- </style>
|