$(function() {
$("img.lazy").attr("src","2.jpg");
show();
$(window).scroll(function () {
show();
});
function show(){
$("img.lazy").each(function () {
var clientH = window.screen.availHeight;
var $height = $(this).height();
var $scroll = $(window).scrollTop();
var $off = $(this).offset().top;
var $val=$(this).attr("data-original");
if ($off - $scroll < clientH) {
$(this).attr("src",$val);
}
})
}
})

注:我写的这个方法,实现的效果是当图片的上边框显示在可视区域内时,把图片的真实路径赋值给src。

  如果需要让图片全部显示在可视区域时再换成真实src的话,需要把if判断中的条件换成   $off + $height - $scroll < clientH

首先给需要延迟加载的img标签加个class名lazy,然后把src的路径赋值给data-original,如下所示:

  <img class="lazy" data-original="images/xinan_searchLogo.png" alt=""/>

然后引入jq文件,然后再把上边的js代码放上即可

思路:1、首先给所有需要延迟加载的图片,添加一个默认的图片(2.jpg),让页面刚开始加载时只加载一张图片

   2、当图片显示在可视区域时,把data-original的值赋给src属性

     3、首页刚进入页面时有些图片就显示在可视区域内 所以要首先执行下show方法。然后再让滚轴滚动时再执行show方法

移动端图片延迟加载(css3实现加载转圈图标)

 function img_lazyLoad(imgSrc){
var $lazy=$("img.lazy");
//$lazy.attr("src",imgSrc);
var str='<div class="loading-circle loading-circle1"></div>'+
'<div class="loading-circle loading-circle2"></div>'+
'<div class="loading-circle loading-circle3"></div>'+
'<div class="loading-circle loading-circle4"></div>'+
'<div class="loading-circle loading-circle5"></div>'+
'<div class="loading-circle loading-circle6"></div>'+
'<div class="loading-circle loading-circle7"></div>'+
'<div class="loading-circle loading-circle8"></div>'+
'<div class="loading-circle loading-circle9"></div>'+
'<div class="loading-circle loading-circle10"></div>'+
'<div class="loading-circle loading-circle11"></div>'+
'<div class="loading-circle loading-circle12"></div>';
$lazy.each(function () {
$(this).parent().addClass("loading"); if($(this).parent(".loading").find(".loading-circle").length == 0){
$(this).parent(".loading").append(str);
}
var clientH = $(window).height(),
$height = $(this).height(),
$scroll = $(window).scrollTop(),
$off = $(this).offset().top,
$val=$(this).attr("data-original");
if ($off + $height - $scroll < clientH) {
$(this).attr("src",$val).removeClass("lazy");
$(this).load(function(){
$(this).parent().removeClass("loading");
$(this).parent().find(".loading-circle").remove()
})
}
})
}
img_lazyLoad();
$(window).on({
scroll:function(){
img_lazyLoad()
}
});
.loading{
position:relative;
}
.loading .loading-circle {
margin:-.49rem 0 0 -.49rem;
width: .98rem;
height: .98rem;
position: absolute;
left: 50%;
top: 50%;
}
.loading .loading-circle:before {
content: '';
display: block;
margin: 0 auto;
width: 15%;
height: 15%;
background-color: #333;
@include border-radius(100%);
}
.loading .loading-circle2 { transform: rotate(30deg);-webkit-transform: rotate(30deg);}
.loading .loading-circle3 { transform: rotate(60deg);-webkit-transform: rotate(60deg);}
.loading .loading-circle4 { transform: rotate(90deg);-webkit-transform: rotate(90deg);}
.loading .loading-circle5 { transform: rotate(120deg);-webkit-transform: rotate(120deg);}
.loading .loading-circle6 { transform: rotate(150deg);-webkit-transform: rotate(150deg);}
.loading .loading-circle7 { transform: rotate(180deg);-webkit-transform: rotate(180deg);}
.loading .loading-circle8 { transform: rotate(210deg);-webkit-transform: rotate(210deg);}
.loading .loading-circle9 { transform: rotate(240deg);-webkit-transform: rotate(240deg);}
.loading .loading-circle10 { transform: rotate(270deg);-webkit-transform: rotate(270deg);}
.loading .loading-circle11 { transform: rotate(300deg);-webkit-transform: rotate(300deg);}
.loading .loading-circle12 { transform: rotate(330deg);-webkit-transform: rotate(330deg);}
@-webkit-keyframes loading-circleFadeDelay {
0%, 39%, 100% { opacity:; }
40% { opacity:; }
}
@keyframes loading-circleFadeDelay {
0%, 39%, 100% { opacity:; }
40% { opacity:; }
}
.loading .loading-circle:before {
animation: loading-circleFadeDelay 1.2s infinite ease-in-out both;
-webkit-animation: loading-circleFadeDelay 1.2s infinite ease-in-out both;
}
.loading .loading-circle2:before {animation-delay: -1.1s; -webkit-animation-delay: -1.1s; }
.loading .loading-circle3:before { animation-delay: -1s;-webkit-animation-delay: -1s;}
.loading .loading-circle4:before { animation-delay: -0.9s;-webkit-animation-delay: -0.9s; }
.loading .loading-circle5:before { animation-delay: -0.8s;-webkit-animation-delay: -0.8s; }
.loading .loading-circle6:before { animation-delay: -0.7s;-webkit-animation-delay: -0.7s; }
.loading .loading-circle7:before { animation-delay: -0.6s;-webkit-animation-delay: -0.6s; }
.loading .loading-circle8:before { animation-delay: -0.5s;-webkit-animation-delay: -0.5s; }
.loading .loading-circle9:before { animation-delay: -0.4s;-webkit-animation-delay: -0.4s; }
.loading .loading-circle10:before { animation-delay: -0.3s;-webkit-animation-delay: -0.3s; }
.loading .loading-circle11:before { animation-delay: -0.2s;-webkit-animation-delay: -0.2s; }
.loading .loading-circle12:before { animation-delay: -0.1s;-webkit-animation-delay: -0.1s; }

图片延迟加载(用jq自己写的方法)的更多相关文章

  1. 第一次自己写jquery图片延迟加载插件,不通用,但修改一下还是可以使用到很多页面上的

    不断修改完善中…… /*! * jquery.lazyoading.js *自定义的页面图片延迟加载插件,比网上的jquery.lazyload简单,也更适合自己的网站 *使用方法: 把img 的cl ...

  2. javascript实现图片延迟加载方法汇总(三种方法)

    看到一些大型网站,页面如果有很多图片的时候,当你滚动到相应的行时,当前行的图片才即时加载的,这样子的话页面在打开只加可视区域的图片,而其它隐藏的图片则不加载,一定程序上加快了页面加载的速度,跟着小编一 ...

  3. 两种图片延迟加载的方法总结jquery.scrollLoading.js与jquery.lazyload.js

    估计网上能查到的最多的两种图片延迟加载方法就是jquery.scrollLoading.js与jquery.lazyload.js了,其中jquery.lazyload.js的调用方法因为有网友爆出的 ...

  4. jquery.lazyload 实现图片延迟加载jquery插件

    看到了淘宝产品介绍中,图片是在下拉滚动条时加载,这是一个很不错的用户体验.减少了页面加载的时间了,也减轻了服务器的压力,就查了下用JQuery..   什么是ImageLazyLoad技术 在页面上图 ...

  5. JS实现移动端图片延迟加载

    图片延迟加载常见的有,jquery.lazyload.js,原生JS实现的echo.js.但是都是必须给图片设置宽高. 因为项目是移动端,而且无法在加载前知道图片的宽高,所以,只好自己写了一个. 既然 ...

  6. JQuery LazyLoad实现图片延迟加载-探究

    对于大量图片的网站,图片延迟加载是提高速度和性能的好方法. 目前图片延迟加载主要分两大块,一是触发加载(根据滚动条位置加载图片):二是自动预加载(加载完首屏后n秒后自动加载其他位置的图片).大体常用的 ...

  7. Lazy Load 图片延迟加载(转)

    jQuery Lazy Load 图片延迟加载来源 基于 jQuery 的图片延迟加载插件,在用户滚动页面到图片之后才进行加载. 对于有较多的图片的网页,使用图片延迟加载,能有效的提高页面加载速度. ...

  8. 利用图片延迟加载来优化页面性能(jQuery)

    图片延迟加载也称懒加载,常用于页面很长,图片很多的页面,以电子商务网站居多,比如大家常上的京东,淘宝,页面以图居多,整个页面少说几百K,多则上兆,如果想一次性加载完成,不仅用户要哭了,服务器也得哭了. ...

  9. 前端页面优化:javascript图片延迟加载

    自己写了个简单的图片延迟加载小插件. 功能如下: 页面刷新,当前屏幕图片直接加载真实地址,body被卷去的部分和下方未显示的部分的img均加载通用图片如:loding.gif 随着屏幕上下滚动加载相应 ...

随机推荐

  1. CentOS6.5安装配置SVN

    安装SVN软件包[root@localhost ~]# yum install subversion#确认是否已安装svn模块[root@localhost ~]# cd /etc/httpd/mod ...

  2. Linux环境下apache性能测试工具ab使用详解

    网站性能压力测试是服务器网站性能调优过程中必不可缺少的一环.只有让服务器处在高压情况下,才能真正体现出软件.硬件等各种设置不当所暴露出的问题. 性能测试工具目前最常见的有以下几种:ab.http_lo ...

  3. OC-数组排序-NSSortDescriptor使用

    OC-数组排序-NSSortDescriptor使用 在Object-c中,当有一个班级类MyClass,还有一个学生类Student.在班级类MyClass中通过一个可变数组NSMutableArr ...

  4. tapping of data 词义来源

    tapping of data 在数据交互的过程 数据被 窃听到. 例如, 网站的账户被泄露, 在用户登陆过程中, 其账号被第三方偷到. tapping 含义 看看 youdao 词典解释: n. [ ...

  5. file_get_contents抓取远程URL内容

    /** * POST URL * @param $url * @param null $post * @return false / string */ public static function ...

  6. Java基础之扩展GUI——高亮元素、上下文菜单、移动旋转元素、自定义颜色(Sketcher 10)

    窗口应用程序. 本例在上一版的基础上实现了高亮元素.移动元素.上下文菜单.旋转元素.设置自定义颜色. 1.自定义常量包: // Defines application wide constants p ...

  7. 助手系列之python的FTP服务器

    电脑的OS是Win7,Python版本是2.7.9,安装了pip 因为python没有内置可用的FTP SERVER,所以先选一个第三方的组件安装上,这里我选的是pyftpdlib pip insta ...

  8. Unable to create SVNRepository object

    I think you are missing at least the library setup stuff:     /*      * Initializes the library to w ...

  9. 关于加权的LIS问题

    蒟蒻Zigzag正在准备联赛...... 这个算是这几天做的唯一一个值得写一写的题吧.首先LIS的N^2暴力dp应该都会写,就是F[i]=Max{F[j]}+1 那么加权的就吧后面的1换成数的权值就行 ...

  10. Json.net对于导航属性的处理(解决对象循环引用)

    对于两张表A.B多对多的关系中,A的导航属性中有B,B的导航属性中有A,这样Json.net对A或者B对象序列化时会形成死循环 所以对于导航属性要加标签 首先在A.B实体类工程(Model)中引用Js ...