001 | (function(){ |
002 | /** |
003 | * 功能:图片惰性加载。在需要图片惰性加载的页面底部载入。再将图片加上class=“lazy”。并将图片的真实地址放置在图片的自定义属性dataimg中。 |
004 | * @author haunghm |
005 | * @version 1.0.0 |
006 | * @dependencies jquery 或者 zepto |
007 | */ |
008 | var lazyLoad = { |
009 | |
010 | init: function() { |
011 | var that = this; |
012 | that.onerrorImgUrl = "./images/grey.png";//图片加载失败用什么图片替换 |
013 | that.srcStore = "dataimg"; //图片真实地址存放的自定义属性 |
014 | that.class = "lazy"; //惰性加载的图片需要添加的class |
015 | that.sensitivity = 50; //该值越小,惰性越强(加载越少) |
016 | |
017 | minScroll = 5, |
018 | slowScrollTime = 200, |
019 | ios = navigator.appVersion.match(/(iPhone\sOS)\s([\d_]+)/), |
020 | isIos = ios && !0 || !1, |
021 | isoVersion = isIos && ios[2].split("_"); |
022 | |
023 | isoVersion = isoVersion && parseFloat(isoVersion.length > 1 ? isoVersion.splice(0, 2).join(".") : isoVersion[0], 10), |
024 | isIos = that.isPhone = isIos && isoVersion < 6; |
025 | |
026 | if (isIos) { |
027 | |
028 | var startSyAndTime, |
029 | setTimeOut; |
030 | $(window).on("touchstart",function() { |
031 | startSyAndTime = { |
032 | sy: window.scrollY, |
033 | time: Date.now() |
034 | }, |
035 | setTimeOut && clearTimeout(setTimeOut) |
036 | }).on("touchend",function(e) { |
037 | if (e && e.changedTouches) { |
038 | var subtractionY = Math.abs(window.scrollY − startSyAndTime.sy); |
039 | if (subtractionY > minScroll) { |
040 | var subtractionTime = Date.now() − startSyAndTime.time; |
041 | setTimeOut = setTimeout(function() { |
042 | that.changeimg(), |
043 | startSyAndTime = {}, |
044 | clearTimeout(setTimeOut), |
045 | setTimeOut = null |
046 | }, |
047 | subtractionTime > slowScrollTime ? 0: 200) |
048 | } |
049 | } else { |
050 | that.changeimg(); |
051 | } |
052 | }).on("touchcancel", function() { |
053 | setTimeOut && clearTimeout(setTimeOut), |
054 | startSyAndTime = {} |
055 | }) |
056 | } else { |
057 | $(window).on("scroll", function() { |
058 | that.changeimg(); |
059 | }); |
060 | } |
061 | setTimeout(function() { |
062 | that.trigger(); |
063 | },90); |
064 | |
065 | }, |
066 | trigger: function() { |
067 | var that = this; |
068 | eventType = that.isPhone && "touchend" || "scroll"; |
069 | that.imglist = $('img.'+that.class+''); |
070 | $(window).trigger(eventType); |
071 | }, |
072 | changeimg: function() { |
073 | function loadYesOrno(img) { |
074 | var windowPageYOffset = window.pageYOffset, |
075 | windowPageYOffsetAddHeight = windowPageYOffset + window.innerHeight, |
076 | imgOffsetTop = img.offset().top; |
077 | return imgOffsetTop >= windowPageYOffset && imgOffsetTop − that.sensitivity <= windowPageYOffsetAddHeight; |
078 | } |
079 | function loadImg(img, index) { |
080 | |
081 | var imgUrl = img.attr(that.srcStore); |
082 | img.attr("src", imgUrl); |
083 | img[0].onload || (img[0].onload = function() { |
084 | $(this).removeClass(that.class).removeAttr(that.srcStore), |
085 | that.imglist[index] = null, |
086 | this.onerror = this.onload = null |
087 | }, |
088 | img[0].onerror = function() { |
089 | this.src = that.onerrorImgUrl, |
090 | $(this).removeClass(that.class).removeAttr(that.srcStore), |
091 | that.imglist[index] = null, |
092 | this.onerror = this.onload = null |
093 | }) |
094 | } |
095 | var that = this; |
096 | that.imglist.each(function(index, val) { |
097 | if (!val) return; |
098 | var img = $(val); |
099 | if (!loadYesOrno(img)) return; |
100 | if (!img.attr(that.srcStore)) return; |
101 | loadImg(img, index); |
102 | }) |
103 | |
104 | } |
105 | }; |
106 | lazyLoad.init(); |
107 | |
108 | }()); |