JS 中屏幕、瀏覽器和文檔的高度、寬度和距離


1、各種對象

window.screen - 屏幕,
window - 窗口,
document.documentElement & document.body.parentNode - 文檔,
document.body - 文檔的主體。

 

2、各種屬性(單位為px)

屏幕

window.screen.availHeight 屏幕可用高度;
window.screen.availWidth 屏幕可用寬度;
window.screen.height 屏幕總高度 = availHeight + 下方任務欄;
window.screen.width 屏幕總寬度 = availWidth + 右方任務欄(如果存在)。

窗口(瀏覽器)

window.screenLeft & window.screenX 瀏覽器左邊框到屏幕左側的水平距離;
window.screenTop & window.screenY 瀏覽器上邊框到屏幕上側的垂直距離;
window.outerHeight / window.outerWidth 窗口外部大小,即整個瀏覽器的大小;
window.innerHeight / window.innerWidth 窗口內部大小,即視口viwport的大小,包括水平/垂直滾動條;
window.onresize 事件是在 window.innerHeight / window.innerWidth 改變時觸發的;

window.pageXOffset & window.scrollX 文檔當前在水平方向上被卷掉的像素數;
window.pageYOffset & window.scrollY 文檔當前在垂直方向上被卷掉的像素數。

元素

document.documentElement, document.body.parentNode 和 document.body 這三個元素節點都繼承了element對象的多個只讀的和可寫的高度和寬度屬性(“=” 右邊加‘CSS’的屬性為CSS中的屬性):

element.clientHeight = CSS height + CSS padding - height of horizontal scrollbar (if present) 不包括邊框,邊距和水平滾動條;
element.clientWidth = CSS width + CSS padding - width of vertical scrollbar (if present) 不包括邊框,邊距和垂直滾動條;
element.clientTop = CSS border-top-width;
element.clientLeft = CSS border-left-width 如果文本方向被設為rtl ,而且左邊有垂直滾動條,那么clientLeft包含滾動條寬度,例如:

在CSS中設置文本方向為ltr默認情況:

#rtl{
    width: 100px;
    height: 100px;
    border: 1px solid;
    direction: ltr;
    overflow: auto;
}

在CSS中設置文本方向為 rtl :

#rtl{
    width: 100px;
    height: 100px;
    border: 1px solid;
    direction: rtl;
    overflow: auto;
}

在HTML中設置文本方向:

<div id='rtl' dir="rtl">this content will have a constant aspect ratio that varies based on the width.this content will have a constant aspect ratio that varies based on the width.</div>

可以看到不管是通過CSS還是HTML設置文本方向為rtl,而且同時存在溢出形成滾動條,那么 clientLeft 都會加上滾動條寬度;

 

element.scrollHeight = clientHeight + 溢出不可見的內容 + 偽元素的高度;

MDN譯文:

Element.scrollHeight只讀屬性是元素內容的高度的度量,包括由於溢出而在屏幕上不可見的內容。

scrollHeight值等於元素所需的最小高度,以便在不使用垂直滾動條的情況下適合視口中的所有內容。高度的測量方式與clientHeight相同:它包含元素的填充,但不包括元素的邊框、邊距或水平滾動條(如果存在)。它還可以包括偽元素的高度,例如 ::before 和 ::after如果元素的內容不需要垂直滾動條就能填滿,則其滾動高度等於clientHeight

element.scrollWidth = clientWidth + 溢出不可見的內容 + 偽元素的寬度;

element.scrollTop 獲取或設置元素內容向上滾動的像素數;

element.scrollLeft 獲取或設置元素內容向左滾動的像素數;

 

element.offsetHeight = CSS height+ CSS padding + CSS border + 水平滾動條高度(如果存在),不包括偽元素的高度;

MDN:

For the document body object, the measurement includes total linear content height instead of the element's CSS height. Floated elements extending below other linear content are ignored.

element.offsetWidth = CSS width + CSS padding + CSS border + 垂直滾動條寬度(如果存在),不包括偽元素的寬度;

element.offsetTop 只讀屬性,返回當前元素的左上角相對於offsetParent 節點頂部的距離(可理解為CSS margin-top-width);

element.offsetLeft 只讀屬性,返回當前元素的左上角相對於offsetParent 節點左側的距離(可理解為CSS margin-left-width)。

 

3、一些兼容寫法

文檔當前在水平方向上被卷掉的像素數,文檔當前在垂直方向上被卷掉的像素數。

var y = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop,
    x = (window.pageXOffset !== undefined) ? window.pageXOffset : (document.documentElement || document.body.parentNode || document.body).scrollLeft;

 

4、應用

The following equivalence returns true if an element is at the end of its scroll, false if it isn't.

element.scrollHeight - element.scrollTop === element.clientHeight

我們可以使用這個等式來判斷元素是否滾動到底部。接下來我們嘗試實現一個簡單的底部進度條:

html:

<!DOCTYPE html> 
<html> 
<head>
    <meta charset="utf-8" />
    <title>頁面底部進度條</title> 
    <link type="text/css" rel='stylesheet' href="test.css"></link>
</head> 
<body> 
    <footer>
        <div id="proress"></div>
    </footer>
    <script src="test.js"></script>
</body> 
</html>

css:

body{
    margin: 0;
    height: 2000px;
    width: 2000px;
}
footer{
    position: fixed;
    width: 100%;
    height: 20px;
    margin: 0;
    left: 0;
    bottom: 0;
    border: 1px solid;
}
#proress{
    background-color: blue;
    width: 0;
    height: 20px;
}

javaScript:

window.onload = window.onresize = window.onscroll = function(){
    var yScr = (window.pageYOffset !== undefined) ? window.pageYOffset : (document.documentElement || document.body.parentNode || document.body).scrollTop,
        yLkd = yScr + document.documentElement.clientHeight,
        num = yLkd / document.documentElement.scrollHeight * document.documentElement.clientWidth;
    document.getElementById('proress').style.width = num + 'px';
    /*var data = [{'1': yLkd, '2': document.documentElement.scrollHeight,'3': document.documentElement.clientWidth},
    {'1': document.documentElement.clientHeight, '2': document.documentElement.clientWidth},
    {'1': document.documentElement.scrollHeight, '2': document.documentElement.scrollWidth},
    {'1': document.documentElement.offsetHeight, '2': document.documentElement.offsetWidth}];
    console.table(data);
    */
};

(以上代碼只在Chrome瀏覽器測試過)

注意各個屬性代表的具體含義,認識它們的區別,然后正確地使用它們,不然最后的結果很可能會出現幾十甚至幾百像素的誤差。

效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM