修改jquery.lazyload.js实现页面延迟载入

 

jquery.lazyload.js并未真正的实现页面延迟载入,它的实现原理是页面载入完毕后将html中所有img标签的src属性都替换为空,把src的初始值存储到一个自定义的属性中,当页面滚动到img标签位置时再将图片载入进来。虽然实现了延迟载入的视觉效果,但是通过httpwatch等工具就可以看到,页面载入完毕后又发布很多空请求,而空请求就是由这些被替换过的img标签产生的,空请求也会一定程度上增加页面的打开时间,所以jquery.lazyload.js并未真生的实现页面延迟载入。

我对jquery.lazyload.js做了一些修改,实现了页面的延迟载入,不仅仅是图片,html代码同样可以延迟载入,大大减少了页面发出的请求数,提高了页面的打开速度。

修改后的jquery.lazyload.js代码如下:

 

调用方法


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <meta name="keywords" content="汽车图片,汽车图片壁纸, 汽车图片桌面, 汽车图片库,汽车图片大全" />

    <meta name="description" content="汽车图片,网上车市汽车图片大全频道为网友提供各种汽车图片及美女图片下载,各种车型汽车图片桌面,汽车图片壁纸,车展美女图片,美女车模图片等。精彩图片,尽在网上车市。" />

    <title>【汽车图片|汽车美女图片-汽车图片大全】-网上车市</title>

    <style>

        .switch_textarea

        {

            display: none;

        }

        .div_style

        {

            width: 600px;

            height: 400px;

        }

    </style>

    <script src="js/jquery.js" type="text/javascript"></script>

    <script type="text/javascript" src="jquery.lazyload.js"></script>

    <script>

        $().ready(function () {

            $(".switch_textarea").lazyload();

        });

    </script>

</head>

<body>

    <h2>

        第1张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd2385fd03.jpg" /></textarea></div>

    <h2>

        第2张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd2366adec.jpg" /></textarea></div>

    <h2>

        第3张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd233b8da4.jpg" /></textarea></div>

    <h2>

        第4张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd231a0fd8.jpg" /></textarea></div>

    <h2>

        第5张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd22e68f56.jpg" /></textarea></div>

    <h2>

        第6张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd22c55a5d.jpg" /></textarea></div>

    <h2>

        第7张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd2278872b.jpg" /></textarea></div>

    <h2>

        第8张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd224a0f6e.jpg" /></textarea></div>

    <h2>

        第9张图</h2>

    <div class="div_style">

        <textarea class="switch_textarea"><img src="http://img.cheshi-img.com/product/1_720/1957/4bdfd21fb42bc.jpg" /></textarea></div>

</body>

</html>

测试页面的html代码中将所有要载入的图片都放到了textarea标签中,其目的就是防止页面载入时这些img标签向服务器发出请求,从而实现减少不必要的请求数,实际应用中也可以将不需第一次载入的html代码放到textarea中。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值