JS scrolling Load

Source: Internet
Author: User

Background
移动web开发中,为了加快响应速度,通常在服务端输出首屏页面,动态异步加载listview。而对于包含大量图片的listview,一起加载将耗费大量http请求,进而影响响应速度。滚动加载这时就可以成为一种可选的优化方案。
Points
    • 生成的img标签用一个属性存储图片地址而不是用src
    • Document.images获取当前img标签
    • Isinsight:是否有元素处于当前可视区域内
    • Window.addeventlistener (' scroll ', lazyload, false);监听滚动事件
Code
<!--Lang:js-->_initscrollevent:function () {var = this;            var lazyload = Self.lazyload = function () {var IMGs = self._getlazyloadimages (); Imgs.foreach (Function (img) {if (Self._isinsight2 (img)) {//Picture if Error, the default figure is substituted for Img.addeventlistener (' Error ', function () {img.src = ' http://cache.50 0boss.com/mobile/touch/images/leyuan/no_img.png '}) img.src = Img.getattribute (' osrc        ')}})} window.addeventlistener (' Scroll ', lazyload, false);    Try load first page of IMGs self._initfirstload ();        }, _initfirstload:function () {//tag if attached to body above, child node may have corresponding attribute not added in//default execution once, delay var self = this;            var interval = setinterval (function () {var IMGs = self._getlazyloadimages (); if (imgs.length) {clearinterval (Interval);            Self.lazyload ();        }}, 1)}, _isinsight2:function (EL) {var top = El.offsettop;        var left = El.offsetleft;        var width = el.offsetwidth;        var height = el.offsetheight;            while (el.offsetparent) {el = el.offsetparent;            Top + = El.offsettop;        Left + = El.offsetleft; } return (Top < (Window.pageyoffset + window.innerheight) && left < (WINDOW.PAGEXOFFSE T + window.innerwidth) && (top + height) > Window.pageyoffset && (left + width) > Wind    Ow.pagexoffset);        },//Get not loaded to lazy picture _getlazyloadimages:function () {var self = this; var lazyimages = [].filter.call (document.images, Function (IMG) {return!! Img.getattribute (' osrc ') && img.getattribute (' osrc '). Length && (!img.getattribute (' src ') | |        Img.getattribute (' src '). length = = 0);        }); Return LAZyimages; }
Add
如果在图片未加载成功的时候有一个类似“加载中”的默认图,可以把img包在div,div设置背景加载图作为默认图,等真正图片加载成功就刚好覆盖刚才的背景加载图

JS scrolling Load

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.