Q&A:
1. 如何判斷一個元素是否出現在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。寫一個函數 isVisible實現
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>task29</title>
</head>
<body>
<p style="height: 100px">內容1</p>
<p style="height: 100px">內容1</p>
<p style="height: 100px">內容1</p>
<p style="height: 100px">內容1</p>
<p style="height: 100px">內容1</p>
<p style="height: 100px">內容1</p>
<p style="height: 100px">內容1</p>
<p style="height: 100px">內容1</p>
<div class="test">hello</div>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$.fn.isVisible = function() {
var $cur = $(this),
offsetTop = $cur.offset().top,
$winH = $(window).height(),
scrollTop = $(window).scrollTop(),
scrollBottom = $(window).scrollTop() + $winH;
if(offsetTop > scrollTop && scrollBottom > offsetTop) {
console.log(true);
return true;
} else {
console.log(false);
return false;
}
}
$('.test').isVisible();
</script>
</body>
</html>
isVisible
2. 當窗口滾動時,判斷一個元素是不是出現在窗口可視范圍。每次出現都在控制臺打印 true。用代碼實現
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>task29</title>
<style type="text/css">
html, body, div {
margin: 0;
padding: 0;
}
.test {
margin: 30px auto;
width: 200px;
height: 200px;
line-height: 200px;
text-align: center;
border: 3px solid;
background-color: rgba(180, 255, 0, 0.5);
}
</style>
</head>
<body>
<div class="test test1">test1</div>
<div class="test test2">test2</div>
<div class="test test3">test3</div>
<div class="test test4">test4</div>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$.fn.isVisible = function() {
var $self = $(this);
$(window).on('scroll', function() {
var $me = $(this);
offsetTop = $self.offset().top,
$winH = $me.height(),
scrollTop = $me.scrollTop(),
scrollBottom = $me.scrollTop() + $winH;
if(offsetTop > scrollTop && scrollBottom > offsetTop) {
console.log(true);
} else {
console.log(false);
}
});
}
$('.test4').each(function() {
$(this).isVisible();
});
</script>
</body>
</html>
3. 當窗口滾動時,判斷一個元素是不是出現在窗口可視范圍。在元素第一次出現時在控制臺打印 true,以后再次出現不做任何處理。用代碼實現
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>task29-demo3</title>
<style type="text/css">
html, body, div {
margin: 0;
padding: 0;
}
.test {
margin: 30px auto;
width: 200px;
height: 200px;
line-height: 200px;
text-align: center;
border: 3px solid;
background-color: rgba(180, 255, 0, 0.5);
}
</style>
</head>
<body>
<div class="test test1">test1</div>
<div class="test test2">test2</div>
<div class="test test3">test3</div>
<div class="test test4">test4</div>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
$.fn.isVisible = function() {
var $self = $(this);
$(window).on('scroll', function() {
var $me = $(this);
offsetTop = $self.offset().top,
$winH = $me.height(),
scrollTop = $me.scrollTop(),
scrollBottom = $me.scrollTop() + $winH;
if(!$self.data('data-visible')) {
if(offsetTop > scrollTop && scrollBottom > offsetTop) {
console.log(true);
$self.data('data-visible', true);
} else {
return;
}
}
});
}
$('.test4').each(function() {
$(this).isVisible();
});
</script>
</body>
</html>
4. 圖片懶加載的原理是什么?
圖片懶加載也稱為曝光加載,將頁面上的圖片分批加載,只有當圖片出現在window窗口中(用戶可見)的時候,才加載圖片;而正常情況下,img元素是自動加載的,所以可以自定義一個圖片地址屬性,當需要加載圖片的時候,將該自定義屬性值賦給src屬性,以實現優化頁面的渲染速度,圖片延遲加載。
coding:
本文歸饑人谷和本人所有,如需轉載請注明來源,謝謝