我已成功编辑 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)
});
关于javascript - If Else JQuery 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31615207/