Home  >  Article  >  Web Front-end  >  How to use AJAX to implement on-demand loading

How to use AJAX to implement on-demand loading

亚连
亚连Original
2018-05-24 09:53:171393browse

How to use AJAX to implement on-demand loading? Next, I will bring you an article on how to use AJAX to implement on-demand loading [recommended]. I think it’s pretty good, so I’d like to share it with you now and give it as a reference.

Advantages of on-demand loading: In actual surveys, it was found that many netizens have clear direction when visiting the website. They often directly search for the product list they need after entering the homepage. If the customer enters Loading all the homepage information and then displaying it to customers on the homepage will greatly waste website resources and reduce customer experience. Therefore, on-demand loading has become the mainstream of today's website construction.

<!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;

width:400px;

margin:0 auto;

}

li{

width:300px;

height:200px;

border:solid 1px #ddd;

overflow:hidden;

}

</style>

</head>

<body>

<ul>

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

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

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

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

<li><img 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(){

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

$(&#39;li[isLoaded!=1]&#39;).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(&#39;url&#39;);

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

if(url){

//发送ajax

$.get(&#39;rexiao.php&#39;,{}, function(data){

curLi.html(data);

return;

})

}

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

var src = $(this).find(&#39;img&#39;).attr(&#39;src&#39;);

//设置

$(this).find(&#39;img&#39;).attr(&#39;src&#39;,src);

//做标识

$(this).attr(&#39;isLoaded&#39;,&#39;1&#39;);

}

})

})



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

$(window).trigger(&#39;scroll&#39;);

</script>

</body>

</html>

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Implementing file upload with progress bar based on Ajax technology

Discuss the issues related to readyState and status in Ajax Question

Comprehensive analysis of $.Ajax() method parameters (graphic tutorial)

The above is the detailed content of How to use AJAX to implement on-demand loading. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn