1、如何判斷一個元素是否出現在窗口可視范圍(瀏覽器的上邊緣和下邊緣之間,肉眼可視)。
先理解兩個概念
- 元素頂部“露出”頁面的時機是:元素距頂部高度 = 窗口滾動高度+窗口高度
- 元素尾部“消失”頁面的時機是:元素距頂部高度+元素高度 = 窗口滾動高度
然后掌握獲取這些高度的方法
- 獲取元素高度:
(1)element.style.height
:高度寫在內聯樣式上(不推薦),單位:px
<div id="div1" style="height:150px"></div>
console.log(div1.style.height)
//"150px"
(2)getComputedStyle(element).height
:計算后的樣式(推薦),單位:px
#div1 {
width: 100px;
height: 150px;
background: green;
margin-top: 1000px;
}
<div id="div1"></div>
console.log(getComputedStyle(div1).height)
//"150px"
(3)$(selector).css(propertyName):單位px或$(selector).propertyName():單位,數值
注意后者只局限于讀寫寬高等
#div1 {
width: 100px;
height: 150px;
background: green;
margin-top: 1000px;
}
<div id="div1"></div>
console.log($('#div1').height())
//150px
console.log($('#div1').css('height'))
//"150px"
- 獲取元素距頂部高度:分別有原生JS和JQuery方法,注意單位
這里要引入一個概念,offsetParent:
offsetParent并不是元素的父元素,而是已經定位的父元素(設置position)。
大多數情況下,元素都沒有offsetParent,所以offsetTop等值都是相對與窗口的。
一旦元素有了offsetParent,便是相對于offsetParent,并且可以有多個。測試中,元素會忽略offsetParent的邊框寬度,造成誤差。
(1)大多數情況元素沒有offsetParent
,我們直接獲取元素距頂部高度:
#div1 {
width: 100px;
height: 150px;
background: green;
margin-top: 1000px;
}
<div id="div1"></div>
console.log($('#div1').offset().top)
//1000
console.log(div1.offsetTop)
//1000
(2)如果考慮offsetParent
,我們可以這樣:
#div1 {
border: 1px solid;
width: 100px;
height: 150px;
background: green;
}
#ct {
top:100px;
position: absolute;
border: 100px solid;
padding: 100px;
}
<div id="ct">
<div id="div1"></div>
</div>
function getOffsetTop( elem ){
var offsetTop = -parseInt(getComputedStyle(elem).borderTopWidth)
do {
offsetTop += elem.offsetTop
+parseInt(getComputedStyle(elem).borderTopWidth)
} while( elem = elem.offsetParent )
console.log(offsetTop)
}
getOffsetTop(div1)
//輸出300,邊框也計算在內了
- 獲取窗口滾動高度:原生JS、JQuery都可以
console.log(document.body.scrollTop)
//359
console.log($('body').scrollTop())
//359
console.log($(window).scrollTop())
//359
- 獲取窗口的高度比較簡單:
console.log(window.innerHeight)
//601
console.log($(window).innerHeight())
//601
知道了上邊這些,判斷元素是否出現在窗口可視范圍就簡單了。
function isVisible($node){
if($node.offset().top<window.innerHeight+$('body').scrollTop()
&&$node.offset().top+$node.height()>$('body').scrollTop()){
console.log($node+'在可視區域')
}else{
console.log($node+'不在可視區域')
}
}
2、當窗口滾動時,判斷一個元素是不是出現在窗口可視范圍。每次出現都在控制臺打印 true 。用代碼實現
function isVisible($node){
if($node.offset().top<window.innerHeight+$('body').scrollTop()
&&$node.offset().top+$node.height()>$('body').scrollTop()){
console.log(true)
}
}
$(window).scroll(function(){
isVisible($('#div1'))
})
預覽地址:http://js.jirengu.com/finef/8/edit
3、當窗口滾動時,判斷一個元素是不是出現在窗口可視范圍。在元素第一次出現時在控制臺打印 true,以后再次出現不做任何處理。用代碼實現
思路:當第一次滿足可視條件時給元素做一個標記,并輸出true;以后每次滾動作出判斷,消失時取消這個標記,已經有標記就什么也不做。
var state = false
function isVisible($node){
if($node.offset().top<window.innerHeight+$('body').scrollTop()
&&$node.offset().top+$node.height()>$('body').scrollTop()){
if(state === false){
console.log(true)
state = true
}
}else{
state = false
}
}
$(window).scroll(function(){
isVisible($('#div1'))
})
預覽地址:http://js.jirengu.com/vijub/3/edit
4、 圖片懶加載的原理是什么?
- 原理:先將img標簽中的src鏈接設為同一張圖片(空白圖片),將其真正的圖片地址存儲再img標簽的自定義屬性中(比如data-src)。當js監聽到該圖片元素進入可視窗口時,即將自定義屬性中的地址存儲到src屬性中,達到懶加載的效果。
- 這樣做能防止頁面一次性向服務器響應大量請求導致服務器響應慢,頁面卡頓或崩潰等問題。