詳解JavaScript編程中的window與window.screen對象
Window 對象
所有瀏覽器都支持 window 對象。它表示瀏覽器窗口。
所有 JavaScript 全局對象、函數(shù)以及變量均自動成為 window 對象的成員。
全局變量是 window 對象的屬性。
全局函數(shù)是 window 對象的方法。
甚至 HTML DOM 的 document 也是 window 對象的屬性之一:
window.document.getElementById("header");
與此相同:
document.getElementById("header");
Window 尺寸
有三種方法能夠確定瀏覽器窗口的尺寸(瀏覽器的視口,不包括工具欄和滾動條)。
對于Internet Explorer、Chrome、Firefox、Opera 以及 Safari:
- window.innerHeight - 瀏覽器窗口的內部高度
- window.innerWidth - 瀏覽器窗口的內部寬度
對于 Internet Explorer 8、7、6、5:
- document.documentElement.clientHeight
- document.documentElement.clientWidth
或者
- document.body.clientHeight
- document.body.clientWidth
實用的 JavaScript 方案(涵蓋所有瀏覽器):
實例
var w=window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth; var h=window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
該例顯示瀏覽器窗口的高度和寬度:(不包括工具欄/滾動條)
Window Screen
window.screen對象在編寫時可以不使用 window 這個前綴。
一些屬性:
- screen.availWidth - 可用的屏幕寬度
- screen.availHeight - 可用的屏幕高度
- Window Screen 可用寬度
- screen.availWidth 屬性返回訪問者屏幕的寬度,以像素計,減去界面特性,比如窗口任務欄。
實例
返回您的屏幕的可用寬度:
<script> document.write("Available Width: " + screen.availWidth); </script>
以上代碼輸出為:
Available Width: 1440
Window Screen 可用高度
screen.availHeight 屬性返回訪問者屏幕的高度,以像素計,減去界面特性,比如窗口任務欄。
實例
返回您的屏幕的可用高度:
<script> document.write("Available Height: " + screen.availHeight); </script>
以上代碼將輸出:
Available Height: 860
相關文章
python 實現(xiàn)登錄網(wǎng)頁的操作方法
今天小編就為大家分享一篇python 實現(xiàn)登錄網(wǎng)頁的操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05Jupyter Notebook調用指定的虛擬環(huán)境的實現(xiàn)示例
本文主要介紹了Jupyter Notebook調用指定的虛擬環(huán)境的實現(xiàn)示例,,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07Python?Setuptools的?setup.py實例詳解
setup.py是一個?python?文件,它的存在表明您要安裝的模塊/包可能已經(jīng)用?Setuptools?打包和分發(fā),這是分發(fā)?Python?模塊的標準。?它的目的是正確安裝軟件,本文給大家講解Python?Setuptools的?setup.py感興趣的朋友跟隨小編一起看看吧2022-12-12