這篇文章主要講解了“css怎么實現(xiàn)平滑滾動效果”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“css怎么實現(xiàn)平滑滾動效果”吧!

創(chuàng)新互聯(lián)建站是一家專業(yè)提供任城企業(yè)網(wǎng)站建設,專注與成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿網(wǎng)站建設、H5響應式網(wǎng)站、小程序制作等業(yè)務。10年已為任城眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡公司優(yōu)惠進行中。
代碼如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>錨點平滑跳轉</title>
<style>
* {
margin: 0;
padding: 0;
}
html {
scroll-behavior: smooth;
}
nav {
width: 50%;
height: 50px;
text-align: center;
position: fixed;
left: 50%;
transform: translateX(-50%);
top: 0;
background: green;
}
nav a {
display: inline-block;
line-height: 50px;
color: #FFF;
text-decoration: none;
padding: 0 30px;
}
.box {
width: 100%;
text-align: center;
font-size: 30px;
color: #FFF;
}
#box1 {
background: #d00;
}
#box2 {
background: #42a4ff;
}
#box3 {
background: #008080;
}
#to-top {
position: fixed;
bottom: 20px;
right: 20px;
border-radius: 50%;
width: 80px;
height: 80px;
background: #ccc;
color: #666;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
}
</style>
</head>
<body>
<nav>
<a href="#box1">box1</a>
<a href="#box2">box2</a>
<a href="#box3">box3</a>
</nav>
<div id="box1" class=" box">box1</div>
<div id="box2" class=" box">box2</div>
<div id="box3" class=" box">box3</div>
<div id="to-top">回到頂部</div>
<script>
onload = function () {
const _Height = document.documentElement.clientHeight;
const Box = document.getElementsByClassName('box');
for (var i = 0; i < Box.length; i++) {
Box[i].style.height = _Height + 'px'
Box[i].style.lineHeight = _Height + 'px'
}
document.querySelector('#to-top').onclick = function (el) {
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
}
</script>
</body>
</html>html {
scroll-behavior: smooth;
}這樣,在回到頂部的時候,會有動畫不會立即過去,或有漸變動畫
錨點,切換屏幕的時候也會有動畫
.querySelector('#to-top').onclick = function (el) {
document.body.scrollTop = document.documentElement.scrollTop = 0;
}感謝各位的閱讀,以上就是“css怎么實現(xiàn)平滑滾動效果”的內容了,經(jīng)過本文的學習后,相信大家對css怎么實現(xiàn)平滑滾動效果這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關知識點的文章,歡迎關注!
名稱欄目:css怎么實現(xiàn)平滑滾動效果
鏈接URL:http://chinadenli.net/article38/pphepp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設、面包屑導航、網(wǎng)站營銷、全網(wǎng)營銷推廣、手機網(wǎng)站建設、靜態(tài)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)