|
| 1 | +<template> |
| 2 | + <div class="v-lazy-demo"> |
| 3 | + <h1>v-lazy 图片懒加载指令演示</h1> |
| 4 | + |
| 5 | + <div class="description"> |
| 6 | + <p>v-lazy 指令用于图片的懒加载,只有当图片进入视口或即将进入视口时才会加载图片。</p> |
| 7 | + <p>滚动页面查看效果,图片将在进入视口时才开始加载。</p> |
| 8 | + </div> |
| 9 | + |
| 10 | + <div v-for="(image, index) in images" :key="index" class="image-container"> |
| 11 | + <div class="image-card"> |
| 12 | + <div class="image-wrapper"> |
| 13 | + <img |
| 14 | + v-lazy="image.url" |
| 15 | + :alt="'示例图片 ' + (index + 1)" |
| 16 | + class="lazy-image" |
| 17 | + data-loaded="false" |
| 18 | + @load="onImageLoad" |
| 19 | + /> |
| 20 | + <div class="loading-placeholder">加载中...</div> |
| 21 | + </div> |
| 22 | + <div class="image-info"> |
| 23 | + <h3>图片 {{ index + 1 }}</h3> |
| 24 | + <p>{{ image.description }}</p> |
| 25 | + </div> |
| 26 | + </div> |
| 27 | + </div> |
| 28 | + </div> |
| 29 | +</template> |
| 30 | + |
| 31 | +<script setup lang="ts"> |
| 32 | +import { ref } from 'vue'; |
| 33 | +import { vLazy } from '@ssuperlilei/directives'; |
| 34 | +
|
| 35 | +// 图片数组,包含真实的图片URL |
| 36 | +const images = ref([ |
| 37 | + { |
| 38 | + url: 'https://picsum.photos/id/1/800/400', |
| 39 | + description: '这是一张使用 v-lazy 指令懒加载的图片。它只会在滚动到视图中时才会加载。', |
| 40 | + }, |
| 41 | + { |
| 42 | + url: 'https://picsum.photos/id/10/800/400', |
| 43 | + description: '懒加载可以减少初始页面加载时间,提高用户体验。', |
| 44 | + }, |
| 45 | + { |
| 46 | + url: 'https://picsum.photos/id/100/800/400', |
| 47 | + description: 'v-lazy 指令使用 IntersectionObserver API 来检测元素可见性。', |
| 48 | + }, |
| 49 | + { |
| 50 | + url: 'https://picsum.photos/id/1000/800/400', |
| 51 | + description: '当图片元素至少有 10% 可见或在视口 100px 范围内时,图片将开始加载。', |
| 52 | + }, |
| 53 | + { |
| 54 | + url: 'https://picsum.photos/id/1001/800/400', |
| 55 | + description: '图片加载后,IntersectionObserver 将停止对该元素的观察。', |
| 56 | + }, |
| 57 | + { |
| 58 | + url: 'https://picsum.photos/id/1002/800/400', |
| 59 | + description: '这个指令非常适合长页面和图片密集型应用程序。', |
| 60 | + }, |
| 61 | + { |
| 62 | + url: 'https://picsum.photos/id/1003/800/400', |
| 63 | + description: '通过减少不必要的图片加载,可以节省带宽并提高页面性能。', |
| 64 | + }, |
| 65 | + { |
| 66 | + url: 'https://picsum.photos/id/1004/800/400', |
| 67 | + description: '如果你已经滚动到了这里,前面的所有图片已经被懒加载了。', |
| 68 | + }, |
| 69 | +]); |
| 70 | +
|
| 71 | +// 图片加载完成时的处理函数 |
| 72 | +const onImageLoad = (event: Event) => { |
| 73 | + const img = event.target as HTMLImageElement; |
| 74 | + img.dataset.loaded = 'true'; |
| 75 | +}; |
| 76 | +</script> |
| 77 | + |
| 78 | +<style scoped> |
| 79 | +.v-lazy-demo { |
| 80 | + max-width: 800px; |
| 81 | + padding: 20px; |
| 82 | + margin: 0 auto; |
| 83 | +} |
| 84 | +
|
| 85 | +h1 { |
| 86 | + margin-bottom: 30px; |
| 87 | + text-align: center; |
| 88 | +} |
| 89 | +
|
| 90 | +.description { |
| 91 | + padding: 15px; |
| 92 | + margin-bottom: 30px; |
| 93 | + background-color: #f5f5f5; |
| 94 | + border-radius: 8px; |
| 95 | +} |
| 96 | +
|
| 97 | +.image-container { |
| 98 | + margin-bottom: 50px; |
| 99 | +} |
| 100 | +
|
| 101 | +.image-card { |
| 102 | + overflow: hidden; |
| 103 | + border-radius: 8px; |
| 104 | + box-shadow: 0 4px 12px rgb(0 0 0 / 10%); |
| 105 | +} |
| 106 | +
|
| 107 | +.image-wrapper { |
| 108 | + position: relative; |
| 109 | + display: flex; |
| 110 | + align-items: center; |
| 111 | + justify-content: center; |
| 112 | + aspect-ratio: 2/1; |
| 113 | + overflow: hidden; |
| 114 | + background-color: #f0f0f0; |
| 115 | +} |
| 116 | +
|
| 117 | +.lazy-image { |
| 118 | + position: absolute; |
| 119 | + top: 0; |
| 120 | + left: 0; |
| 121 | + width: 100%; |
| 122 | + height: 100%; |
| 123 | + object-fit: cover; |
| 124 | + opacity: 0; |
| 125 | + transition: opacity 0.3s ease; |
| 126 | +} |
| 127 | +
|
| 128 | +.lazy-image[data-loaded='true'] { |
| 129 | + opacity: 1; |
| 130 | +} |
| 131 | +
|
| 132 | +.loading-placeholder { |
| 133 | + display: flex; |
| 134 | + align-items: center; |
| 135 | + justify-content: center; |
| 136 | + width: 100%; |
| 137 | + height: 100%; |
| 138 | + font-size: 18px; |
| 139 | + color: #666; |
| 140 | +} |
| 141 | +
|
| 142 | +.image-info { |
| 143 | + padding: 15px; |
| 144 | +} |
| 145 | +
|
| 146 | +h3 { |
| 147 | + margin-top: 0; |
| 148 | + margin-bottom: 10px; |
| 149 | +} |
| 150 | +</style> |
0 commit comments