2

到目前为止,我只为错误消息正常工作。但是,我希望这也适用于成功消息。这应该在联系表单中按下提交按钮时发生。单击页面右上角的联系人以滚动到它。

你可以在这里测试它。

这是我用于错误消息的 jQuery:

     $(document).ready(function() {
     $(".error:first").attr("id","errors");
    $("#errors").each(function (){
        $("html,body").animate({scrollTop:$('#errors').offset().top-175}, 1000);
    });
  });

有什么方法可以修改它以使用相同的 offset().top-175 滚动到 #success 和 #errors ?

提前致谢!

4

3 回答 3

4

你可以这样做:

  $(document).ready(function() {
     var pos = null;
     if($("#contact-form #errors.visible").length > 0)
        pos = $('#errors').offset().top;

     if($("#contact-form #success.visible").length > 0)
        pos = $('#success').offset().top;

     if(pos != null)
         $("html,body").animate({scrollTop:pos-175}, 1000);
  });

并修复您的脚本“js/contact_script.js”必须在 JQuery lib 之后声明的事实

于 2013-01-28T20:47:01.733 回答
1

此解决方案对 Contact Form 7(WordPress 的流行表单插件)进行同样的工作。我在谷歌搜索我的问题时发现了这个页面,所以我添加了下面的解决方案来帮助其他也在这个页面结束的人。

jQuery(function ($) {
    $(document).ready(function ()
    {
        var wpcf7Elm = document.querySelector( '.wpcf7' );
        wpcf7Elm.addEventListener( 'wpcf7submit', function( event ) {
            setTimeout(function() {
                $([document.documentElement, document.body]).animate({
                    scrollTop: $(".wpcf7-response-output").offset().top - 100
                }, 500);
            }, 500);
            //console.log("Submited");
        }, false );
    });
});
于 2019-10-24T17:33:48.453 回答
0
$(document).ready(function () {
    var $elementToScrollTo;
    var $firstError = $(".error:first");
    if ($firstError.length > 0) {
        $firstError.attr("id", "errors");
        $elementToScrollTo = $firstError;
    }
    else {
        $elementToScrollTo = $("#success");
    }
    $("html,body").animate({
        scrollTop: $elementToScrollTo.offset().top - 175
    }, 1000);
});
于 2013-01-28T20:42:49.347 回答