【发布时间】:2014-06-29 15:47:28
【问题描述】:
当宽度低于 768 像素时,我有一个固定在页面顶部的导航栏。
当它超过 768px 时,它从底部开始,当用户滚动经过它时,它会卡在顶部。
这两个实例本身都可以正常工作,但是当浏览器调整大小时,从 768 像素以下到以上时会出现一些问题。 (从 768 到以下工作正常。)
当我在 768 像素以下的浏览器中加载页面,然后调整上方窗口的大小时,我遇到了问题。
我希望导航栏在状态之间平稳过渡。 (当加载到 768 像素以上然后调整到下方并调整到上方时,它工作得很好 - 理想情况下,这就是我希望它在加载到 768 像素以下时工作的方式。)或者作为替代方案,当从下方移动时,只需将导航栏固定到顶部768px 以上。
这是我的 CSS
.header{
width: 100%;
min-width: 300px;
height: 100px;
padding: 0px;
background: black;
position: absolute;
bottom: 0px;
z-index: 99999;
-webkit-font-smoothing: subpixel-antialiased;
}
.header.fixed{
width: 100%;
position: fixed;
top: 0px;
height: 100px;
-webkit-font-smoothing: subpixel-antialiased;
}
@media only screen and (max-width: 768px){
.header{
width: 100%;
background: black;
position: fixed;
top: 0px;
height: 100px;
-webkit-font-smoothing: subpixel-antialiased;
}
}
这是Javascript
<script>
jQuery(document).ready(function() {
var s = jQuery(".header");
var pos = s.position();
jQuery(window).scroll(function() {
var windowpos = jQuery(window).scrollTop();
if (windowpos >= pos.top) {
s.addClass("fixed");
} else {
s.removeClass("fixed");
}
});
});
</script>
我也试过下面没有运气。
<script>
if ( jQuery(window).width() > 768) {
jQuery(document).ready(function() {
var s = jQuery(".header");
var pos = s.position();
jQuery(window).scroll(function() {
var windowpos = jQuery(window).scrollTop();
if (windowpos >= pos.top) {
s.addClass("fixed");
} else {
s.removeClass("fixed");
}
});
});
}</script>
<script>
jQuery(window).resize(function(){
if ( jQuery(window).width() > 768) {
jQuery(document).ready(function() {
var s = jQuery(".header");
var pos = s.position();
jQuery(window).scroll(function() {
var windowpos = jQuery(window).scrollTop();
if (windowpos >= pos.top) {
s.addClass("fixed");
} else {
s.removeClass("fixed");
}
});
});
}})</script>
【问题讨论】:
标签: javascript jquery responsive-design nav sticky