javascript - If Else JQuery 错误

标签 javascript jquery if-statement

我已成功编辑 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/

相关文章:

javascript - Material-UI AppBar 按钮在较小的屏幕上相互折叠

javascript - 使用 MVC Form Post 发送 HTML 字符串

javascript - node.js 使用 xml2js 将 xml 转换为 json

javascript - 使用 Laravel 测试时是否启用了 JS?

javascript - Jquery .get 未检索文件

jquery - 类型 'ripples' 上不存在属性 'JQuery<HTMLElement>' 。 Angular 分量

javascript - 幻灯片 javascript 不工作

if-statement - 数字范围之间的多个 IF 语句

Python If 语句使用 'and' 和 'in'

javascript - 如何使用javascript中的if语句根据随机数显示/隐藏文本(无jquery)