Jquery PlugIn for lazy loading items.

来源:互联网 发布:淘宝好的男装店 编辑:程序博客网 时间:2024/05/22 04:44
//lazy load image
(function ($) {
$.fn.onScroll = function (options) {
var defaultOpt = {
callback: null,
params: null,
reserve: 0
};
var opt = $.extend({}, defaultOpt, options);
var $this = $(this);
var _checkScreen = function(ignoreCache){
var scrolltop = $(window).scrollTop();
var wheight = $(window).height();
var offsetTop = $this.offset().top;
if (offsetTop >= scrolltop && offsetTop <= (scrolltop + wheight + (ignoreCache === true ?0:opt.reserve))) {
if ($.isFunction(opt.callback)) {
opt.callback($this, opt.params);
} else {
$(document).trigger(opt.callback, [$this, opt.params]);
}
}
}
$(document).on('dom.scroll', _checkScreen);
_checkScreen(true);
};
$(document).on('dom.load', function () {
$("[data-onscroll]").each(function () {
var $this = $(this);
$this.onScroll({
callback: $this.attr('data-onscroll'),
params: $this.attr('data-onscroll-params'),
reserve: Number($this.attr('data-reserve')) || 0
})
$this.removeAttr("data-onscroll");
});
});
})(jQuery);

PlugIn for lazy loading items.


Usage:

<iframe class="shown" data-onscroll="informaScrollToEvent" data-src="<%= informaUrl %>" width="100%" frameborder="0" height="500px" style="border:1px solid #d7d7d7;"></iframe>
$(document).one('informaScrollToEvent', function (e, iframe, params) {            var url = $(iframe).attr('data-src');            $(iframe).attr('src', url);            $(iframe).removeAttr('data-src');        });




原创粉丝点击