一、問答
1、如何判斷一個元素是否出現(xiàn)在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。寫一個函數(shù) isVisible實現(xiàn)
function isVisible($node){
var scrollH=$(window).height()+$(window).scrollTop(),
selfDoc=$node.offset().top,
selfH=$node.outerHeight(true);
if (selfDoc < scrollH && scrollH < selfDoc+selfH ) {
return true
}
else return false
}
2、當(dāng)窗口滾動時,判斷一個元素是不是出現(xiàn)在窗口可視范圍。每次出現(xiàn)都在控制臺打印 true 。用代碼實現(xiàn)
$(window).on("scroll",function () {
if (isVisible($node)){
console.log("true");
}
})
3、當(dāng)窗口滾動時,判斷一個元素是不是出現(xiàn)在窗口可視范圍。在元素第一次出現(xiàn)時在控制臺打印 true,以后再次出現(xiàn)不做任何處理。用代碼實現(xiàn).
$(window).on("scroll",function () {
$("$node").each(function () {
var $cur=$(this);
if (lode($cur.attr("isLoded"))){
return
}
if (isVisible($cur)){
lode($cur);
}
});
function lode($ele) {
$ele.attr("isLoded",true);
}
function isVisible($node){
var scrollH=$(window).height()+$(window).scrollTop(),
selfDoc=$node.offset().top,
selfH=$node.outerHeight(true);
if (selfDoc < scrollH && scrollH < selfDoc+selfH ) {
return true
}
else {
return false
}
}
})
4、圖片懶加載的原理是什么?
通過判斷當(dāng)前元素是否出現(xiàn)在瀏覽器窗口的可視范圍內(nèi)(滾動的垂直距離加上窗口的垂直距離大于指定元素到文檔的垂直距離),如果在則立即加載事先設(shè)定好的圖片【通過屬性設(shè)置的方法,將自定義的img屬性值(預(yù)加載的真實url地址)把原有事先設(shè)定好的非真實img的src值給替換掉】;
二、代碼
(一)、實現(xiàn)如下回到頂部效果
當(dāng)頁面滾動到一定距離時,窗口右下角會出現(xiàn)回到頂部按鈕,點擊按鈕頁面會滾動到頂部。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
p{
line-height: 3;
}
div.ct{
position: relative;
}
div.goTop{
position: fixed;
top: 560px;
left: 1200px;
border: 1px solid red;
border-radius: 3px;
padding: 10px 20px;
cursor: pointer;
display: none;
}
</style>
</head>
<body>
<div class="ct">
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<p>內(nèi)容1</p>
<p>內(nèi)容2</p>
<p>內(nèi)容3</p>
<div class="goTop">回到頂部</div>
</div>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
<script>
$(window).on("scroll",function () {
if (canShow()){
$(".goTop").css("display","block");
}
else {
$(".goTop").css("display","none");
}
});
$(".goTop").on("click",function () {
$(window).scrollTop(0);
});
function canShow() {
var winH=$(window).height(),
scrollH=$(window).scrollTop();
if ( scrollH > winH ){
return true;
}
else {
return false
}
}
</script>
</body>
</html>
預(yù)覽地址:https://github.com/have-not-BUG/task/blob/master/renwu/renwu29/renwu29-1.html
(二)、實現(xiàn)如下圖片懶加載效果
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>加載更多</title>
<style>
ul,li{
list-style: none;
}
.ct{
margin: 0 auto;
width: 800px;
}
.ct li{
margin: 10px 10px;
float: left;
width: 220px;
}
.ct li img{
width: 220px;
height: 400px ;
}
p{
float: left;
width: 50px;
}
.clearfix::after{
content: "";
display: block;
clear: both;
}
</style>
<body>
<ul class="ct clearfix">
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<p id="hello">hello </p>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
<li><a href="#"></a></li>
</ul>
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.js"></script>
<script>
showCheck ();
$(window).on("scroll",function () {
showCheck ();
});
function showCheck () {
$(".ct img").each(function () {
var $cur=$(this);
if ($cur.attr("isLoded")){
return
}
if (isVisible($cur)){
lode($cur);
}
});
function lode($ele) {
$ele.attr("isLoded",true);
$ele.attr("src", $ele.attr("data-src"))
}
function isVisible($node){
var scrollH=$(window).height()+$(window).scrollTop(),
selfDoc=$node.offset().top;
if (selfDoc < scrollH ) {
return true
}
else {
return false
}
}
}
</script>
</body>
</html>
預(yù)覽地址:https://github.com/have-not-BUG/task/blob/master/renwu/renwu29/renwu29-2.html
(三)、實現(xiàn)如下無限滾動效果
當(dāng)頁面滾動會無限加載數(shù)據(jù)展示到頁面。當(dāng)鼠標(biāo)放置上去會變色。
提示:當(dāng)?shù)撞考虞d更多按鈕出現(xiàn)時,通過 ajax 發(fā)送請求獲取數(shù)據(jù),append 到容器里。事件綁定使用代理方式。
效果預(yù)覽
ps:自己實現(xiàn)效果后,可參考 demo 里的注釋
HTML代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>29-3加載更多</title>
<style>
ul,li{
list-style: none;
color: #000;
margin: 0;
padding: 0;
}
li{
border: 1px solid #ddd;
line-height: 50px;
height: 50px;
margin-bottom: 10px;
cursor: pointer;
}
.hover{
background-color: green;
color: #fff;
}
a{
text-decoration: none;
color: #E27272;
display: block;
text-align: center;
height: 40px;
line-height: 40px;
width: 100px;
border: 1px solid #E27272;
border-radius: 2px;
margin: 0 auto;
}
</style>
<script src="http://cdn.bootcss.com/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<ul id="ct">
<li>內(nèi)容1</li>
<li>內(nèi)容2</li>
<li>內(nèi)容3</li>
<li>內(nèi)容4</li>
<li>內(nèi)容5</li>
<li>內(nèi)容6</li>
<li>內(nèi)容7</li>
<li>內(nèi)容8</li>
<li>內(nèi)容9</li>
<li>內(nèi)容10</li>
<li>內(nèi)容11</li>
</ul>
<a id="more" href="####">加載更多</a>
<script>
var cur=11;
var clock;
lodeMore();
$(window).on("scroll",function () {
if(clock) clearTimeout(clock);
clock = setTimeout(function(){
if(!isVisible($("a#more"))) return;
lodeMore();
}, 100);
});
function onSuccess(json){
cur =cur+6;
append(json.data);
}
function append(items){
for (var i=0;i<items.length;i++){
$("#ct").append("<li>"+items[i]+"</li>");
}
}
function isVisible($node){
var winH = $(window).height(), //窗口高度
offsetH = $node.offset().top, //元素到根節(jié)點頂部距離
scrollH = $(window).scrollTop(); //滾動的垂直距離
if( (offsetH > scrollH) && (offsetH < scrollH + winH ) ){
return true;
}
return false;
}
function lodeMore() {
$.ajax({
url:"renwu29_3.php",
type:"get",
dataType:"json",
data: {
start: cur,
len: 6
},
success: function(json){
onSuccess (json)
},
error: function(){
console.log('出錯了')
}
})
}
$("#ct").on("mouseenter","li",function (e) {
$(this).addClass("hover");
});
$("#ct").on("mouseleave","li",function () {
$(this).removeClass("hover");
})
</script>
</body>
</html>
php代碼:
<?php
$start = $_GET['start'];
$len = $_GET['len'];
$items = array();
for($i = 0; $i < $len; $i++){
array_push($items, '內(nèi)容' . ($start+$i+1));
}
$ret = array('status'=>1, 'data'=>$items);
echo json_encode($ret);
?>
HTML代碼預(yù)覽地址:https://github.com/have-not-BUG/task/blob/master/renwu/renwu29/3/renwu29-3.html
本地測試成功:

本地測試成功
**本文版權(quán)歸本人即簡書筆名:該賬戶已被查封 所有,如需轉(zhuǎn)載請注明出處。謝謝! *