【问题标题】:Why doesn't webpage work with smooth scrolling?为什么网页不能平滑滚动?
【发布时间】:2018-06-05 16:47:23
【问题描述】:

我想让我的导航栏链接平滑滚动到他们选择的链接(目的地)。问题是,它们只移动到目的地,而不是平滑滚动。有解决办法吗?不想用Bootstrap,什么样的JQuery代码可以实现这个平滑滚动

$('a[href*=#]:not([href=#])').click(function() {
  if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') ||
    location.hostname == this.hostname) {

    var target = $(this.hash);
    target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
    if (target.length) {
      $('html,body').animate({
        scrollTop: target.offset().top
      }, 1500);
      return false;
    }
  }
});
.topnav a {
  color: black;
  text-decoration: none;
  font-style: bold;
  font-size: 20px;
  font-family: Cabin;
  padding-left: 20px;
  padding-right: 20px;
}

.column {
  float: left;
  width: 25%;
}


/* Clear floats after rows */

.row:after {
  content: "";
  display: table;
  clear: both;
  padding: 8px;
}


/* Content */

.content {
  background-color: white;
  padding: 10px;
}

.topnav {
  float: right;
  padding-top: 20px;
}

.About {
  padding: 20px;
  text-align: center;
  font-style: bold;
  font-size: 20px;
  font-family: Cabin;
}

p {
  font-size: 16px;
  font-family: Lora;
  padding: 20px;
}

h3 {
  color: white;
  font-family: Cabin;
}

body,
html {
  height: 100%;
  position: relative;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: 0;
  padding: 0;
}

.bg {
  /* The image used */
  position: relative;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background-image: url("house.jpg");
  opacity: 0.80;
  filter: alpha(opacity=80);
  /* For IE8 and earlier */
  /* Full height */
  height: 100%;
  /* Center and scale the image nicely */
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 traditional//EN">
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=`device-width`, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" type="text/css" href="style.css">
  <link href='https://fonts.googleapis.com/css?family=Cabin:700' rel='stylesheet' type='text/css'>
  <link href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">

</head>

<body>
  <div class="bg">
    <div class="topnav">
      <a class="active" href="#Home">Home</a>
      <a href="#Portfolio">Portfolio</a>
      <a href="#Contact">Contact</a>
      <a href="#About">About</a>
    </div>
  </div>
  <div class="About">
    <h1 id="About">About Cabin</h1>
    <p>Cabin is a website theme I created to demonstrate my skills as a front-end web developer. This website uses stock photos free of property and commercial rights.
      <h1 id="Portfolio">Portfolio</h1>
    </p>
    <div class="row">
      <div class="column">
        <div class="content">
          <img src="picOne.jpg" alt="" style="width:100%">
          <h3>My Work</h3>
          <p>Lorem ipsum..</p>
        </div>
      </div>
      <div class="column">
        <div class="content">
          <img src="picTwo.jpg" alt="" style="width:100%">
          <h3>My Work</h3>
          <p>Lorem ipsum..</p>
        </div>
      </div>
      <div class="column">
        <div class="content">
          <img src="picThree.jpg" alt="" style="width:100%">
          <h3>My Work</h3>
          <p>Lorem ipsum..</p>
        </div>
      </div>
      <div class="column">
        <div class="content">
          <img src="picFour.jpg" alt="" style="width:100%">
          <h3>My Work</h3>
          <p>Lorem ipsum..</p>
        </div>
      </div>
    </div>
    <div class="row">
      <div class="column">
        <div class="content">
          <img src="picOne.jpg" alt="" style="width:100%">
          <h3>My Work</h3>
          <p>Lorem ipsum..</p>
        </div>
      </div>
      <div class="column">
        <div class="content">
          <img src="picTwo.jpg" alt="" style="width:100%">
          <h3>My Work</h3>
          <p>Lorem ipsum..</p>
        </div>
      </div>
      <div class="column">
        <div class="content">
          <img src="picThree.jpg" alt="" style="width:100%">
          <h3>My Work</h3>
          <p>Lorem ipsum..</p>
        </div>
      </div>
      <div class="column">
        <div class="content">
          <img src="picFour.jpg" alt="" style="width:100%">
          <h3>My Work</h3>
          <p>Lorem ipsum..</p>
        </div>
      </div>
    </div>
  </div>
</body>

</html>

这是我迄今为止尝试过的代码。

【问题讨论】:

  • 我建议你使用 jQuery.animate()。它带有 3 个内置缓动函数,但您可以手动添加其他函数。

标签: html css navbar


【解决方案1】:

你需要添加jquery让它滚动流畅,检查一下

$('a[href*=#]:not([href=#])').click(function() {
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
        || location.hostname == this.hostname) {

        var target = $(this.hash);
        target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
           if (target.length) {
             $('html,body').animate({
                 scrollTop: target.offset().top
            }, 1500);
            return false;
        }
    }
});
.topnav a {
  color: black;
  text-decoration: none;
  font-style: bold;
  font-size: 20px;
  font-family: Cabin;
  padding-left: 20px;
  padding-right: 20px;

}

.column {
    float: left;
    width: 25%;
}

/* Clear floats after rows */
.row:after {
    content: "";
    display: table;
    clear: both;
    padding: 8px;
}

/* Content */
.content {
    background-color: white;
    padding: 10px;
}
.topnav{
  float: right;
  padding-top: 20px;

}

.About{
padding: 20px;
text-align: center;
font-style: bold;
font-size: 20px;
font-family: Cabin;

}

p{
  font-size: 16px;
  font-family: Lora;
  padding: 20px;
}

h3{
  color: white;
  font-family: Cabin;
}
body, html {
    height: 100%;
    position: relative;
    top: 0; right: 0; bottom: 0; left: 0;
    margin: 0;
    padding: 0;
}

.bg {
    /* The image used */
    position: relative;
    top: 0; right: 0; bottom: 0; left: 0;
    background-image: url("house.jpg");
    opacity: 0.80;
    filter: alpha(opacity=80); /* For IE8 and earlier */

    /* Full height */
    height: 100%;

    /* Center and scale the image nicely */
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class = "bg">
  <div class="topnav">
  <a class="active" href="#Home">Home</a>
  <a href="#Portfolio">Portfolio</a>
  <a href="#Contact">Contact</a>
  <a href="#About">About</a>
</div>
</div>
<div class = "About">
  <h1 id= "About" >About Cabin</h1>
  <p>Cabin is a website theme I created to demonstrate my skills
  as a front-end web developer.

  This website uses stock photos free of property and commercial rights.
</p>
<h1 id="Portfolio">Portfolio</h1>
<div class="row">
  <div class="column">
    <div class="content">
      <img src="picOne.jpg" alt="" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="picTwo.jpg" alt="" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="picThree.jpg" alt="" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="picFour.jpg" alt="" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
</div>
<div class="row">
  <div class="column">
    <div class="content">
      <img src="picOne.jpg" alt="" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="picTwo.jpg" alt="" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="picThree.jpg" alt="" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="picFour.jpg" alt="" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
</div>
</div>

【讨论】:

    【解决方案2】:

    您缺少对 jQuery 的引用。

    只需添加

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    

    之前

    $('a[href*=#]:not([href=#])').click(function() {
    ...
    

    【讨论】:

      【解决方案3】:

      试试这个

      更新小提琴链接:https://jsfiddle.net/vinaykumar0459/1po7nfhy/22/

      $('a').click(function(){
          $('html, body').animate({
              scrollTop: $( $(this).attr('href') ).offset().top
          }, 500);
          return false;
      });
      

      【讨论】:

        【解决方案4】:

        这很好用,对于您所追求的(您需要 JQUERY):

        // Select all links with hashes
        $('a[href*="#"]')
          // Remove links that don't actually link to anything
          .not('[href="#"]')
          .not('[href="#0"]')
          .click(function(event) {
            // On-page links
            if (
              location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') &&
              location.hostname == this.hostname
            ) {
              // Figure out element to scroll to
              var target = $(this.hash);
              target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
              // Does a scroll target exist?
              if (target.length) {
                // Only prevent default if animation is actually gonna happen
                event.preventDefault();
                $('html, body').animate({
                  scrollTop: target.offset().top
                }, 1000, function() {
                  // Callback after animation
                  // Must change focus!
                  var $target = $(target);
                  $target.focus();
                  if ($target.is(":focus")) { // Checking if the target was focused
                    return false;
                  } else {
                    $target.attr('tabindex', '-1'); // Adding tabindex for elements not focusable
                    $target.focus(); // Set focus again
                  };
                });
              }
            }
          });
        .topnav a {
          color: black;
          text-decoration: none;
          font-style: bold;
          font-size: 20px;
          font-family: Cabin;
          padding-left: 20px;
          padding-right: 20px;
        }
        
        .column {
          float: left;
          width: 25%;
        }
        
        
        /* Clear floats after rows */
        
        .row:after {
          content: "";
          display: table;
          clear: both;
          padding: 8px;
        }
        
        
        /* Content */
        
        .content {
          background-color: white;
          padding: 10px;
        }
        
        .topnav {
          float: right;
          padding-top: 20px;
        }
        
        .About {
          padding: 20px;
          text-align: center;
          font-style: bold;
          font-size: 20px;
          font-family: Cabin;
        }
        
        p {
          font-size: 16px;
          font-family: Lora;
          padding: 20px;
        }
        
        h3 {
          color: white;
          font-family: Cabin;
        }
        
        body,
        html {
          height: 100%;
          position: relative;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          margin: 0;
          padding: 0;
        }
        
        .bg {
          /* The image used */
          position: relative;
          top: 0;
          right: 0;
          bottom: 0;
          left: 0;
          background-image: url("house.jpg");
          opacity: 0.80;
          filter: alpha(opacity=80);
          /* For IE8 and earlier */
          /* Full height */
          height: 100%;
          /* Center and scale the image nicely */
          background-position: center;
          background-repeat: no-repeat;
          background-size: cover;
        }
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 traditional//EN">
        <html lang="en">
        
        <head>
          <meta charset="UTF-8">
          <meta name="viewport" content="width=`device-width`, initial-scale=1.0">
          <meta http-equiv="X-UA-Compatible" content="ie=edge">
          <title>Document</title>
          <link rel="stylesheet" type="text/css" href="style.css">
          <link href='https://fonts.googleapis.com/css?family=Cabin:700' rel='stylesheet' type='text/css'>
          <link href="https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic" rel="stylesheet" type="text/css">
        
        </head>
        
        <body>
          <div class="bg">
            <div class="topnav">
              <a class="active" href="#Home">Home</a>
              <a href="#Portfolio">Portfolio</a>
              <a href="#Contact">Contact</a>
              <a href="#About">About</a>
            </div>
          </div>
          <div class="About">
            <h1 id="About">About Cabin</h1>
            <p>Cabin is a website theme I created to demonstrate my skills as a front-end web developer. This website uses stock photos free of property and commercial rights.
              <h1 id="Portfolio">Portfolio</h1>
            </p>
            <div class="row">
              <div class="column">
                <div class="content">
                  <img src="picOne.jpg" alt="" style="width:100%">
                  <h3>My Work</h3>
                  <p>Lorem ipsum..</p>
                </div>
              </div>
              <div class="column">
                <div class="content">
                  <img src="picTwo.jpg" alt="" style="width:100%">
                  <h3>My Work</h3>
                  <p>Lorem ipsum..</p>
                </div>
              </div>
              <div class="column">
                <div class="content">
                  <img src="picThree.jpg" alt="" style="width:100%">
                  <h3>My Work</h3>
                  <p>Lorem ipsum..</p>
                </div>
              </div>
              <div class="column">
                <div class="content">
                  <img src="picFour.jpg" alt="" style="width:100%">
                  <h3>My Work</h3>
                  <p>Lorem ipsum..</p>
                </div>
              </div>
            </div>
            <div class="row">
              <div class="column">
                <div class="content">
                  <img src="picOne.jpg" alt="" style="width:100%">
                  <h3>My Work</h3>
                  <p>Lorem ipsum..</p>
                </div>
              </div>
              <div class="column">
                <div class="content">
                  <img src="picTwo.jpg" alt="" style="width:100%">
                  <h3>My Work</h3>
                  <p>Lorem ipsum..</p>
                </div>
              </div>
              <div class="column">
                <div class="content">
                  <img src="picThree.jpg" alt="" style="width:100%">
                  <h3>My Work</h3>
                  <p>Lorem ipsum..</p>
                </div>
              </div>
              <div class="column">
                <div class="content">
                  <img src="picFour.jpg" alt="" style="width:100%">
                  <h3>My Work</h3>
                  <p>Lorem ipsum..</p>
                </div>
              </div>
            </div>
          </div>
        </body>
        
        </html>

        【讨论】:

          【解决方案5】:

          更新小提琴链接:https://jsfiddle.net/1po7nfhy/20/

          首先需要jQuery,但您的选择器无效。 # 是一个特殊的字符,需要像

          一样进行转义
          $('a[href*=\\#]:not([href=\\#])')
          

          https://api.jquery.com/category/selectors/

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-10
            • 2021-01-24
            • 1970-01-01
            相关资源
            最近更新 更多