1. 如何判斷一個(gè)元素是否出現(xiàn)在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)日熬。寫一個(gè)函數(shù) isVisible實(shí)現(xiàn)
function isVisible($node){
var windowHeight = $(window).height(),//瀏覽器窗口可視區(qū)域的高度
scrollTop = $(window).scrollTop(),//窗口滾動(dòng)的頂部偏移量,即此時(shí)頁面的上邊界到可視區(qū)域的上邊界的偏移量,簡單的可以理解成整個(gè)頁面滾動(dòng)了多少距離
offsetTop = $node.offset().top,//元素的絕對(duì)偏移量,指元素的實(shí)際尺寸(即不包括外邊框margin)的上邊界到頁面頂端的距離.這個(gè)值不隨窗口滾動(dòng)而改變
nodeHeight = $node.outerHeight();//元素的實(shí)際尺寸,即 height+padding+border,
// $('#dom').outerHeight(true)#dom的實(shí)際尺寸及外邊距,即 height+padding+border+margin
if(windowHeigth+scrollTop>offsetTop && scrollTop<offsetTop+nodeHeight){
return true;
}else{
return false;
}
//scrollTop< offsetTop+nodeHeight瀏覽器上邊緣
//windowHeight+scrollTop>offsetTop瀏覽器下邊緣
}
2.當(dāng)窗口滾動(dòng)時(shí),判斷一個(gè)元素是不是出現(xiàn)在窗口可視范圍友题。每次出現(xiàn)都在控制臺(tái)打印 true 郁季。用代碼實(shí)現(xiàn)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>懶加載2</title>
<style>
body{
font: 20px/1.5 ;
background: #FF9279;
padding: 30px;
height: 2500px;
}
.p1{
position: absolute;
top: 1500px;
left: 40%;
padding: 50px;
border: 2px solid #FF0500;
background: #0096CC;
}
</style>
</head>
<body>
<p class="p1">hello</P>
<script src="jquery-3.2.0.min.js"></script>
<script>
$(window).on('scroll',function(){
if(isShow($('.p1'))){
console.log(true)
}else{
console.log(false)
}
})
function isShow($node){
var windowHeight = $(window).height(),
scrollTop = $(window).scrollTop(),
offsetTop = $node.offset().top,
nodeHeight = $node.outerHeight();
if(windowHeight+scrollTop>offsetTop && scrollTop <offsetTop +nodeHeight){
return true
}else{
return false;
}
}
</script>
</body>
</html>
3. 當(dāng)窗口滾動(dòng)時(shí),判斷一個(gè)元素是不是出現(xiàn)在窗口可視范圍浴骂。在元素第一次出現(xiàn)時(shí)在控制臺(tái)打印 true乓土,以后再次出現(xiàn)不做任何處理。用代碼實(shí)現(xiàn)
<p class="p1">hello</P>
<script src="jquery-3.2.0.min.js"></script>
<script>
var lock = false//設(shè)置一個(gè)鎖
$(window).on('scroll',function(){
if(!lock){//在滾動(dòng)時(shí)不會(huì)在執(zhí)行下面函數(shù)
isShow($('.p1'))
}
})
function isShow($node){
var windowHeight = $(window).height(),
scrollTop = $(window).scrollTop(),
offsetTop = $node.offset().top,
nodeHeight = $node.outerHeight();
if(windowHeight+scrollTop>offsetTop && scrollTop <offsetTop +nodeHeight){
console.log(true)
lock = true;//當(dāng)?shù)谝怀霈F(xiàn)在可是區(qū)時(shí)把鎖設(shè)置true
return true
}else{
return false;
}
}
<body>
<p class="p1">hello</P>
<script src="jquery-3.2.0.min.js"></script>
<script>
$(window).on('scroll',function(){
isCheck($('.p1'))
})
function isShow($node){
var windowHeight = $(window).height(),
scrollTop = $(window).scrollTop(),
offsetTop = $node.offset().top,
nodeHeight = $node.outerHeight();
if(windowHeight+scrollTop>offsetTop && scrollTop <offsetTop +nodeHeight){
return true
}else{
return false;
}
}
function isCheck($node){
if($node.not('.load').length===1&& isShow($node)){
console.log(true)
$node.addClass('load')
}else{
return;
}
}
</script>
</body>
4. 圖片為什么懶加載,懶加載的原理是什么趣苏?
為什么懶加載:如果頁面的圖片過多狡相,打開時(shí)頁面時(shí)會(huì)同時(shí)發(fā)很多請(qǐng)求去加載圖片,會(huì)阻塞頁面的加載速度食磕。
原理:把圖片的src放在自定義的屬性上data-src尽棕,當(dāng)滾動(dòng)頁面時(shí)把可視區(qū)的圖片自定義屬性值換成src屬性值。
5.實(shí)現(xiàn)圖片的懶加載
<script>
check(); //打開網(wǎng)頁把在可視區(qū)的img展示出來
$(window).on('scroll', check)
function check(){
$('.container img').not('.load').each(function(){//遍歷沒有.load的img
if(isShow($(this))){//如果在可視區(qū)
show($(this))//執(zhí)行函數(shù)
}
})
}
function show($imgs){
$imgs.each(function(){//遍歷所有imgs
var imgUrl = $(this).attr('data-src');//獲取自定義屬性
$(this).attr('src',imgUrl);//改變自定義的屬性值
$(this).addClass('load')
})
}
function isShow($node){
var windowHeight = $(window).height(),
scrollTop = $(window).scrollTop(),
offsetTop = $node.offset().top,
nodeHeight = $node.height();
if(windowHeight+scrollTop>offsetTop && scrollTop< offsetTop+nodeHeight){//可視區(qū)域
return true;
}else{
return false;
}
}
</script>
6.懶加載新聞
<style>
html,body,h2,p,ul,li{
margin:0px;
padding:0px;
list-style: none;
}
a{
color:#333;
text-decoration:none;
}
.container{
width:600px;
margin:0 auto;
}
.item{
margin-top:20px;
}
.item:after{
content:'';
display: block;
clear:both;
}
.item .thumb img{
width:100px;
height:100px;
}
.item .thumb{
float:left;
}
.item h2{
margin-left:120px;
font-size:14px;
}
.item p{
margin-left:120px;
font-size:14px;
margin-top:60px;
color:#ccc;
}
.load-more{
visibility: hidden;
margin: 3px;
height: 3px;
}
</style>
</head>
<body>
<div class="container">
<ul class="news">
<!--<li>
<a>
<div>
<img src="" alt="">
</div>
<h2></h2>
<p></p>
</a>
</li>
-->
</ul>
<p class="load-more">沒有更多了</p>
</div>
<script src="jquery-3.2.0.min.js"></script>
<script>
var pageIndex =0;//每次后端給一頁數(shù)據(jù) 一頁數(shù)據(jù)加載2個(gè)新聞
var isOver = false;// 數(shù)據(jù)全部被加載完以后為true;
var isNewsArrive = true ; //滾動(dòng)時(shí)數(shù)據(jù)是否到了 給個(gè)鎖
getNews();
$(window).on('scroll',checkNews);
function checkNews(){
if(isShow($('.load-more')) && !isOver && isNewsArrive){
getNews();
}
}
function getNews(){
isNewsArrive = false;
$.get('/getNews',{page:pageIndex}).done(function(ret){
isNewsArrive = true //數(shù)據(jù)到了
if(ret.status === 0){//與后端約定status===0時(shí)數(shù)據(jù)發(fā)送成功
pageIndex++;
appendHtml(ret.data)//ret.data獲取后端retNews
checkNews();//如果.load-more出現(xiàn)在可視區(qū)繼續(xù)獲取
}else{
alert('獲取新聞出錯(cuò)')
}
}).fail(function(){
alert('系統(tǒng)異常')
})
}
function appendHtml(news){
if(news.length === 0){
isOver = true;
$('.container').append('<p>沒有更多了</p>')
return ;//數(shù)據(jù)全部加載完后
}
var html ='';//接受到數(shù)據(jù)后拼接html
$.each(news,function(){
html +='<li class="item">';
html +=' <a href="'+this.link+'">';
html += '<div class="thumb"> ![]('+ this.img +')</div>';
html +='<h2>'+this.title+'</h2>';
html +=' <p>'+this.brif+'</P>';
html +='</a></li>';
})
$('.news').append(html);
}
function isShow($node){
var windowHeight = $(window).height(),
scrollTop = $(window).scrollTop(),
offsetTop = $node.offset().top,
nodeHeight = $node.outerHeight(true);
if(windowHeight+scrollTop>offsetTop && scrollTop< offsetTop+nodeHeight){
return true
}else{
return false
}
}
</script>
//后端
app.get('/getNews',function(req,res){
var news = [
{
link:'http://mil.qq.com/mil_index.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289576514_150120/0',
title:'韓媒:韓國旅行社停止銷售中國旅游產(chǎn)品 銷量銳減',
brif:'薩德對(duì)韓國人民生活的影響-1'
},
{
link:'http://news.qq.com/l/milite/jqlw/listjunqingliaowang2012.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290217879_150120/0',
title:'陸克文:特朗普時(shí)期彬伦,臺(tái)灣問題不再是中美臺(tái)面上問題',
brif:'薩德對(duì)韓國人民生活的影響-2'
},
{
link:'http://news.qq.com/l/milite/milgn/list2010122872223.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290161671_150120/0',
title:'樂天免稅店銷售額銳減25% 韓國免稅店開拓東南亞市場',
brif:'薩德對(duì)韓國人民生活的影響-3'
},
{
link:'http://news.qq.com/l/milite/zhoubiansaomiao/list2012095132256.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290216074_150120/0',
title:'朝中社:美國“反恐戰(zhàn)”是前所未聞的國家恐怖行為',
brif:'薩德對(duì)韓國人民生活的影響-4'
},
{
link:'http://news.qq.com/l/milite/milhqj/list2010122872321.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289499164_150120/0',
title:'美前防長:對(duì)朝鮮動(dòng)武風(fēng)險(xiǎn)太大滔悉,還是談判吧',
brif:'薩德對(duì)韓國人民生活的影響-5'
},
{
link:'http://news.qq.com/l/milite/milhqj/list2010122872321.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290217031_150120/0',
title:'央視揭秘遼寧艦首次遠(yuǎn)航 露臉的仨人啥來頭?',
brif:'薩德對(duì)韓國人民生活的影響-6'
},
{
link:'http://news.qq.com/l/milite/junbei/list2012095132410.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289499766_150120/0',
title:'媒體:留給和平解決朝核問題的時(shí)間或許所剩無多',
brif:'薩德對(duì)韓國人民生活的影響-7'
},
{
link:'http://v.qq.com/cover/j/j02y37wjjgnxdel.html?vid=q0016flpc3k',
img:'http://inews.gtimg.com/newsapp_ls/0/1289332905_150120/0',
title:'德女防長回懟特朗普欠軍費(fèi)言論:我們不欠北約的錢',
brif:'薩德對(duì)韓國人民生活的影響-8'
},
{
link:'http://news.qq.com/l/milite/gaoqingtuku/listgaoqingtuku2012.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289495870_150120/0',
title:'俄媒:學(xué)生超越老師 中國造艦已遙遙領(lǐng)先俄羅斯',
brif:'薩德對(duì)韓國人民生活的影響-9'
},
{
link:'http://mil.qq.com/mil_index.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289576514_150120/0',
title:'韓媒:韓國旅行社停止銷售中國旅游產(chǎn)品 銷量銳減',
brif:'薩德對(duì)韓國人民生活的影響-1'
},
{
link:'http://news.qq.com/l/milite/jqlw/listjunqingliaowang2012.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290217879_150120/0',
title:'陸克文:特朗普時(shí)期媚朦,臺(tái)灣問題不再是中美臺(tái)面上問題',
brif:'薩德對(duì)韓國人民生活的影響-2'
},
{
link:'http://news.qq.com/l/milite/milgn/list2010122872223.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290161671_150120/0',
title:'樂天免稅店銷售額銳減25% 韓國免稅店開拓東南亞市場',
brif:'薩德對(duì)韓國人民生活的影響-3'
},
{
link:'http://news.qq.com/l/milite/zhoubiansaomiao/list2012095132256.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290216074_150120/0',
title:'朝中社:美國“反恐戰(zhàn)”是前所未聞的國家恐怖行為',
brif:'薩德對(duì)韓國人民生活的影響-4'
},
{
link:'http://news.qq.com/l/milite/milhqj/list2010122872321.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289499164_150120/0',
title:'美前防長:對(duì)朝鮮動(dòng)武風(fēng)險(xiǎn)太大氧敢,還是談判吧',
brif:'薩德對(duì)韓國人民生活的影響-5'
},
{
link:'http://news.qq.com/l/milite/milhqj/list2010122872321.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290217031_150120/0',
title:'央視揭秘遼寧艦首次遠(yuǎn)航 露臉的仨人啥來頭?',
brif:'薩德對(duì)韓國人民生活的影響-6'
},
{
link:'http://news.qq.com/l/milite/junbei/list2012095132410.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289499766_150120/0',
title:'媒體:留給和平解決朝核問題的時(shí)間或許所剩無多',
brif:'薩德對(duì)韓國人民生活的影響-7'
},
{
link:'http://v.qq.com/cover/j/j02y37wjjgnxdel.html?vid=q0016flpc3k',
img:'http://inews.gtimg.com/newsapp_ls/0/1289332905_150120/0',
title:'德女防長回懟特朗普欠軍費(fèi)言論:我們不欠北約的錢',
brif:'薩德對(duì)韓國人民生活的影響-8'
},
{
link:'http://news.qq.com/l/milite/gaoqingtuku/listgaoqingtuku2012.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289495870_150120/0',
title:'俄媒:學(xué)生超越老師 中國造艦已遙遙領(lǐng)先俄羅斯',
brif:'薩德對(duì)韓國人民生活的影響-9'
},
{
link:'http://mil.qq.com/mil_index.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289576514_150120/0',
title:'韓媒:韓國旅行社停止銷售中國旅游產(chǎn)品 銷量銳減',
brif:'薩德對(duì)韓國人民生活的影響-1'
},
{
link:'http://news.qq.com/l/milite/jqlw/listjunqingliaowang2012.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290217879_150120/0',
title:'陸克文:特朗普時(shí)期询张,臺(tái)灣問題不再是中美臺(tái)面上問題',
brif:'薩德對(duì)韓國人民生活的影響-2'
},
{
link:'http://news.qq.com/l/milite/milgn/list2010122872223.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290161671_150120/0',
title:'樂天免稅店銷售額銳減25% 韓國免稅店開拓東南亞市場',
brif:'薩德對(duì)韓國人民生活的影響-3'
},
{
link:'http://news.qq.com/l/milite/zhoubiansaomiao/list2012095132256.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290216074_150120/0',
title:'朝中社:美國“反恐戰(zhàn)”是前所未聞的國家恐怖行為',
brif:'薩德對(duì)韓國人民生活的影響-4'
},
{
link:'http://news.qq.com/l/milite/milhqj/list2010122872321.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289499164_150120/0',
title:'美前防長:對(duì)朝鮮動(dòng)武風(fēng)險(xiǎn)太大孙乖,還是談判吧',
brif:'薩德對(duì)韓國人民生活的影響-5'
},
{
link:'http://news.qq.com/l/milite/milhqj/list2010122872321.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290217031_150120/0',
title:'央視揭秘遼寧艦首次遠(yuǎn)航 露臉的仨人啥來頭?',
brif:'薩德對(duì)韓國人民生活的影響-6'
},
{
link:'http://news.qq.com/l/milite/junbei/list2012095132410.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289499766_150120/0',
title:'媒體:留給和平解決朝核問題的時(shí)間或許所剩無多',
brif:'薩德對(duì)韓國人民生活的影響-7'
},
{
link:'http://v.qq.com/cover/j/j02y37wjjgnxdel.html?vid=q0016flpc3k',
img:'http://inews.gtimg.com/newsapp_ls/0/1289332905_150120/0',
title:'德女防長回懟特朗普欠軍費(fèi)言論:我們不欠北約的錢',
brif:'薩德對(duì)韓國人民生活的影響-8'
},
{
link:'http://news.qq.com/l/milite/gaoqingtuku/listgaoqingtuku2012.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289495870_150120/0',
title:'俄媒:學(xué)生超越老師 中國造艦已遙遙領(lǐng)先俄羅斯',
brif:'薩德對(duì)韓國人民生活的影響-9'
},
{
link:'http://mil.qq.com/mil_index.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289576514_150120/0',
title:'韓媒:韓國旅行社停止銷售中國旅游產(chǎn)品 銷量銳減',
brif:'薩德對(duì)韓國人民生活的影響-1'
},
{
link:'http://news.qq.com/l/milite/jqlw/listjunqingliaowang2012.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290217879_150120/0',
title:'陸克文:特朗普時(shí)期份氧,臺(tái)灣問題不再是中美臺(tái)面上問題',
brif:'薩德對(duì)韓國人民生活的影響-2'
},
{
link:'http://news.qq.com/l/milite/milgn/list2010122872223.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290161671_150120/0',
title:'樂天免稅店銷售額銳減25% 韓國免稅店開拓東南亞市場',
brif:'薩德對(duì)韓國人民生活的影響-3'
},
{
link:'http://news.qq.com/l/milite/zhoubiansaomiao/list2012095132256.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290216074_150120/0',
title:'朝中社:美國“反恐戰(zhàn)”是前所未聞的國家恐怖行為',
brif:'薩德對(duì)韓國人民生活的影響-4'
},
{
link:'http://news.qq.com/l/milite/milhqj/list2010122872321.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289499164_150120/0',
title:'美前防長:對(duì)朝鮮動(dòng)武風(fēng)險(xiǎn)太大唯袄,還是談判吧',
brif:'薩德對(duì)韓國人民生活的影響-5'
},
{
link:'http://news.qq.com/l/milite/milhqj/list2010122872321.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1290217031_150120/0',
title:'央視揭秘遼寧艦首次遠(yuǎn)航 露臉的仨人啥來頭?',
brif:'薩德對(duì)韓國人民生活的影響-6'
},
{
link:'http://news.qq.com/l/milite/junbei/list2012095132410.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289499766_150120/0',
title:'媒體:留給和平解決朝核問題的時(shí)間或許所剩無多',
brif:'薩德對(duì)韓國人民生活的影響-7'
},
{
link:'http://v.qq.com/cover/j/j02y37wjjgnxdel.html?vid=q0016flpc3k',
img:'http://inews.gtimg.com/newsapp_ls/0/1289332905_150120/0',
title:'德女防長回懟特朗普欠軍費(fèi)言論:我們不欠北約的錢',
brif:'薩德對(duì)韓國人民生活的影響-8'
},
{
link:'http://news.qq.com/l/milite/gaoqingtuku/listgaoqingtuku2012.htm',
img:'http://inews.gtimg.com/newsapp_ls/0/1289495870_150120/0',
title:'俄媒:學(xué)生超越老師 中國造艦已遙遙領(lǐng)先俄羅斯',
brif:'薩德對(duì)韓國人民生活的影響-9'
},
]
var pageIndex = req.query.page;
var len = 2;
var retNews = news.slice(pageIndex*len,pageIndex*len+len)//獲取數(shù)據(jù)第一次0 2 /第二次 2 4 /....
res.send({
status:0,
data:retNews
})
})
版權(quán)歸饑人谷 楠柒 所有 如有轉(zhuǎn)載請(qǐng)附上地址