javascript

網頁

[PRACTICE] 取得全網頁高度

網頁包含三個 div。

div 的 CSS 設定:

box-sizing: border-box;

width: 300px;
height: 500px;

margin: 20px auto;
padding: 10px;

border: 5px solid #eee;

每個 div 的高度為:border + padding + 內容 = 500px

第一個 div 跟第二個 div 之間的 margin + 第二個 div 跟第三個 div 之間的 margin(div 之間的 margin 會重疊):20px + 20px

再加上第一個 div 的 margin(top) 跟最後一個 div 的 margin(bottom):20px + 20px

全網頁高度為:20px + 500px + 20px + 500px + 20px + 500px + 20px = 1580px

JS 的部份,document.documentElement 指的是 html 元素。

Demo
https://www.yuantw.com/demo/js-offset-height/

網頁

[PRACTICE] Infinity Scroll

跟著 Udemy 的課程 JavaScript Web Projects: 20 Projects to Build Your Portfolio 做練習。

這個練習是從 Unsplash 的 API 取得圖片,當捲軸快滑到底部時,再次從 API 取得圖片。

在 JS 裡有監聽 scroll 事件。

scroll 事件會連續觸發,導致連續跟 Unsplash API 要圖檔,為了避免這個狀況,可以新增一個變數(imgLoadedStatus)當作開關,值為布林值。

Demo
https://www.yuantw.com/demo/infinity-scroll/

返回頂端