在 JavaScript 中自動滾動到頁面底部
-
使用 JavaScript
scrollTo
函式滾動到底部 -
使用 JavaScript
scrollIntoView
函式滾動到底部 -
使用 JavaScript
scrollTop
函式滾動到底部 -
使用 JavaScript
ScrollingElement
函式滾動到底部 -
使用 JavaScript
scrollBy
函式滾動到底部
我們旨在向你介紹使用 JavaScript 自動滾動到頁面底部的不同方法。本教程還教你滾動到帶有和不帶有動畫的螢幕上的特定座標。
使用 JavaScript scrollTo
函式滾動到底部
HTML 程式碼:
<!DOCTYPE html>
<html>
<head>
<title>Scroll Automatically</title>
</head>
<body id='main_body'>
<button id="bottompage">Bottom</button>
<div id="headingone">
<h1>Heading One</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676?
b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div id="headingtwo">
<h1>Heading Two</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676?
b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676?
b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div id="headingthree">
<h1>Heading Three</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676?
b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<h1> Hi There! </h1>
</body>
</html>
CSS 程式碼:
button{
float:right;
background-color:yellow;
color:red;
}
JavaScript 程式碼:
document.querySelector('#bottompage').addEventListener('click', () => {
window.scrollTo(0, document.body.scrollHeight);
})
上面給出的程式碼選擇了 id 值為 bottompage
的標籤元素。每當單擊它時,就會觸發一個滾動到頁面底部(垂直滾動)的事件,因為 x
設定為 0
,y
設定為 document.body.scrollHeight
,即 1655px
。
scrollTo
函式用於將 document
滾動到提供的網頁座標。如果滾動條可見並且文件
與螢幕相比較大,則此方法有效。
scrollTo()
方法的兩個引數都是必需的。引數 x
以畫素為單位表示水平滾動多少,而以畫素為單位表示垂直滾動的量為 y
。
你可能正在考慮如何計算高度?為此,你可以閱讀 this 以瞭解所有高度計算。
使用 JavaScript scrollIntoView
函式滾動到底部
HTML 程式碼:
<!DOCTYPE html>
<html>
<head>
<title>Scroll Automatically</title>
</head>
<body id='main_body'>
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<a id="heading_one" href="javascript:void(0)">Heading 1</a>
<a id="heading_two" href="javascript:void(0)">Heading 2</a>
<a id="heading_three" href="javascript:void(0)">Heading 3</a>
<div id="headingone">
<h1>Heading One</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676?
b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div id="headingtwo">
<h1>Heading Two</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676?
b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
<ul>
<li>List one in heading two</li>
<li>List two in heading two</li>
<li id="list3">List three in heading two</li>
<li>List four in heading two</li>
<li>List five in heading two</li>
<li>List six in heading two</li>
<li>List seven in heading two</li>
<li>List eight in heading two</li>
</ul>
</div>
<div id="headingthree">
<h1>Heading Three</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676?
b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<h1> Hi There! </h1>
</body>
</html>
CSS 程式碼:
button{
float:right;
background-color:yellow;
color:red;
}
a{
display: block;
}
JavaScript 程式碼:
let hone = document.querySelector('#heading_one');
let listthree = document.querySelector('#list3');
hone.addEventListener('click', navigatelistthree, false);
function navigatelistthree(e) {
listthree.scrollIntoView({behavior: 'smooth'});
}
在此示例程式碼中,querySelector
獲取第一個具有 id
屬性值為 heading_one
的元素並將其儲存到 hone
變數中。之後,它選擇第一個具有 id
值的元素作為 list3
並將其儲存到名為 listthree
的變數中。
每當你單擊 Heading 1
超連結時,navigatelistthree
方法就會執行。此函式將你帶到標題 2
下的第三個列表項,寫為標題二中的列表三
。
scrollIntoView
方法可幫助你滾動檢視中的元素。它用於你希望突出顯示特定元素並希望滾動它的地方。
該方法接受一個引數;它可以是一個布林
值或一個物件
。
你可以使用 scrollIntoView()
立即跳轉到你想要的元素。或者,你可以使用 behavior
屬性平滑滾動。
檢查此連結以瞭解有關 scrollIntoView()
函式引數的更多資訊。
使用 JavaScript scrollTop
函式滾動到底部
HTML 程式碼:
<div id="boxcontent">
<div class="maincontent">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</div>
</div>
<button id="button">Scroll to Bottom</button>
CSS 程式碼:
#boxcontent {
height: 200px;
width: 300px;
overflow: scroll;
width: 300px;
}
.maincontent {
width: 100%;
height: 400px;
}
JavaScript 程式碼:
document.getElementById('button').addEventListener('click', function() {
var element = document.getElementById('boxcontent');
element.scrollTop = element.scrollHeight;
});
首先,如果你單擊滾動到底部
按鈕,則會選擇 id 值為按鈕
的元素。然後,選擇 id 屬性值為 boxcontent
的另一個元素並將其儲存到 element
變數中。
它會將你帶到內容框的末尾,該內容框是可滾動的,因為 element.scrollHeight
的值已分配給 element.scrollTop
。你可以在此處檢視更多詳細資訊以進行練習。
JavaScript 中名為 scrollTop
的屬性設定或獲取元素內容垂直滾動的畫素數。
如果 non-scrollable
屬性與元素相關聯,scrollTop
值將為零。或者元素沒有溢位。請記住,如果有負值,此屬性會將自身設定為 ZERO
。
使用 JavaScript ScrollingElement
函式滾動到底部
HTML 程式碼:
<!DOCTYPE html>
<html>
<head>
<title>Scroll Automatically</title>
</head>
<body id='main_body'>
<button id="bottompage">Bottom</button>
<div id="headingone">
<h1>Heading One</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract- technology-background-of-software-developer-picture-id1294521676? b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div id="headingtwo">
<h1>Heading Two</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676? b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div id="headingthree">
<h1>Heading Three</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676? b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<h1> Hi There! </h1>
</body>
</html>
CSS 程式碼:
button{
float:right;
background-color:yellow;
color:red;
}
JavaScript 程式碼:
document.querySelector('#bottompage').addEventListener('click', () => {
var scrollingElement = (document.scrollingElement || document.body);
scrollingElement.scrollTop = scrollingElement.scrollHeight;
})
上面給出的程式碼選擇帶有 id
作為 bottompage
的標籤元素。
當你單擊 id 值為 bottompage
的元素時,將觸發一個事件。之後,根元素(HTML
標籤)或 body
標籤(任何可用的)的引用儲存在名為 scrollingElement
的變數中。
此外,由於將 scrollingElement.scrollHeight
的值分配給 scrollingElement.scrollTop
,scrollTop
的值從 0px
更改為 1310px
。
使用 JavaScript scrollBy
函式滾動到底部
HTML 程式碼:
<!DOCTYPE html>
<html>
<head>
<title>Scroll Automatically</title>
</head>
<body id='main_body'>
<div id="headingone">
<h1>Heading One</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676? b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div id="headingtwo">
<h1>Heading Two</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676? b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div id="headingthree">
<h1>Heading Three</h1>
<img src="https://media.istockphoto.com/photos/programming-code-abstract-technology- background-of-software-developer-picture-id1294521676? b=1&k=20&m=1294521676&s=170667a&w=0&h=7pqhrZcqqbQq43Q0_TD0Y_YjInAyvA9xiht9bto030U=" alt="Programming Picture">
<p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
</body>
</html>
CSS 程式碼:
body{
background-color:lightgray;
}
h1{
color:red;
}
JavaScript 程式碼:
let scrollID;
let stopped = true;
let scrollSpeed = 2; // 1 - Fast | 2 - Medium | 3 - Slow
let scrollInterval = scrollSpeed * 3;
function startScrolling() {
let ID = setInterval(function() {
window.scrollBy(0, 2);
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight) {
// Reached end of page
stopScroll();
}
}, scrollInterval);
return ID;
}
function stopScroll() {
clearInterval(scrollID);
}
document.body.addEventListener('keypress', function(event) {
if (event.which == 13 || event.keyCode == 13) {
// It's the 'Enter' key
if (stopped == true) {
scrollID = startScrolling();
stopped = false;
} else {
stopScroll();
stopped = true;
}
}
}, true);
我們在此示例程式碼中使用了 setInterval
方法中的 scrollBy
函式來為滾動設定動畫。
當你希望頁面滾動到一定數量的畫素時,使用 scrollBy
方法。scrollBy
函式有兩個引數,x
和 y
(都是必需的)。
setInterval()
方法在給定的時間間隔內呼叫一個函式。innerHeight
返回視窗的內部高度(以畫素為單位)。scrollY
返回 document
現在垂直滾動的畫素數。
名為 offsetHeight
的只讀屬性以整數形式返回元素的高度(在我們的示例中,它是 body
標籤)。此高度包括邊框和垂直填充。
如果按下 Enter 鍵,上面的程式碼會滾動網頁。並以兩種方式停止滾動,再次按下 Enter 鍵或 window.innerHeight
和 window.scrollY
之和大於或等於 document.body.offsetHeight
。