如果使用 JavaScript 頁面底部可見,如何傳回 true?
在本教學中,我們將檢查頁面的底部是否對使用者可見。我們可以透過使用視窗的高度和滾動視窗的高度來實現此功能。要編寫此程式碼,我們需要了解 JavaScript 的三種方法,如下所示:
scrollY - 它是視窗的唯讀屬性,並傳回文件具有的像素垂直捲動。
window.scrollY
scrollHeight -它是一個 HTML DOM 元素,也是視窗的唯讀屬性。它會傳回元素內容的高度,包括不可見的內容。
element.scrollHeight
clientHeight - 它也是唯讀屬性,傳回元素的可視高度(以像素為單位),包括填充,但不包括邊框、捲軸或邊距。
element.clientHeight window.clientHeight
注意 - 上述三種方法皆以像素為單位測量元素的值。
語法
以下是要檢查的條件的語法如果頁面底部可見。
document.documentElement.clientHeight + window.scrollY >=(document.documentElement.scrollHeight ||document.documentElement.clientHeight);
如果上述條件成立,頁面底部將可見。
方法
我們檢查 clientHeight 和 < em>scrollY 大於或等於 scrollHeight 或 clientHeight。如果此條件為真,則頁面底部將可見。因此,我們定義一個函數,如果滿足條件,則傳回 true。
範例
使用documentElement 的clientHeight 屬性>
在下面的程式中,我們檢查頁面底部是否可見。我們使用 documentElement 的 clientHeight 屬性檢查語法部分中定義的條件。
<!DOCTYPE html> <html> <head> <title>Example - Bottom Visible JavaScript</title> </head> <body> <div style="margin-bottom:100px;"> <h3>Checking if bottom of page is visible</h3> <p id = "bottom"> Is bottom of the Page visible?<br></p> </div> <div> You reached to the bottom of the page.</div> <script> const bottomVisible = () => document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight); console.log(bottomVisible()); document.getElementById("bottom").innerHTML += bottomVisible() </script> </body> </html>
在上面的程式碼中,我們比較兩個值,一個是客戶端高度和scrollY之和,另一個是滾動高度和客戶端高度的或運算。當客戶端高度與scrollY總和大於或等於滾動高度與客戶端高度的或運算時,結果值為true,表示頁面底部可見。
範例
使用window介面的clientHeight屬性
在下面的程式中,我們檢查是否頁面底部可見或不可見。我們使用 window 介面的 clientHeight 屬性來檢查語法部分中定義的條件。
<!DOCTYPE html> <html> <head> <title>Example - Bottom Visible JavaScript</title> </head> <body> <div style="margin-bottom:100px;"> <h3>Checking if bottom of page is visible</h3> <p id = "bottom"> Is bottom of the Page visible?<br></p> </div> <div> You reached to the bottom of the page.</div> <script> const bottomVisible = () => window.innerHeight + window.scrollY >=(document.documentElement.scrollHeight || window.innerHeight); console.log(bottomVisible()); document.getElementById("bottom").innerHTML += bottomVisible() </script> </body> </html>
範例
頁面底部不可見
#在下面的程式中,我們將div 的下邊距設定得很高,以便頁面底部不可見。
<html> <head> <title>Example - Bottom Visible JavaScript</title> </head> <body> <div style="margin-bottom:2500px;"> <h3>The bottom of page not visible</h3> <p id = "bottom"> Is bottom of the Page visible?<br></p> <p id> <br> Please scroll down to reach the bottom...</p> </div> <div> You reached to the bottom of the page.</div> <script> const bottomVisible = () => window.clientHeight + window.scrollY >=(document.documentElement.scrollHeight || window.clientHeight); console.log(bottomVisible()); document.getElementById("bottom").innerHTML += bottomVisible() </script> </body> </html>
以上是如果使用 JavaScript 頁面底部可見,如何傳回 true?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

JavaScript是現代Web開發的基石,它的主要功能包括事件驅動編程、動態內容生成和異步編程。 1)事件驅動編程允許網頁根據用戶操作動態變化。 2)動態內容生成使得頁面內容可以根據條件調整。 3)異步編程確保用戶界面不被阻塞。 JavaScript廣泛應用於網頁交互、單頁面應用和服務器端開發,極大地提升了用戶體驗和跨平台開發的靈活性。

Python和JavaScript開發者的薪資沒有絕對的高低,具體取決於技能和行業需求。 1.Python在數據科學和機器學習領域可能薪資更高。 2.JavaScript在前端和全棧開發中需求大,薪資也可觀。 3.影響因素包括經驗、地理位置、公司規模和特定技能。

如何在JavaScript中將具有相同ID的數組元素合併到一個對像中?在處理數據時,我們常常會遇到需要將具有相同ID�...

學習JavaScript不難,但有挑戰。 1)理解基礎概念如變量、數據類型、函數等。 2)掌握異步編程,通過事件循環實現。 3)使用DOM操作和Promise處理異步請求。 4)避免常見錯誤,使用調試技巧。 5)優化性能,遵循最佳實踐。

實現視差滾動和元素動畫效果的探討本文將探討如何實現類似資生堂官網(https://www.shiseido.co.jp/sb/wonderland/)中�...

JavaScript的最新趨勢包括TypeScript的崛起、現代框架和庫的流行以及WebAssembly的應用。未來前景涵蓋更強大的類型系統、服務器端JavaScript的發展、人工智能和機器學習的擴展以及物聯網和邊緣計算的潛力。

深入探討console.log輸出差異的根源本文將分析一段代碼中console.log函數輸出結果的差異,並解釋其背後的原因。 �...
