Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
双击li元素jQuery,滚动到屏幕顶部_Jquery_Html_Css - Fatal编程技术网

双击li元素jQuery,滚动到屏幕顶部

双击li元素jQuery,滚动到屏幕顶部,jquery,html,css,Jquery,Html,Css,我试图完成我的投资组合,但当我在导航栏中单击一个列表元素时,它会将我带到我想去的地方,但当我再次单击同一元素时,它会将我带回到顶部。我想这是因为jQuery正在滚动到href,并且在列表元素中我有一个带有href的锚。我怎样才能阻止这一切 jQuery- $('a').click(function(){ $('html, body').animate({ scrollTop: $('[name="' + $.attr(this, 'href').substr(1) + '

我试图完成我的投资组合,但当我在导航栏中单击一个列表元素时,它会将我带到我想去的地方,但当我再次单击同一元素时,它会将我带回到顶部。我想这是因为jQuery正在滚动到href,并且在列表元素中我有一个带有href的锚。我怎样才能阻止这一切

jQuery-

  $('a').click(function(){
    $('html, body').animate({
      scrollTop: $('[name="' + $.attr(this, 'href').substr(1) + '"]').offset().top
    }, 500); 
  });

我认为问题在于您没有说明窗口的当前滚动位置

当您滚动到某个分区时,其
$.offset().top
为0,即使它不在页面顶部,但在屏幕顶部

将当前滚动条位置添加到您的
scrollTop
功能对我来说很有效:

$('a').click(function(){

    var scrollToName = $(this).attr("href").substr(1);
    var scrollTo = $("body").scrollTop() + $('[name="' + scrollToName + '"]').offset().top;

    $('html, body').animate({
        scrollTop: scrollTo
    }, 500);
});

以下是

我认为问题在于您没有说明窗口的当前滚动位置

当您滚动到某个分区时,其
$.offset().top
为0,即使它不在页面顶部,但在屏幕顶部

将当前滚动条位置添加到您的
scrollTop
功能对我来说很有效:

$('a').click(function(){

    var scrollToName = $(this).attr("href").substr(1);
    var scrollTo = $("body").scrollTop() + $('[name="' + scrollToName + '"]').offset().top;

    $('html, body').animate({
        scrollTop: scrollTo
    }, 500);
});
这是你的电话号码