Thêm lazy load cho Blogspot (Blogger)
Lazy load là gì? Lazyload là plugin của jQuery cải thiện việc tải hình ảnh ở những trang web dài. Ảnh bên ngoài khung hình sẽ chỉ thể hiện khi người dùng kéo chuột tới. Sử dụng Lazyload ở những trang web dài với nhiều hình ảnh sẽ giúp cho trang được tải nhanh hơn, trong một số trường hợp nó còn giúp giảm tải cho máy chủ web.
Cách thêm lazy load ảnh cho Blogspot
Tiến hành
1. Các bạn vào Blogger >> Mẫu >> Chỉnh sửa HTML >> Tìm đến thẻ đóng </body>.
2. Thêm toàn bộ code sau vào trước nó.
<script type='text/javascript'> //<;b.loaded=true}).attr("src",a(b).attr("original"))}});if("scroll"!=c.event){a(b).bind(c.event,function(c){if(!b.loaded){a(b).trigger("appear")}})}});a(c.container).trigger(c.event);return this};a.belowthefold=function(b,c){if(c.container===undefined||c.container===window){var d=a(window).height()+a(window).scrollTop()}else{var d=a(c.container).offset().top+a(c.container).height()}return d<=a(b).offset().top-c.threshold};a.rightoffold=function(b,c){if(c.container===undefined||c.container===window){var d=a(window).width()+a(window).scrollLeft()}else{var d=a(c.container).offset().left+a(c.container).width()}return d<=a(b).offset().left-c.threshold};a.abovethetop=function(b,c){if(c.container===undefined||c.container===window){var d=a(window).scrollTop()}else{var d=a(c.container).offset().top}return d>=a(b).offset().top+c.threshold+a(b).height()};a.leftofbegin=function(b,c){if(c.container===undefined||c.container===window){var d=a(window).scrollLeft()}else{var d=a(c.container).offset().left}return d>=a(b).offset().left+c.threshold+a(b).width()};a.extend(a.expr[":"],{"below-the-fold":"$.belowthefold(a, {threshold : 0, container: window})","above-the-fold":"!$.belowthefold(a, {threshold : 0, container: window})","right-of-fold":"$.rightoffold(a, {threshold : 0, container: window})","left-of-fold":"!$.rightoffold(a, {threshold : 0, container: window})"})})(jQuery);$(function(){$("img").lazyload({placeholder:"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjybKeeGEv74IKXxKROtpIFCRjaGTgd8xkSvuB0nUa88XKR3swjtH4BHCgm3jNkBa4p4VXh_wLAbAqPcvoloRkcK93p7Op5kxyUPYT5-EX5TsFIhjsjvBoQGGp9zQ8fvEhwtXgILVkejb2h/s640/arlinadesign.gif",effect:"fadeIn",threshold:"-50"})}); //]]> </script>3. Lưu lại và tận hưởng kết quả.
Lời kết
Bạn nên chú gì rằng Lazyload ở những trang web dài với nhiều hình ảnh sẽ giúp cho trang được tải nhanh hơn, trong một số trường hợp nó còn giúp giảm tải cho máy chủ web.
Chúc các bạn thành công.
Thanks AdminHieu nhieu, giải pháp them JS rất nhanh và hiệu quả rất tốt, mình cứ tìm cách tích hợp code trên CMS của mình bằng cách thay đổi giá trị IMG rất mất thời gian và kém hiệu suất. Cám ơn Hiếu !
ReplyDeleteOK
Delete