My website features a sticky navigation, but it won't work on mobile devices...
我的网站具有粘性导航,但它不适用于移动设备...
How do I make it sticky on mobile devices, or how do I prevent it from being sticky (while keeping the navigation, of course)?
如何在移动设备上粘贴它,或者如何防止粘性(当然保持导航)?
I looked everywhere for an answer, but couldn't find one that worked (and didn't disable my navigation:) Your help is much appreciated!
我到处寻找答案,但找不到一个有效(并没有禁用我的导航:)你的帮助非常感谢!
This is a part of my index.html header:
这是我的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>
And this is the code in the jquery.sticky javascript:
这是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 个解决方案
#1
17
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
}
#2
3
Abstain from reading the user agent string as it may be tampered with or spoofed. Use Modernizr which performs a test of what features exactly the user environment supports, specifically for touch: if (Modernizr.touch)
不要读取用户代理字符串,因为它可能被篡改或欺骗。使用Modernizr执行测试用户环境支持的功能,特别是触摸:if(Modernizr.touch)
For your needs:
满足您的需求:
<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>
#3
2
You should check if user is using a mobile browser or not. if so don't execute the sticky plugin method.
您应该检查用户是否使用移动浏览器。如果是这样,不执行粘性插件方法。
<!-- 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>
The code for user agent detection is borrowed from here here
这里借用了用户代理检测的代码
#4
2
I think a good soluion would be is to just check for screen or viewport size. This is in no way a check for mobile devices, but unsticking the navigation on small screens/viewports seems pretty reasonable. I'ts not the most important part of the page on a small screen, site content is. Sticky navigation isn't all that critical.
我认为一个好的解决方案就是检查屏幕或视口大小。这绝不是对移动设备的检查,但在小屏幕/视口上取消导航似乎非常合理。我不是小屏幕上页面最重要的部分,网站内容是。粘性导航并不是那么重要。
Checking for particular platforms or devices is so 1999-ish, I wouldn't recommend it. What if a user is seeing your site on a win8 tablet or something, do you still need that sticky nav?
检查特定的平台或设备是如此1999年,我不推荐它。如果用户在win8平板电脑上看到您的网站,或者您仍然需要粘性导航,该怎么办?
Add the following to the <head>
to let mobile devices know the width of your site content (change 960 to whatever width you have):
将以下内容添加到,让移动设备知道您网站内容的宽度(将960更改为您拥有的任何宽度):
<meta name="viewport" content="width=960px, initial-scale=1">
And in javascript check like this (change 640 to whatever you feel appropriate):
并在javascript中检查这样(将640更改为您认为合适的任何内容):
if (document.documentElement.clientWidth > 640 ) {
$("nav").sticky({ topSpacing: 0, className: 'sticky', center: true });
}
#1
17
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
}
#2
3
Abstain from reading the user agent string as it may be tampered with or spoofed. Use Modernizr which performs a test of what features exactly the user environment supports, specifically for touch: if (Modernizr.touch)
不要读取用户代理字符串,因为它可能被篡改或欺骗。使用Modernizr执行测试用户环境支持的功能,特别是触摸:if(Modernizr.touch)
For your needs:
满足您的需求:
<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>
#3
2
You should check if user is using a mobile browser or not. if so don't execute the sticky plugin method.
您应该检查用户是否使用移动浏览器。如果是这样,不执行粘性插件方法。
<!-- 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>
The code for user agent detection is borrowed from here here
这里借用了用户代理检测的代码
#4
2
I think a good soluion would be is to just check for screen or viewport size. This is in no way a check for mobile devices, but unsticking the navigation on small screens/viewports seems pretty reasonable. I'ts not the most important part of the page on a small screen, site content is. Sticky navigation isn't all that critical.
我认为一个好的解决方案就是检查屏幕或视口大小。这绝不是对移动设备的检查,但在小屏幕/视口上取消导航似乎非常合理。我不是小屏幕上页面最重要的部分,网站内容是。粘性导航并不是那么重要。
Checking for particular platforms or devices is so 1999-ish, I wouldn't recommend it. What if a user is seeing your site on a win8 tablet or something, do you still need that sticky nav?
检查特定的平台或设备是如此1999年,我不推荐它。如果用户在win8平板电脑上看到您的网站,或者您仍然需要粘性导航,该怎么办?
Add the following to the <head>
to let mobile devices know the width of your site content (change 960 to whatever width you have):
将以下内容添加到,让移动设备知道您网站内容的宽度(将960更改为您拥有的任何宽度):
<meta name="viewport" content="width=960px, initial-scale=1">
And in javascript check like this (change 640 to whatever you feel appropriate):
并在javascript中检查这样(将640更改为您认为合适的任何内容):
if (document.documentElement.clientWidth > 640 ) {
$("nav").sticky({ topSpacing: 0, className: 'sticky', center: true });
}