【问题标题】:jquery scroll to form inputjquery滚动到表单输入
【发布时间】:2014-01-22 19:26:34
【问题描述】:

我正在网页上尝试scrollTo(),我想要一个按钮来触发scrollTo();它将滚动到表单顶部并突出显示第一个输入字段。

我当前的代码有效,但是屏幕快速闪烁。我试图使用preventDefault() 无济于事。请在下面查看我的代码。

$(document).ready(function () {
    $("#get-started").click(function (e) {
        e.preventDefault();
        $('html, body').animate({
            scrollTop: $("#get-risk").offset().top
        }, 2000);
        $("#get-started-apply-now-form [name='last_name']").focus();
    });
});

【问题讨论】:

标签: jquery scrollto scrolltop jquery-scrollable


【解决方案1】:

当聚焦一个元素时,浏览器会滚动到那个元素,打乱你的动画。

将焦点放在动画回调中以避免它。

$(document).ready(function () {
    $("#get-started").click(function (e) {
        e.preventDefault();
        $('html, body').animate({
            scrollTop: $("#get-risk").offset().top
        }, 2000, function() {
            $("#get-started-apply-now-form [name='last_name']").focus();
        });
    });
});

【讨论】:

  • 感谢 adeneo!通过将焦点放在动画回调中,效果很好!
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-20
  • 2015-07-29
  • 1970-01-01
相关资源
最近更新 更多