Snavigation/src/components/Cover.vue
2023-07-27 17:33:15 +08:00

112 lines
2.5 KiB
Vue

<template>
<div :class="status.siteStatus !== 'normal' ? 'cover focus' : 'cover'">
<img
v-show="status.imgLoadStatus"
class="background"
alt="background"
:src="bgUrl"
@load="imgLoadComplete"
@error.once="imgLoadError"
/>
<Transition name="fade">
<div v-if="set.showBackgroundGray" class="gray" />
</Transition>
</div>
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount } from "vue";
import { statusStore, setStore } from "@/stores";
const set = setStore();
const status = statusStore();
const bgUrl = ref(null);
const imgTimeout = ref(null);
// 壁纸随机数
// 请依据文件夹内的图片个数修改 Math.random() 后面的第一个数字
const bgRandom = Math.floor(Math.random() * 3 + 1);
// 赋值壁纸
const setBgUrl = () => {
const { backgroundType } = set;
switch (backgroundType) {
case 0:
bgUrl.value = `/background/bg${bgRandom}.jpg`;
break;
case 1:
const isMobile = window.innerWidth < 768;
bgUrl.value = `https://api.dujin.org/bing/${isMobile ? "m" : "1920"}.php`;
break;
case 2:
bgUrl.value = "https://api.aixiaowai.cn/gqapi/gqapi.php";
break;
case 3:
bgUrl.value = "https://api.aixiaowai.cn/api/api.php";
break;
default:
break;
}
};
// 图片加载完成
const imgLoadComplete = () => {
imgTimeout.value = setTimeout(() => {
status.setImgLoadStatus(true);
console.log("壁纸加载完成");
}, 500);
};
// 图片显示失败
const imgLoadError = () => {
console.error("图片加载失败:", bgUrl.value);
bgUrl.value = `/background/bg${bgRandom}.jpg`;
};
onMounted(() => {
setBgUrl();
});
onBeforeUnmount(() => {
clearTimeout(imgTimeout.value);
});
</script>
<style lang="scss" scoped>
.cover {
width: 100%;
height: 100%;
position: relative;
background-color: var(--body-background-color);
&.focus {
.background {
filter: blur(10px) brightness(0.8);
transform: scale(1.1);
}
}
.background {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
object-fit: cover;
backface-visibility: hidden;
transition: filter 0.3s, transform 0.3s;
animation: fade-blur-in 1s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.gray {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-image: radial-gradient(
rgba(0, 0, 0, 0) 0,
rgba(0, 0, 0, 0.5) 100%
),
radial-gradient(rgba(0, 0, 0, 0) 33%, rgba(0, 0, 0, 0.3) 166%);
}
}
</style>