【问题标题】:jquery smooth scroll on click (both directions, down and up)jquery在点击时平滑滚动(两个方向,向下和向上)
【发布时间】:2015-12-16 15:11:35
【问题描述】:

我有一个基本脚本,当用户单击它们时,它会平滑滚动到锚定链接。我的问题是平滑滚动似乎只能以一种方式工作,当我单击“转到顶部”链接之一时,页面只是跳转......

$(function(){
// Smooth scrolling for anchored links
$('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
    }, 2000);
    return false;
  }
}
});
// Scroll down automatically on page load
$('html, body').animate({
scrollTop: $('.destination').offset().top
}, 2000);   
}); 

https://jsfiddle.net/p70d4doq/22/

任何帮助将不胜感激!

谢谢,安德烈亚斯

【问题讨论】:

  • 我觉得不错。 :)

标签: javascript jquery hyperlink anchor smooth-scrolling


【解决方案1】:

你忘了添加这个位:

$('a[href="#"]').click(function () {
    $('html, body').animate({
        scrollTop: 0
    }, 2000);
});

请不要给一个不起作用的小提琴!在发布链接之前检查。

工作小提琴:https://jsfiddle.net/a5u0zzLr/

【讨论】:

  • 好的,对不起。我今天早些时候在这里问了一个问题,有人让我发布一段我遇到问题的代码。所以我认为这样做是可以的。无论如何,谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2018-05-06
  • 2020-04-06
  • 2017-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多