溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

滾動(dòng)scroll如何理解

發(fā)布時(shí)間:2022-01-10 09:50:25 來(lái)源:億速云 閱讀:149 作者:柒染 欄目:網(wǎng)絡(luò)安全

這篇文章給大家介紹滾動(dòng)scroll如何理解,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

滾動(dòng)寬高

scrollHeight

  scrollHeight表示元素的總高度,包括由于溢出而無(wú)法展示在網(wǎng)頁(yè)的不可見(jiàn)部分

scrollWidth

  scrollWidth表示元素的總寬度,包括由于溢出而無(wú)法展示在網(wǎng)頁(yè)的不可見(jiàn)部分

  [注意]IE7-瀏覽器返回值是不準(zhǔn)確的

  【1】沒(méi)有滾動(dòng)條時(shí),scrollHeight與clientHeight屬性結(jié)果相等,scrollWidth與clientWidth屬性結(jié)果相等

<div id="test" ></div><script>//120 120console.log(test.scrollHeight,test.scrollWidth);//120 120console.log(test.clientHeight,test.clientWidth);</script>

  【2】存在滾動(dòng)條時(shí),但元素設(shè)置寬高大于等于元素內(nèi)容寬高時(shí),scroll和client屬性的結(jié)果相等

<div id="test" >
    內(nèi)容<br>內(nèi)容<br></div><script>//103(120-17) 103(120-17)console.log(test.scrollHeight,test.scrollWidth);//103(120-17) 103(120-17)console.log(test.clientHeight,test.clientWidth);</script>

  【3】存在滾動(dòng)條,但元素設(shè)置寬高小于元素內(nèi)容寬高,即存在內(nèi)容溢出的情況時(shí),scroll屬性大于client屬性

  [注意]scrollHeight屬性存在兼容性問(wèn)題,chrome和safari瀏覽器中,scrollHeight包含padding-bottom;而IE和firefox不包含padding-bottom

<div id="test" >
    內(nèi)容</div><script>//chrome/safari:220(200+10+10)//firefox/IE:210(200+10)console.log(test.scrollHeight);//103(120-17)console.log(test.clientHeight);</script>

頁(yè)面尺寸

  document.documentElement.clientHeight表示頁(yè)面的可視區(qū)域的尺寸,而document.documentElement.scrollHeight表示html元素內(nèi)容的實(shí)際尺寸。但是由于各個(gè)瀏覽器表現(xiàn)不一樣,分為以下幾種情況

  【1】html元素沒(méi)有滾動(dòng)條時(shí),IE和firefox的client和scroll屬性始終相同,且返回可視區(qū)的尺寸大小;而safari和chrome表現(xiàn)正常,clientHeight返回可視區(qū)域大小,而scrollHeight返回元素內(nèi)容大小

//firefox:  755 755//chrome:   947 8(body元素的margin)//safari:   744 8(body元素的margin)//IE:       768 768console.log(document.documentElement.clientHeight,document.documentElement.scrollHeight)

  【2】html元素存在滾動(dòng)條時(shí),各個(gè)瀏覽器都表現(xiàn)正常。clientHeight返回可視區(qū)域大小,而scrollHeight返回元素內(nèi)容大小

<body ><script>//firefox:  755 1016(1000+8*2)//chrome:   947 1016(1000+8*2)//safari:   744 1016(1000+8*2)//IE:       768 1016(1000+8*2)console.log(document.documentElement.clientHeight,document.documentElement.scrollHeight)</script>

兼容

  因此要取得文檔實(shí)際高度時(shí),要取得<html>元素的scrollHeight和clientHeight的最大值

var docHeight = Math.max(document.documentElement.scrollHeight,document.documentElement.clientHeight);var docWidth  = Math.max(document.documentElement.scrollWidth,document.documentElement.clientWidth);

滾動(dòng)長(zhǎng)度

scrollTop

  scrollTop屬性表示被隱藏在內(nèi)容區(qū)域上方的像素?cái)?shù)。元素未滾動(dòng)時(shí),scrollTop的值為0,如果元素被垂直滾動(dòng)了,scrollTop的值大于0,且表示元素上方不可見(jiàn)內(nèi)容的像素寬度

scrollLeft

  scrollLeft屬性表示被隱藏在內(nèi)容區(qū)域左側(cè)的像素?cái)?shù)。元素未滾動(dòng)時(shí),scrollLeft的值為0,如果元素被水平滾動(dòng)了,scrollLeft的值大于0,且表示元素左側(cè)不可見(jiàn)內(nèi)容的像素寬度

  當(dāng)滾動(dòng)條滾動(dòng)到內(nèi)容底部時(shí),符合以下等式

scrollHeight == scrollTop  + clientHeight
<div id="test" >
    內(nèi)容</div><button id='btn1'>點(diǎn)擊</button><div id="result"></div><script>btn1.onclick = function(){
    result.innerHTML = 'scrollTop:' + test.scrollTop+';clientHeight:' + test.clientHeight + ';scrollHeight:' + test.scrollHeight
}</script>

  與scrollHeight和scrollWidth屬性不同的是,scrollLeft和scrollTop是可寫(xiě)的

  [注意]為scrollLeft和scrollTop賦值為負(fù)值時(shí),并不會(huì)報(bào)錯(cuò),而是靜默失敗

<div id="test" >
    內(nèi)容</div><button id='btn1'>向下滾動(dòng)</button><button id='btn2'>向上滾動(dòng)</button><script>btn1.onclick = function(){test.scrollTop += 10;}
btn2.onclick = function(){test.scrollTop -= 10;}</script>

頁(yè)面滾動(dòng)

  理論上,通過(guò)document.documentElement.scrollTop和scrollLeft可以反映和控制頁(yè)面的滾動(dòng);但是chrome和safari瀏覽器是通過(guò)document.body.scrollTop和scrollLeft來(lái)控制的

<body ><button id='btn1' >點(diǎn)擊</button><div id="result" ></div><script>btn1.onclick = function(){
    result.innerHTML = 'html的scrollTop:' + document.documentElement.scrollTop +';body的scrollTop:' + document.body.scrollTop;
}</script>    </body>

  所以,頁(yè)面的滾動(dòng)高度兼容寫(xiě)法是

var docScrollTop = document.documentElement.scrollTop || document.body.scrollTop

回到頂部

  可以利用scrollTop來(lái)實(shí)現(xiàn)回到頂部的功能

function scrollTop(){    if((document.body.scrollTop || document.documentElement.scrollTop) != 0){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    }
}
<body >
<button id='btn' >回到頂部</button>
<script>function scrollTop(){    if((document.body.scrollTop || document.documentElement.scrollTop) != 0){
        document.body.scrollTop = document.documentElement.scrollTop = 0;
    }
}
btn.onclick = scrollTop;</script>
</body>

  還有兩個(gè)window的只讀屬性可以獲取整個(gè)頁(yè)面滾動(dòng)的像素值,它們是pageXOffset和pageYOffset

pageXOffset

  pageXOffset表示水平方向上頁(yè)面滾動(dòng)的像素值

pageYOffset

  pageYOffset表示垂直方向上頁(yè)面滾動(dòng)的像素值

  [注意]IE8-瀏覽器不支持

<body ><button id='btn1' >點(diǎn)擊</button><div id="result" ></div><script>btn1.onclick = function(){
    result.innerHTML = 'pageYOffset:' + window.pageYOffset;
}</script>    </body>

滾動(dòng)方法

scrollTo(x,y)

  scrollTo(x,y)方法滾動(dòng)當(dāng)前window中顯示的文檔,讓文檔中由坐標(biāo)x和y指定的點(diǎn)位于顯示區(qū)域的左上角

<body ><button id='btn' >滾動(dòng)</button><script>btn.onclick = function(){scrollTo(0,0);}</script>

scrollBy(x,y)

  scrollBy(x,y)方法滾動(dòng)當(dāng)前window中顯示的文檔,x和y指定滾動(dòng)的相對(duì)量

<body ><button id='btn1' >向下滾動(dòng)</button><button id='btn2' >向上滾動(dòng)</button><script>btn1.onclick = function(){scrollBy(0,100);}
btn2.onclick = function(){scrollBy(0,-100);}</script>

【小應(yīng)用】

  利用scrollBy()加setInterval計(jì)時(shí)器實(shí)現(xiàn)簡(jiǎn)單的快速滾動(dòng)功能

<body ><button id='btn1' >開(kāi)始滾動(dòng)</button><button id='btn2' >停止?jié)L動(dòng)</button><script>var timer = 0;
btn1.onclick = function(){
    timer = setInterval(function(){
        scrollBy(0,10);
    },100)}
btn2.onclick = function(){
    clearInterval(timer);
    timer = 0;
}</script>

scrollIntoView()

  Element.scrollIntoView方法滾動(dòng)當(dāng)前元素,進(jìn)入瀏覽器的可見(jiàn)區(qū)域

  該方法可以接受一個(gè)布爾值作為參數(shù)。如果為true,表示元素的頂部與當(dāng)前區(qū)域的可見(jiàn)部分的頂部對(duì)齊(前提是當(dāng)前區(qū)域可滾動(dòng));如果為false,表示元素的底部與當(dāng)前區(qū)域的可見(jiàn)部分的尾部對(duì)齊(前提是當(dāng)前區(qū)域可滾動(dòng))。如果沒(méi)有提供該參數(shù),默認(rèn)為true

<body ><div id="test" ></div><button id='btn1' >滾動(dòng)到頁(yè)面開(kāi)頭</button><button id='btn2' >滾動(dòng)到頁(yè)面結(jié)尾</button><script>btn1.onclick = function(){
    test.scrollIntoView();
};
btn2.onclick = function(){
    test.scrollIntoView(false);
}</script>

scrollIntoViewIfNeeded()

  scrollIntoViewIfNeeded(true)方法只在當(dāng)前元素在視口中不可見(jiàn)的情況下,才滾動(dòng)瀏覽器窗口或容器元素,最終讓它可見(jiàn)。如果當(dāng)前元素在視口中可見(jiàn),這個(gè)方法什么也不做 

  如果將可選的alignCenter參數(shù)設(shè)置為true,則表示盡量將元素顯示在視口中部(垂直方向)

  [注意]該方法只有chrome和safari支持

<body ><div id="test" ></div><button id='btn' >滾動(dòng)到頁(yè)面中間</button><script>btn.onclick = function(){
    test.scrollIntoViewIfNeeded(true)
};</script>

scrollByLines(lineCount)

  scrollByLines(lineCount)方法將元素的內(nèi)容滾動(dòng)指定的行髙,lineCount值可以是正值, 也可以是負(fù)值

  [注意]該方法只有safari支持

<div id="test" >
    內(nèi)容</div><button id='btn1'>向下滾動(dòng)</button><button id='btn2'>向上滾動(dòng)</button><script>btn1.onclick = function(){test.scrollByLines(1);}
btn2.onclick = function(){test.scrollByLines(-1);}</script>

scrollByPages(pageCount)

  scrollByPages(pageCount)方法將元素的內(nèi)容滾動(dòng)指定的頁(yè)面高度,具體高度由元素的高度決定

  [注意]該方法只有safari支持

<div id="test" >
    內(nèi)容</div><button id='btn1'>向下滾動(dòng)</button><button id='btn2'>向上滾動(dòng)</button><script>btn1.onclick = function(){test.scrollByPages(1);}
btn2.onclick = function(){test.scrollByPages(-1);}</script>

滾動(dòng)事件

  scroll事件是在window對(duì)象上發(fā)生的,它表示的是頁(yè)面中相應(yīng)元素的變化。當(dāng)然,scroll事件也可以用在有滾動(dòng)條的元素上

<body ><div id="result" ></div><script>window.onscroll = function(){
    result.innerHTML = '頁(yè)面的scrollTop:' + (document.documentElement.scrollTop||document.body.scrollTop);
}</script>    </body>

關(guān)于滾動(dòng)scroll如何理解就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI