当前位置 : 首页 » 编程语言 » 正文

jQuery滚动加载图片效果的实现

分类 : 编程语言 | 发布时间 : 2018-04-21 09:06:21 | 浏览 : 1

复制代码 代码如下:

<BR>/****************滚动加载图片函数****************/
     function scrollLoading(){
         var st = $(window).scrollTop(), sth = st + $(window).height();
         var $targetpics = $("img.scrloadpic");
         var len = $targetpics.length;
         for(var i=0; i< len; i++)
         {
             var $temppic = $targetpics.eq(i);
             var post = $temppic.position().top;
             var posb = post + $temppic.height();

             if((post > st && post < sth) || (posb > st && posb < sth))
             {

                 var url = $temppic.attr("xsrc");
                 if(url != "")
                 {
                    $temppic.attr("src", url);
                    $temppic.attr("xsrc", "");
                 }
             }
         }
     }


 上面是实现滚动加载的一个功能函数;下面在html代码中需要在img中加一个xsrc属性暂时存放真实图片的路径,src中则存放loading图片的路径,为每个需要实现这个功能的图片加一个名字为“scrloadpic”的类。
复制代码 代码如下:

<img src="load.gif" xsrc="myimgae.jpg" class="scrloadpic"/>

 最后在调用函数。
复制代码 代码如下:

scrollLoading();
$(window).bind("scroll", scrollLoading);

相关阅读:

Remove duplicates from an array of objects in JavaScript

Can I call jquery click() to follow an <a> link if I haven't bound an event handler to it with bind or click already?

Check whether a string matches a regex in JS

jQuery event to trigger action when a div is made visible

Check if user is using IE with jQuery

Resize HTML5 canvas to fit window

How do I replace a character at a particular index in JavaScript?

JavaScript file upload size validation

Converting a string to a date in JavaScript

How to get the data-id attribute?