使用jQuery平滑滚动到ID

使用jQuery平滑滚动到ID,jquery,html,Jquery,Html,在使用我的导航栏导航到ID时,难以实现平滑滚动功能。相反,它会进行默认跳转,这有点让人头疼 我在使用jQuery方面非常缺乏经验,因此任何指向正确方向的指针都将不胜感激 我已经尝试过实现它,但是,正如我所说的,它是“跳跃”而不是滚动 HTML 也许可以尝试指定缓和选项 大概是这样的: $("#popular").click(function() { $('html, body').animate({ scrollTop: $("#popular").offset().to

在使用我的导航栏导航到ID时,难以实现平滑滚动功能。相反,它会进行默认跳转,这有点让人头疼

我在使用jQuery方面非常缺乏经验,因此任何指向正确方向的指针都将不胜感激

我已经尝试过实现它,但是,正如我所说的,它是“跳跃”而不是滚动

HTML


也许可以尝试指定缓和选项

大概是这样的:

$("#popular").click(function() {
    $('html, body').animate({
        scrollTop: $("#popular").offset().top
    }, 800, 'linear');
});
或:


将上述内容添加到$(文档)中。jQuery文件中的ready函数和所有以#开头的链接都会有一个指向目标的动画滚动条。

$('col-sm-12')
正在寻找一个
标记,不用说它不存在。你的意思是说,
$('.col-sm-12')
?已经做出了改变,但仍然没有区别。谢谢你!您还在
.col-sm-12
中设置滚动动画,可能应该是
$(窗口)。设置动画
。还有一个问题,
$(“#popular”)。在
上应该是
$(“a”)。在
上是更具体的问题。仍然只是跳跃(默认),道歉。您是否收到任何javascript错误?还要确保你的div有一个开场白“我不知道你想用Jquery做什么,但你可以试试:$(document).ready(function(){$(“#popular”).on('click',function(e){$('.col-sm-12')。animate({scrollTop:$(this).offset().top},800);});不幸的是,还是一样。
$(document).ready(function() {
  $("#popular").on('click', function(event) {
    if (this.hash !== "") {
      event.preventDefault();
      var hash = this.hash;
      $('col-sm-12').animate({
        scrollTop: $(hash).offset().top
      }, 800, function() {
        window.location.hash = hash;
      });
    }
$("#popular").click(function() {
    $('html, body').animate({
        scrollTop: $("#popular").offset().top
    }, 800, 'linear');
});
$(document).ready(function () {
    $("#popular").on('click', function (e) {
        $('.col-sm-12').animate({
            scrollTop: $(this).offset().top
        }, 800);
    });
});
    $(document).ready(function(){
  $('a[href^="#"]').on('click',function (e) {
      e.preventDefault();

      var target = this.hash;
      var $target = $(target);

      $('html, body').stop().animate({
          'scrollTop': $target.offset().top
      }, 900, 'swing', function () {
          window.location.hash = target;
      });
  });
});