fixed 懒加载效果

This commit is contained in:
Vaica committed 2026-01-31 17:24:31 +08:00
1 parent c26a5ef875
commit 53f2c6076c
3 files changed
+126 -128

No files matched your search

+97 -1
View File
@@ -1 +1,97 @@
!function(t){function o(o,a,i){var n,c,e=new Image;return e.src=o,e.onload=function(){a(e.width,e.height),r.onLoadBackStart(i,t("img[data-lazy-src]:eq("+i+")"))},[n,c]}function a(a,n){var c=l.eq(a).attr("data-lazy-src");o(c,function(){try{i(t("img[data-lazy-src]:eq("+a+")"),a,n)}catch(t){}},a)}function i(o,a,i){if(!o.attr("data-comp")){o.css("visibility","hidden"),o.attr("src",o.attr("data-lazy-src"));var c=o.width(),e=o.height(),d=o.offset().top,f=o.offset().left;o.css("visibility","visible"),t("body").append("<div class='meng-lazy-div"+a+"' style='background-color: "+r.coverColor+";position:absolute;width:"+c+"px;height:"+e+"px;top:"+d+"px;left:"+f+"px;z-index:500'>"+r.coverDiv+"</div>"),n(a,i,o),o.attr("data-comp","true")}}function n(o,a,i){t(".meng-lazy-div"+o).animate({opacity:"0"},r.showTime,function(){t(this).remove(),r.onLoadBackEnd(o,i),a()})}function c(){var o=t(document).scrollTop(),i=[];l.each(function(a){var n=t(this);n.attr("data-comp")||n.offset().top-o+r.offsetTopm>=0&&n.offset().top-o<s+r.offsetBottom&&i.push(a)}),0!=i.length&&a(i[0],function(){c()})}function e(){c()}function d(o){r=t.extend(r,o),l.each(function(){var o=t(this).attr("data-lazy-src");f.push(o),t(this).attr("src",A)}),e(),window.onscroll=function(){e()}}var r={coverColor:"#dfdfdf",coverDiv:"",showTime:300,offsetBottom:0,offsetTopm:50,onLoadBackEnd:function(t,o){},onLoadBackStart:function(t,o){}},f=[],s=t(window).height(),l=(t(window).width(),t("img[data-lazy-src]")),A="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";window.lazyLoadInit=d}($);
(function ($) {
var defaults = {
showTime: 800,
offsetBottom: 0,
offsetTopm: 50,
onLoadBackEnd: function (index, dom) { },
onLoadBackStart: function (index, dom) { }
};
var $lazyImgList = $();
function checkOffset() {
var scrollTop = $(document).scrollTop();
var windowHeight = $(window).height();
$lazyImgList.each(function (index) {
var $dom = $(this);
if ($dom.attr("data-comp")) return; // Already processed
var offsetTop = $dom.offset().top;
// Check if image is in viewport (with some buffer)
if (offsetTop - scrollTop + defaults.offsetTopm >= 0 &&
offsetTop - scrollTop < (windowHeight + defaults.offsetBottom)) {
showImg($dom, index);
}
});
}
function showImg($dom, index) {
$dom.attr("data-comp", "true");
var src = $dom.attr("data-lazy-src");
defaults.onLoadBackStart(index, $dom);
// Create a temporary image to preload
var temp = new Image();
temp.onload = function() {
// Prepare for transition
$dom.css({
"opacity": 0,
"transition": "opacity " + (defaults.showTime / 1000) + "s ease-in-out"
});
// Swap source
$dom.attr("src", src);
// Force reflow to ensure transition triggers
void $dom[0].offsetWidth;
// Fade in
$dom.css("opacity", 1);
defaults.onLoadBackEnd(index, $dom);
};
temp.onerror = function() {
// On error, show anyway (maybe broken image icon) but ensure visible
$dom.attr("src", src);
$dom.css("opacity", 1);
};
temp.src = src;
}
function init(setting) {
defaults = $.extend(defaults, setting);
// Auto-convert standard images to lazy load format
// Exclude .blog-img (Header Avatar) to prevent visibility issues
$("img").not(".blog-img").each(function() {
var $this = $(this);
// Only convert if not already converted and has a src
if (!$this.attr("data-lazy-src") && $this.attr("src")) {
$this.attr("data-lazy-src", $this.attr("src"));
// Set placeholder
$this.attr("src", "data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");
}
});
$lazyImgList = $("img[data-lazy-src]");
checkOffset();
// Debounced scroll handler could be better, but sticking to simple binding for now
$(window).off("scroll.lazy").on("scroll.lazy resize.lazy", function() {
checkOffset();
});
}
function tryAgain() {
// Re-initialize for new content
init(defaults);
}
window.lazyLoadInit = init;
window.lazyLoadAgain = tryAgain;
})(jQuery);