【问题标题】:scrolltop does not work on centosscrolltop 在 centos 上不起作用
【发布时间】:2015-12-17 20:36:19
【问题描述】:

我正在尝试使单击按钮时滚动平滑。它适用于本地 apache 服务器和 IE10+。但是当部署在“centos”上的项目中时,它在同一个浏览器上不起作用。不会产生错误。谁能帮我找出这背后的原因?

    $(document).ready(function() {
    //+ Scroll smoothly to the target href.
    $("#btnGenerate").click(function(e){
        e.preventDefault();

        var offset = $( $.attr(this, 'href') ).offset().top;
        if (offset == 0)
        {
            offset = 230;
        }

        $("html, body").animate({
            scrollTop: offset
            },
            1000,
            function () {
                alert('animation complete.');
            }
        );
    });
    //-
});

HTML:

<input id="btnGenerate" href="#labelFirstChart" type="button" value="Generate"/>

【问题讨论】:

  • 浏览器控制台说什么?...流畅如你所愿?..这里的代码应该可以正常工作..
  • 控制台上的偏移值符合预期。但动画函数不会被调用。
  • 仍然陷入了这个问题。有人有答案吗?

标签: javascript jquery html centos


【解决方案1】:

这应该适用于具有锚链接的元素:

$('a[href^=#]').on('click', function(e){
    e.preventDefault();
    var href = $(this).attr('href');
    $('html, body').animate({ 
        scrollTop:$(href).offset().top
    },'slow');
});

这是一个例子:

http://codepen.io/acki/pen/wMGaPB

【讨论】:

  • 浏览器控制台是什么意思?一些 JS 错误?
  • 感谢您的帮助 Christoph,但它在 localhost 上运行良好。但在 centos 上没有。所有包含的 jquery 和其他文件在两个项目中都是相同的。
  • 如果您将我的代码复制到 CentOS 上的测试文件中,那么它可以工作吗?
  • 还有:你能给我一个测试链接吗?
  • 是的。试用作品。似乎代码文件中的错误。编译文件完全没有错误。
猜你喜欢
  • 1970-01-01
  • 2015-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 2019-07-31
相关资源
最近更新 更多