zoukankan      html  css  js  c++  java
  • [麦先生]如何使用AJAX实现按需加载

     按需加载的优势:在实际调查中发现,很多的网民在游览网站时具有明确的指向性,往往在进入主页后直接搜索进入自己需要的商品列表内,如果在客户进入主页时将主页信息全部加载完毕后展示给顾客,会极大的浪费网站资源,同时也会降低客户体验度,因而按需加载则成为了当今网站构建的主流;

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>按需加载图片</title>

    <style type="text/css">

    *{margin:0px;padding:0px;list-style:none;}

    ul{

    height:auto;

    overflow:hidden;

    400px;

    margin:0 auto;

    }

    li{

    300px;

    height:200px;

    border:solid 1px #ddd;

    overflow:hidden;

    }

    </style>

    </head>

    <body>

    <ul>

    <li><img data-src="./sunli/1.jpg" alt="" width="100%"></li>

    <li><img data-src="./sunli/2.jpg" alt="" width="100%"></li>

    <li><img data-src="./sunli/3.jpg" alt="" width="100%"></li>

    <li><img data-src="./sunli/4.jpg" alt="" width="100%"></li>

    <li><img data-src="./sunli/5.jpg" alt="" width="100%"></li>

    <li url="./rexiao.php">

    </li>

    </ul>

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

    <script type="text/javascript">

    //绑定窗口的滚动事件

    $(window).scroll(function(){

    //遍历检测里面的元素尺寸

    $('li[isLoaded!=1]').each(function(){

    //获取滚动高度

    var sT = $(window).scrollTop();

    //获取窗口的可视区域的高度

    var cT = $(window).height();

    //获取元素距离文档顶部的偏移量

    var t = $(this).offset().top;

    //暂存当前元素对象

    var curLi = $(this);

    //检测判断

    if(t <= sT + cT){

    //检测是否具有url属性

    var url = $(this).attr('url');

    //如果有  发送ajax 获取请求之后的数据

    if(url){

    //发送ajax

    $.get('rexiao.php',{}, function(data){

    curLi.html(data);

    return;

    })

    }

    //这个时候要显示了 修改元素的src属性 

    var src = $(this).find('img').attr('data-src');

    //设置

    $(this).find('img').attr('src',src);

    //做标识

    $(this).attr('isLoaded','1');

    }

    })

    })

    //使用代码来触发滚动事件  

    $(window).trigger('scroll');

    </script>

    </body>

    </html>

  • 相关阅读:
    JavaScript 的 Promise
    MacOS copy图标shell脚本
    ExtJS 修改load paging时的参数
    JSONP
    8种跨域解决方案
    Ext Store Proxy Ajax
    ExtJS 自定义组件
    MacOS Apache配置
    xshell 上传 下载文件
    shell 内网主机存活探测器
  • 原文地址:https://www.cnblogs.com/maixiansheng/p/5484295.html
Copyright © 2011-2022 走看看