滚动时需要修复div从顶部停留20px

App*_*Guy 5 html css jquery

我不确定这可以用纯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> 
Run Code Online (Sandbox Code Playgroud)

和CSS

.product_image {
    position: relative;
    float: left;
    width: 450px;
    margin-left: 10px;
    margin-top: 10px;
}

.product_image_fixed {
    position: fixed;
    float: left;
}
Run Code Online (Sandbox Code Playgroud)

我希望我的问题足够清楚,我似乎无法找到解决方案,所以我提前感谢你的帮助!

Roh*_*zad 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"); 
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

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;
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

演示

更多关于