以下代码在Opera中运行良好,但在FF和Chrome中运行不正常。标题部分从像素(行)100px开始,而不是页面顶部的0px。我想让它与浏览器兼容?
注意:标题始终固定在顶部,并且根据滚动操作可以扩展到100px的高度或缩小到50px的高度。
jfiddle失败的原因。我无法创造它。
由于
JS
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function ()
{
var showing_default = true;
var did_scroll = false;
$(window).on("scroll", function (e)
{
did_scroll = true;
});
window.setInterval(function ()
{
if (did_scroll)
{
did_scroll = false;
if (showing_default && $(document).scrollTop() >= 100)
{
showing_default = false;
$("#header_container").css('position', 'fixed');
$("#default").stop().hide();
$("#sticky").fadeIn(500);
}
else if (! showing_default && $(document).scrollTop() < 100)
{
showing_default = true;
$("#sticky").stop().hide();
$("#default").fadeIn(500);
$("#header_container").css('position', 'fixed');
}
}
}, 250);
});
</script>
CSS
/* --- COMMON ---------------------------------------------- */
*
{
margin: 0px;
padding: 0px;
}
/* --- HEADER ---------------------------------------------- */
#header_container
{
display: block;
z-index: 100;
position: fixed;
width: 100%;
background: #EEEEEE;
}
#default
{
display: block;
margin: auto;
width: 900px;
height: 100px;
}
#sticky
{
display: none;
margin: auto;
width: 900px;
height: 50px;
}
/* --- CAROUSEL -------------------------------------------- */
#carousel_container
{
display: block;
margin-top: 100px;
width: 100%;
background: #FFFFFF;
}
#carousel
{
display: block;
margin: auto;
width: 900px;
}
.image
{
display: none;
width: 900px;
height: 400px;
}
.first
{
display: block;
}
/* --- BODY ------------------------------------------------ */
#body_container
{
display: block;
background: #BABABA;
}
#body
{
display: block;
margin: auto;
width: 900px;
}
/* --- FOOTER ---------------------------------------------- */
#footer_container
{
display: block;
background: #DBDBDB;
}
#footer
{
display: block;
margin: auto;
width: 900px;
}
HTML
<div id="header_container">
<div id="default">DEFAULT HEADER</div>
<div id="sticky">STICKY HEADER</div>
</div>
<div id="carousel_container">
<div id="carousel">
<div class="image first"><img src="images/1.jpg" width="900px" height="400px" alt="" /></div>
<div class="image"><img src="images/2.jpg" width="900px" height="400px" alt="" /></div>
<div class="image"><img src="images/3.jpg" width="900px" height="400px" alt="" /></div>
<div class="image"><img src="images/4.jpg" width="900px" height="400px" alt="" /></div>
</div>
</div>
<div id="body_container">
<div id="body">
TOP<br /><br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br />BODY<br /><br />BOTTOM
</div>
</div>
<div id="footer_container">
<div id="footer">FOOTER</div>
</div>
</body>
</html>
答案 0 :(得分:6)
像这样请在下面的选择器中添加top:0;
:
#header_container {
display: block;
z-index: 100;
position: fixed;
width: 100%;
background: #EEEEEE;
top: 0;
}