我不确定是否可以使用纯CSS或者如果我需要使用一些jQuery来完成此操作。
我有一个div(product_image),它在当前状态下距离顶部大约400px并且相对定位,因此它清除了顶部菜单和标题,我需要做的是当用户向下滚动并达到大约20px时从div的顶部开始,我需要div才能修复。
这是我尝试过的,我有主要的div与相对定位然后我有另一个div包装内部固定定位。问题在于div保持在距离顶部400px的位置。
以下是代码:
<div class="product_image">
<div class="product_image_fixed">
<a href="products/1.jpg" class="zoom" title="A bed!" rel="gal1">
<img src="products/1.jpg" width="450" alt="" title="A bed!">
</a>
<ul id="thumblist" class="clearfix" >
<li><a class="zoomThumbActive" href='javascript:void(0);' rel="{gallery: 'gal1', smallimage: 'products/1.jpg',largeimage: 'products/1.jpg'}"><img src='products/1.jpg' width="150" height="100"></a></li>
<li><a href='javascript:void(0);' rel="{gallery: 'gal1', smallimage: 'products/1a.jpg',largeimage: 'products/1a.jpg'}"><img src='products/1a.jpg' width="150" height="100"></a></li> </ul>
</div>
</div>
和CSS
.product_image {
position: relative;
float: left;
width: 450px;
margin-left: 10px;
margin-top: 10px;
}
.product_image_fixed {
position: fixed;
float: left;
}
我希望我的问题足够明确,我似乎无法找到解决方案,所以我提前感谢你的帮助!
答案 0 :(得分:12)
用于jquery
<强> Jquery的强>
$(document).ready(function() {
var s = $("#sticker");
var pos = s.position();
$(window).scroll(function() {
var windowpos = $(window).scrollTop();
if (windowpos >= pos.top) {
s.addClass("stick");
} else {
s.removeClass("stick");
}
});
});
<强>的CSS 强>
div#wrapper {
margin:0 auto;
width:500px;
background:#FFF;
}
div#mainContent {
width:160px;
padding:20px;
float:left;
}
div#sideBar {
width:130px;
padding:20px;
margin-left:30px;
float:left;
}
.clear {
clear:both;
}
div#sticker {
padding:20px;
margin:20px 0;
background:#AAA;
width:190px;
}
.stick {
position:fixed;
top:0px;
}
<强> HTML 强>
<div id="wrapper">
<div id="mainContent">
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
some content here <br />
</div>
<div id="sideBar">Some content here
<!--Some content in your right column/sidebar-->
<div id="sticker">...This is scroll here </div>
</div>
<div class="clear"></div>
</div>
<强> Demo 强>