简体   繁体   English

jQuery函数中有条件的iPhone / iPad

[英]iPhone/iPad Conditional in jQuery Function

Is it possible to not have "offset: -195" called when on an iPad or iPhone? 在iPad或iPhone上是否可能没有调用“ offset:-195”?

        $(window).bind("load", function() {

    $(document).on('click', 'a[href*="#"]', function() {
        var slashedHash = '#/' + this.hash.slice(1);
        if (this.hash) {

            if (slashedHash === location.hash) {
                $.smoothScroll({
                    scrollTarget: this.hash,
                    easing: 'easeOutQuart',
                    speed: 2000,
                    offset: -195
                });
            } else {
                $.bbq.pushState(slashedHash);
            }

            return false;
        }
    });

    $(window).bind('hashchange', function(event) {
        var tgt = location.hash.replace(/^#\/?/, '');
        if (document.getElementById(tgt)) {
            $.smoothScroll({
                scrollTarget: '#' + tgt,
                easing: 'easeOutQuart',
                speed: 2000,
                offset: -195

            });
        }
    });

    $(window).trigger('hashchange');
});

Thanks for you help. 感谢您的帮助。

Try this... 尝试这个...

var offset = -195;

var is_uiwebview = /(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/i.test(navigator.userAgent);
if (is_uiwebview) {
    offset = 0;
}

$(window).bind("load", function() {
    $(document).on('click', 'a[href*="#"]', function() {
        var slashedHash = '#/' + this.hash.slice(1);
        if (this.hash) {
            if (slashedHash === location.hash) {
                $.smoothScroll({
                    scrollTarget: this.hash,
                    easing: 'easeOutQuart',
                    speed: 2000,
                    offset: offset
                });
            } else {
                $.bbq.pushState(slashedHash);
            }
            return false;
        }
    });
});

$(window).bind('hashchange', function(event) {
    var tgt = location.hash.replace(/^#\/?/, '');
    if (document.getElementById(tgt)) {
        $.smoothScroll({
            scrollTarget: '#' + tgt,
            easing: 'easeOutQuart',
            speed: 2000,
            offset: offset
        });
    }
});

I've just added the section of code at the beginning that checks the useragent to detect AppleWebKit devices. 我刚刚在代码开头添加了一段代码,用于检查useragent以检测AppleWebKit设备。 The variable offset is set to -195 initially, and then set to 0 if it is an Apple device. 可变offset最初设置为-195,如果 Apple设备,则设置为0。 That value is then used in calls to smoothScroll . 该值然后在对smoothScroll调用中smoothScroll

The code I used to detect browser type was taken directly from an answer on this question... 我用来检测浏览器类型的代码直接来自此问题的答案...

detect ipad/iphone webview via javascript 通过javascript检测ipad / iphone Webview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM