时间:2021-07-01 10:21:17 帮助过:3人阅读
其实实时加载图片思路很简单,页面一开始加载的时候把不需要一开始就加载的图片(如第二屏以下的图片,反正是要往下滚动才能看到的,不如把它们设置成往下滚动的时候再实时加载)路径保持到一个自定义的属性里,如:,src="https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com/https://img.gxlcms.com//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s//Uploads-s/new/2019-09-24-201924/b.gif"是一个1px*1px的透明gif图片,自定义属性data-src里的"http://y3.ifengimg.com/a/2014_37/03c4830691a6d8b.jpg"才是图片的路径,往下滚动到它的时候把data-src里的值替换掉src的值,然后把data-src属性移除掉……
如下是demo
如上demo里的cont1里的图片是一加载页面就显示的,对于首页布局是固定的,可以确认那些图片是第二屏以下的,但是对于内容页,主体内容容器里的图片出现的位置是不可控制的,这就需要加载完毕真个页面的时候再判断是不是在在可视范围内,如果是在可视范围内就把它加载出来(当然,首页也可这么做,但是如果网速很慢,那么打开首页的时候要等到整个页面架构加载完毕之后才显示图片,而不是从上而下加载出来),图片要设置width与height属性,这让它在页面占一个位置,不然为加载前它只占透明gif图片的大小位置。
如下demo针对内容页做了一点修改,window.onload、window.onscroll的时候都触发加载