<template>
<div class="text-container">
<p class="text">这是一个长文本内容,可能会超出容器宽度,需要进行省略号显示</p>
</div>
</template>
<style>
.text-container {
width: 200px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.text {
margin: 0;
font-size: 14px;
line-height: 1.5;
}
</style>Vue实现图片懒加载
<template>
<div class="image-container">
<img v-bind:src="placeholder" v-lazy="imageUrl" alt="Lazy Loading Image" />
</div>
</template>
<script>
export default {
data() {
return {
placeholder: 'placeholder.png',
imageUrl: 'actual_image.jpg',
};
},
directives: {
lazy: {
inserted: function (el, binding) {
let options = {
threshold: 0.5,
rootMargin: '0px 0px 100px 0px',
};
let lazyImage = new IntersectionObserver(function (entries) {
let image = entries[0];
if (image.isIntersecting) {
el.src = binding.value;
lazyImage.unobserve(el);
}
}, options);
lazyImage.observe(el);
},
},
},
};
</script>Vue实现无限滚动加载数据
<template>
<div class="data-container">
<ul>
<li v-for="(item, index) in items" :key="index">{{ item }}</li>
</ul>
<div v-if="isLoading" class="loading-message">加载中,请稍候...</div>
<div v-else-if="isEnd" class="end-message">已加载全部数据</div>
</div>
</template>
<script>
export default {
data() {
return {
items: [],
page: 1,
isLoading: false,
isEnd: false,
};
},
created() {
this.loadData();
window.addEventListener('scroll', this.handleScroll);
},
destroyed() {
window.removeEventListener('scroll', this.handleScroll);
},
methods: {
loadData() {
// 模拟异步加载数据
this.isLoading = true;
setTimeout(() => {
for (let i = 0; i< 10; i++) {
this.items.push(`Item ${this.items.length + 1}`);
}
this.page++;
this.isLoading = false;
if (this.page >= 5) {
this.isEnd = true;
}
}, 1500);
},
handleScroll() {
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
let windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
if (scrollTop + windowHeight >= scrollHeight - 100 && !this.isLoading && !this.isEnd) {
this.loadData();
}
},
},
};
</script>