使用图片懒加载

This commit is contained in:
ctw committed 2025-01-16 09:54:43 +08:00
1 parent b6f313ed78
commit 628fa5bfdc
2 files changed
+61 -49

No files matched your search

+52 -46
View File
@@ -1,72 +1,78 @@
<!-- 图片预览 --> <!-- 图片预览 -->
<script lang="ts" setup name="PreviewImage"> <script lang="ts" setup name="PreviewImage">
type Props = { type Props = {
src?: string src?: string
width?: string | number width?: string | number
height?: string | number height?: string | number
} }
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
width: 50, width: 50,
height: 50, height: 50,
}) })
const realSrc = computed(() => { const realSrc = computed(() => {
if (!props.src) return ""; if (!props.src) return ""
let real_src = props.src.split(",")[0]; let real_src = props.src.split(",")[0]
return dispatchUrl(real_src) ?? ""; return dispatchUrl(real_src) ?? ""
}) })
const realSrcList = computed(() => { const realSrcList = computed(() => {
if (!props.src) return ""; if (!props.src) return ""
let real_src = props.src.split(",")?.map(v => dispatchUrl(v)); let real_src = props.src.split(",")?.map((v) => dispatchUrl(v))
return real_src ?? []; return real_src ?? []
}) })
const realWidth = computed(() => typeof props.width == "string" ? props.width : `${props.width}px`) const realWidth = computed(() => (typeof props.width == "string" ? props.width : `${props.width}px`))
const realHeight = computed(() => typeof props.height == "string" ? props.height : `${props.height}px`) const realHeight = computed(() => (typeof props.height == "string" ? props.height : `${props.height}px`))
function dispatchUrl(url?: string) { function dispatchUrl(url?: string) {
if (!url) return "" if (!url) return ""
return url.includes('http') ? url : import.meta.env.VITE_IMAGE_BASE_PATH + url return url.includes("http") ? url : import.meta.env.VITE_IMAGE_BASE_PATH + url
} }
</script> </script>
<template> <template>
<el-image v-if="props.src" hide-on-click-modal preview-teleported :src="`${realSrc}`" fit="cover" <el-image
:style="`width:${realWidth};height:${realHeight};`" :preview-src-list="realSrcList"> v-if="props.src"
<template #error> hide-on-click-modal
<div class="image-slot"> preview-teleported
<i class="el-icon-picture-outline"></i> :src="`${realSrc}`"
</div> fit="cover"
</template> :style="`width:${realWidth};height:${realHeight};`"
</el-image> :preview-src-list="realSrcList"
<div v-else :style="`width:${realWidth};height:${realHeight};`" /> loading="lazy"
>
<template #error>
<div class="image-slot">
<i class="el-icon-picture-outline"></i>
</div>
</template>
</el-image>
<div v-else :style="`width:${realWidth};height:${realHeight};`" />
</template> </template>
<style lang="scss" scoped> <style lang="scss" scoped>
.el-image { .el-image {
border-radius: 5px; border-radius: 5px;
background-color: #ebeef5; background-color: #ebeef5;
box-shadow: 0 0 5px 1px #ccc; box-shadow: 0 0 5px 1px #ccc;
:deep(.el-image__inner) { :deep(.el-image__inner) {
transition: all 0.3s; transition: all 0.3s;
cursor: pointer; cursor: pointer;
&:hover { &:hover {
transform: scale(1.2); transform: scale(1.2);
} }
} }
:deep(.image-slot) { :deep(.image-slot) {
display: flex; display: flex;
justify-content: center; justify-content: center;
align-items: center; align-items: center;
width: 100%; width: 100%;
height: 100%; height: 100%;
color: #909399; color: #909399;
font-size: 30px; font-size: 30px;
} }
} }
</style> </style>
+9 -3
View File
@@ -12,7 +12,7 @@
v-for="(item, index) in weekData.data" v-for="(item, index) in weekData.data"
:key="index" :key="index"
@click="timeClick(item, index)" @click="timeClick(item, index)"
:class="{ selectedSpan: isActive === index ? 'selected' : '' }" :class="{ selectedSpan: isActive === index }"
class="border-2 border-solid border-gray-200 text-center" class="border-2 border-solid border-gray-200 text-center"
> >
<span>{{ item }}</span> <span>{{ item }}</span>
@@ -55,7 +55,12 @@
<td v-if="!loading" class="text-center" @click="addFood(3)">晚餐</td> <td v-if="!loading" class="text-center" @click="addFood(3)">晚餐</td>
<div class="food_list"> <div class="food_list">
<td class="food_list_item" v-for="(item, index) in foodData3" :key="index" @click="delteData(item.tid)"> <td class="food_list_item" v-for="(item, index) in foodData3" :key="index" @click="delteData(item.tid)">
<el-image style="width: 80px; height: 80px" :src="imageBaseUrl + item.picUrl" fit="cover" /> <el-image
style="width: 80px; height: 80px"
loading="lazy"
:src="imageBaseUrl + item.picUrl"
fit="cover"
/>
<div class="w-full flex justify-around items-center text-black mt-2"> <div class="w-full flex justify-around items-center text-black mt-2">
<span>{{ item.name }}</span> <span>{{ item.name }}</span>
<span class="mx-1">|</span> <span class="mx-1">|</span>
@@ -114,7 +119,7 @@ const addFood = async (hourType: number) => {
//选中具体时间数据 //选中具体时间数据
const timeClick = async (time: string, index: number) => { const timeClick = async (time: string, index: number) => {
//数据查询操作 //数据查询操作
if (time) { if (time && isActive.value !== index) {
//时间处理 //时间处理
time = time.substring(0, time.indexOf("(")) time = time.substring(0, time.indexOf("("))
timedata = time timedata = time
@@ -265,6 +270,7 @@ onMounted(async () => {
border-left: 2px solid transparent; border-left: 2px solid transparent;
color: rgb(255, 187, 0); color: rgb(255, 187, 0);
position: relative; position: relative;
cursor: default;
} }
.selectedSpan::before { .selectedSpan::before {
content: ""; content: "";