要实现图片的延时加载,可以使用以下方法:
<img src="placeholder.jpg" data-src="image.jpg" class="lazyload" />
<script>
window.onload = function() {
var lazyloadImages = document.querySelectorAll('.lazyload');
lazyloadImages.forEach(function(img) {
if (img.getBoundingClientRect().top <= window.innerHeight && img.getBoundingClientRect().bottom >= 0) {
img.src = img.getAttribute('data-src');
img.classList.remove('lazyload');
}
});
};
</script>
<div class="lazyload" style="background-image: url('placeholder.jpg');"></div>
<script>
window.onload = function() {
var lazyloadImages = document.querySelectorAll('.lazyload');
lazyloadImages.forEach(function(img) {
if (img.getBoundingClientRect().top <= window.innerHeight && img.getBoundingClientRect().bottom >= 0) {
img.style.backgroundImage = 'url(' + img.getAttribute('data-src') + ')';
img.classList.remove('lazyload');
}
});
};
</script>
无论使用哪种方法,都需要注意以下几点:
上一篇:php如何实现简单购物车
下一篇:php xls如何转csv
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站