Skip to content

Instantly share code, notes, and snippets.

@letswritetw
Created August 2, 2018 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save letswritetw/084f07341d6bae554ba9b90c413f1bbc to your computer and use it in GitHub Desktop.
Save letswritetw/084f07341d6bae554ba9b90c413f1bbc to your computer and use it in GitHub Desktop.
像Medium的漸近式圖片加載
$(window).on('load', () => {
// 自由尺寸,先處理 padding-bottom
if($('.w-free').length > 0) {
$('.w-free').each(function() {
const w = $(this).find('.js-get-img').data('width'),
h = $(this).find('.js-get-img').data('height'),
pd = (h / w) * 100;
$(this).find('.js-get-img').css('paddingBottom', pd + '%');
});
}
// 監聽 scroll,加載圖片
$('.lazy-image').each(function() {
const self = $(this);
const waypoint = new Waypoint({
element: self[0],
handler() {
const src = self.find('.js-get-img').data('img'); // 抓原始圖路徑
// 建立物件
const img = new Image();
img.src = src;
// 圖片載入完,append 進 div 裡
$(img).on('load', () => {
self.find('.js-get-img').append(img);
setTimeout(function(){
self.find('.js-get-img').addClass('show');
}, 50);
});
// destroy 該 div 的監聽 scroll
this.destroy();
},
offset: '75%'
});
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment