If Else JQuery error

If Else JQuery error

本文关键字:error JQuery Else If      更新时间:2023-09-26

我已经成功编辑了一个wordpress, woocommerce js文件。但是,当我尝试更改标准滚动功能以首先检查浏览器宽度时,它会因意外的标记(错误)而失败。我看不出问题在哪里,我试过好几种重写方法。我正在尝试的代码是:

// Scroll to top
    $( 'html, body' ).animate( {
if( $(window).width() < 680 ) {
   scrollTop: ( $( 'form.checkout' ).offset().top - 100 )
}, 1000;
else {
   scrollTop: ( $( 'form.checkout' ).offset().top - 170 )
}, 1000;
                    }

animate调用之前进行条件检查-而不是在它内部!

if( $(window).width() < 680 ) {
    $( 'html, body' ).animate( {
        scrollTop: ( $( 'form.checkout' ).offset().top - 100 )
    });
} else {
    $( 'html, body' ).animate( {
        scrollTop: ( $( 'form.checkout' ).offset().top - 170 )
    });
}

或者另一种方法:

var offset = ($(window).width() < 680) ? 100 : 170;
$( 'html, body' ).animate( {
    scrollTop: ( $( 'form.checkout' ).offset().top - offset)
});