如何防止脚本在移动设备上运行?

时间:2012-09-04 08:37:35

标签: javascript iphone ipad mobile

我的网站具有粘性导航功能,但无法在移动设备上使用...

如何在移动设备上粘贴,或者如何防止其粘滞(当然,在保留导航的同时)?

我到处寻找答案,但找不到一个有效的(并没有禁用我的导航:)你的帮助非常感谢!

这是我的index.html标题的一部分:

<head>

<link href="css/reset.css" rel="stylesheet" type="text/css" />
<link href="css/960.css" rel="stylesheet" type="text/css" />
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<link href="fancybox/jquery.fancybox-1.3.4.css" rel="stylesheet" type="text/css" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="js/smooth-scroll.js" type="text/javascript"></script>
<script src="js/jquery.sticky.js" type="text/javascript"></script>
<script src="contactform.js" type="text/javascript"></script>

<!-- sticky nav -->
<script type="text/javascript">
$(window).load(function(){
$("nav").sticky({ topSpacing: 0, className: 'sticky', center: true });
});
</script>

</head>

这是jquery.sticky javascript中的代码:

(function($) {
var defaults = {
        topSpacing: 0,
        bottomSpacing: 0,
        className: 'is-sticky',
        center: false
    },
    $window = $(window),
    $document = $(document),
    sticked = [],
    windowHeight = $window.height(),
    scroller = function() {
        var scrollTop = $window.scrollTop(),
            documentHeight = $document.height(),
            dwh = documentHeight - windowHeight,
            extra = (scrollTop > dwh) ? dwh - scrollTop : 0;
        for (var i = 0; i < sticked.length; i++) {
            var s = sticked[i],
                elementTop = s.stickyWrapper.offset().top,
                etse = elementTop - s.topSpacing - extra;
            if (scrollTop <= etse) {
                if (s.currentTop !== null) {
                    s.stickyElement.css('position', '').css('top', '').removeClass(s.className);
                    s.currentTop = null;
                }
            }
            else {
                var newTop = documentHeight - s.elementHeight - s.topSpacing - s.bottomSpacing - scrollTop - extra;
                if (newTop < 0) {
                    newTop = newTop + s.topSpacing;
                } else {
                    newTop = s.topSpacing;
                }
                if (s.currentTop != newTop) {
                    s.stickyElement.css('position', 'fixed').css('top', newTop).addClass(s.className);
                    s.currentTop = newTop;
                }
            }
        }
    },
    resizer = function() {
        windowHeight = $window.height();
    };

// should be more efficient than using $window.scroll(scroller) and $window.resize(resizer):
if (window.addEventListener) {
    window.addEventListener('scroll', scroller, false);
    window.addEventListener('resize', resizer, false);
} else if (window.attachEvent) {
    window.attachEvent('onscroll', scroller);
    window.attachEvent('onresize', resizer);
}

$.fn.sticky = function(options) {
    var o = $.extend(defaults, options);
    return this.each(function() {
        var stickyElement = $(this);
        if (o.center)
            var centerElement = "margin-left:auto;margin-right:auto;";

        stickyId = stickyElement.attr('id');
        stickyElement
            .wrapAll('<div id="' + stickyId + 'StickyWrapper" style="' + centerElement + '"></div>')
            .css('width', stickyElement.width());
        var elementHeight = stickyElement.outerHeight(),
            stickyWrapper = stickyElement.parent();
        stickyWrapper
            .css('width', stickyElement.outerWidth())
            .css('height', elementHeight)
            .css('clear', stickyElement.css('clear'));
        sticked.push({
            topSpacing: o.topSpacing,
            bottomSpacing: o.bottomSpacing,
            stickyElement: stickyElement,
            currentTop: null,
            stickyWrapper: stickyWrapper,
            elementHeight: elementHeight,
            className: o.className
        });
    });
};
})(jQuery);

4 个答案:

答案 0 :(得分:19)

function isMobile() {
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
}

if (!isMobile()) {
//place script you don't want to run on mobile here

}

答案 1 :(得分:3)

禁止阅读用户代理字符串,因为它可能被篡改或欺骗。使用Modernizr执行用户环境支持的功能测试,特别是触摸:if (Modernizr.touch)

根据您的需要:

<head>

<link href="css/reset.css" rel="stylesheet" type="text/css" />
<link href="css/960.css" rel="stylesheet" type="text/css" />
<link href="css/styles.css" rel="stylesheet" type="text/css" />
<link href="fancybox/jquery.fancybox-1.3.4.css" rel="stylesheet" type="text/css" />

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="js/smooth-scroll.js" type="text/javascript"></script>
<script src="js/jquery.sticky.js" type="text/javascript"></script>
<script src="http://ajax.aspnetcdn.com/ajax/modernizr/modernizr-2.0.6-development-only.js" type="text/javascript"></script>
<script src="contactform.js" type="text/javascript"></script>

<!-- sticky nav -->
<script type="text/javascript">
$(window).load(function(){
    if (Modernizr.touch) {
        $("nav").sticky({ topSpacing: 0, className: 'sticky', center: true });
    }
});
</script>

</head>

答案 2 :(得分:2)

您应该检查用户是否使用移动浏览器。如果是这样,不要执行粘性插件方法。

<!-- sticky nav -->
<script type="text/javascript">

var isMobile = {
Android: function() {
    return navigator.userAgent.match(/Android/i) ? true : false;
},
BlackBerry: function() {
    return navigator.userAgent.match(/BlackBerry/i) ? true : false;
},
iOS: function() {
    return navigator.userAgent.match(/iPhone|iPad|iPod/i) ? true : false;
},
Windows: function() {
    return navigator.userAgent.match(/IEMobile/i) ? true : false;
},
any: function() {
    return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Windows());
}
};

$(function(){
    if(! isMobile.any()){
        $("nav").sticky({ topSpacing: 0, className: 'sticky', center: true });
    }
});
</script>

用户代理检测的代码来自此处here

答案 3 :(得分:2)

我认为一个好的解决方案就是检查屏幕或视口大小。这绝不是对移动设备的检查,但在小屏幕/视口上取消导航似乎非常合理。我不是小屏幕上页面中最重要的部分,网站内容是。粘性导航并不是那么重要。

检查特定的平台或设备是如此1999年,我不推荐它。如果用户在win8平板电脑上看到您的网站或者其他什么,您还需要粘性导航吗?

将以下内容添加到<head>,让移动设备知道您网站内容的宽度(将960更改为您拥有的任何宽度):

<meta name="viewport" content="width=960px, initial-scale=1">

在javascript中检查这样(将640更改为您认为合适的任何内容):

if (document.documentElement.clientWidth > 640 ) {
    $("nav").sticky({ topSpacing: 0, className: 'sticky', center: true });
}