(function($){ $.apeng={ mouseenter:function(){ $(this).siblings().css({"opacity":.5}) }, mouseleave:function(){ $(this).siblings().css({"opacity":1}) }, init:function(){ $(".banners div,friendlink .row div,.row.hover div").on("mouseenter",$.apeng.mouseenter); $(".banners div,friendlink .row div,.row.hover div").on("mouseleave",$.apeng.mouseleave) } }; $(function(){ $.apeng.init(); }) function lazy(classname){ this.classname = classname; // lazy.prototype.isvisible = function ($node) { var scrolltop = $(window).scrolltop(); // 获取滚动条滚动高度 var windowheight = $(window).height(); // 获取视窗高度 var offsettop = $node.offset().top; // 获取元素相对整个文档的高度 if (offsettop < scrolltop + windowheight && offsettop > scrolltop) { return true; } return false; }; lazy.prototype.init = function() { this._self = this; // $(this.classname).attr("sign", "false"); $(window).scroll(this.onscroll.bind(this)); //强制指定this this.onscroll(); }; lazy.prototype.onscroll = function() { // 这里this指向实例 var that = this; $(this.classname).each(function () { // 对元素使用isvisible()检测 // 这里this指向img dom元素 if (that.isvisible($(this)) && $(this).attr("sign") === "false") { $(this).attr("sign", "true"); $(this).attr('src', $(this).attr('data-src')); } }) } } jquery.extend({ 'lazy': function(arg1) { return new lazy(arg1); } }) })(jquery)