简体   繁体   English

jQuery平滑滚动锚点

[英]Jquery Smooth Scrolling anchor

I am having a problem, so basically, I tried to add a smooth scrolling with jquery, I want to have a smooth scrolling everytime I click in my navbar anchor link, I tried to do it by myself but it's not working, any help would be much appreciated, Thanks. 我遇到了问题,因此基本上,我尝试使用jquery添加平滑滚动,我想每次单击导航栏锚链接时都进行平滑滚动,我尝试自己完成此操作,但是它不起作用,任何帮助都会非常感谢,谢谢。

// Smooth Scroll to div
$(".ser").click(function() {
  $("html, body").animate(
    {
      scrollTop: $("#services").offset().top
    },
    1000
  );
});
<div class="navbar">
  <div class="container">
    <div class="brand">
      <h2>Kataki</h2>
    </div>
    <ul class="links">
      <li>
        <a href="#" class="active">
          Home
        </a>
      </li>
      <li>
        <a class="ser" href="#">
          About
        </a>
      </li>
      <li>
        <a href="#">Portfolio</a>
      </li>
      <li>
        <a href="#">Testimonials</a>
      </li>
      <li>
        <a href="#">Contact</a>
      </li>
    </ul>
    <div class="clearfix" />
  </div>
</div>
<div id="services" class="services">
  <div class="container">
    <h2>Our Services</h2>
  </div>
</div>

You appear to be using the slim build of jQuery, which doesn't include most of the library. 您似乎使用的是jQuery的苗条版本,其中不包含大多数库。 Instead, you should be using the full version and just need to put your script in document ready event : 相反,您应该使用完整版本,只需要将脚本置于文档就绪事件中即可:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
    </script>
    <script>
    // Smooth Scroll to div
    $(document).ready(function(){
    $(".ser").click(function() {
    $("html, body").animate(
    {
  scrollTop: $("#services").offset().top
},1000
);
 });
  });
   </script>

I tried this Here and it worked fine. 在这里尝试过,效果很好。 if this doesn't solve your issue then you probably have another java script error in your code. 如果这不能解决您的问题,则您的代码中可能还有另一个Java脚本错误。 I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM