css怎么實(shí)現(xiàn)平滑滾動(dòng)效果

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

創(chuàng)新互聯(lián)建站是一家專業(yè)提供任城企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、H5響應(yīng)式網(wǎng)站、小程序制作等業(yè)務(wù)。10年已為任城眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。

代碼如下:

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>錨點(diǎn)平滑跳轉(zhuǎn)</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;
}

這樣,在回到頂部的時(shí)候,會(huì)有動(dòng)畫不會(huì)立即過去,或有漸變動(dòng)畫
錨點(diǎn),切換屏幕的時(shí)候也會(huì)有動(dòng)畫

.querySelector('#to-top').onclick = function (el) {
        document.body.scrollTop = document.documentElement.scrollTop = 0;
      }

感謝各位的閱讀,以上就是“css怎么實(shí)現(xiàn)平滑滾動(dòng)效果”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)css怎么實(shí)現(xiàn)平滑滾動(dòng)效果這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

名稱欄目:css怎么實(shí)現(xiàn)平滑滾動(dòng)效果
鏈接URL:http://muchs.cn/article38/pphepp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、面包屑導(dǎo)航、網(wǎng)站營銷全網(wǎng)營銷推廣、手機(jī)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司