【问题标题】:jQuery scrollTo plugin not workingjQuery scrollTo 插件不起作用
【发布时间】:2017-10-09 20:02:12
【问题描述】:

我使用的是1.4.13版本的插件,我也确保页面已经成功加载了插件。

它曾经在我的网站上运行良好,但现在它似乎什么也没做。我正在使用以下代码来运行它:

HTML:

<span class="button-project-info">
    <a class="button button-04" href="#project-info">Project Info</a>
</span>

CSS:

$('.button-project-info a').bind('click', function(e) {
    try {
        e.preventDefault();
        target = this.hash;
        $('html, body').scrollTo(target, 150);
    } catch (error) {
        alert('error - ' + error);
    }
});

我也尝试了以下方法,点击链接时alert 语句运行良好:

$('.button-project-info a').bind('click', function(e) {
    alert('0000');
});

【问题讨论】:

    标签: javascript jquery html scroll scrollto


    【解决方案1】:

    如果要滚动整个页面,请使用:

        target = this.hash;
        $(window).scrollTo(target, 150);
    

    当我这样做时,我倾向于使用锚点中的​​href,所以我从链接中获取哈希而不是target = this.hash;

    这是我的首选实现,它基本上可以捕获所有哈希 URL 和滚动...

    $('.button-project-info a').click(function () {
      var hash = '#' + this.href.split('#')[1];
      $(window).scrollTo(hash, 1000);
      return false;
    });
    

    【讨论】:

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