style(背景图组件): 适配重复背景的使用

This commit is contained in:
hzj 2026-01-05 18:29:31 +08:00
parent f2c89996ac
commit f5c979bfa1

View File

@ -1,13 +1,13 @@
<template>
<div class="bg-layer" :style="layerStyle">
<div class="bg-layer" :style="combinedLayerStyle">
<img
v-for="(imgSrc, index) in backgroundImages"
v-for="(imgSrc, index) in filteredBackgroundImages"
:key="index"
v-smart-img
:src="imgSrc"
:alt="`background-${index}`"
:class="['bg-image', imageClass]"
:style="imageStyle"
:style="combinedImageStyle"
/>
</div>
</template>
@ -40,6 +40,55 @@ const props = defineProps({
type: String,
default: '',
},
//
filterEmpty: {
type: Boolean,
default: true,
},
// 使CSS
useCssBackground: {
type: Boolean,
default: false,
},
// CSS
cssBackgroundProps: {
type: Object,
default: () => ({}),
},
})
//
const filteredBackgroundImages = computed(() => {
if (props.filterEmpty) {
return props.backgroundImages.filter((img) => img && img.trim() !== '')
}
return props.backgroundImages
})
//
const combinedLayerStyle = computed(() => {
if (props.useCssBackground && filteredBackgroundImages.value.length > 0) {
return {
...props.layerStyle,
backgroundImage: `url(${filteredBackgroundImages.value[0]})`,
backgroundRepeat: props.cssBackgroundProps.repeat,
backgroundPosition: props.cssBackgroundProps.position,
backgroundSize: props.cssBackgroundProps.size,
}
}
return {
...props.layerStyle,
}
})
const combinedImageStyle = computed(() => {
return props.useCssBackground
? { display: 'none' } // 使CSSimg
: props.imageStyle
})
</script>