本文介绍了我怎么能坚持div后向下滚动一点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们向下滚动页面时,当第二个div满足顶部边界时,我想粘贴第二个div。当它被修复,它应该与其他页面一起滚动。如何实现这一点?

  #settings {
width:100%;
background:#383838;
height:60px;
}
#menu {
width:100%;
position:relative;
height:100px;
background:#aaa;
}
#body-content {
height:900px;
position:relative;
}

和HTML

 < body> 
< div id =top>
< div id =settings>
< / div>
< div id =menu>
< / div>
< / div>
< div id =body-content>
< / div>

< / body>

在此示例中,第二个div应该是固定的,当我们滚动页面。当我们向上滚动时,应该转变到之前的状态本身。

解决方案

您可以使用 jquery / p>

  $(function(){
//检查粘性标题的初始位置
var stickyHeaderTop = $('#stickyheader')。offset()。top;

$(window).scroll(function(){
if($(window).scrollTop()> stickyHeaderTop ){
$('#stickyheader')。css({position:'fixed',top:'0px'});
$(' block');
} else {
$('#stickyheader')。css({position:'static',top:'0px'});
$(' ).css('display','none');
}
});
});

DEMO 不要忘记在网页中加入jquery库连结(假设您作为初学者)

 < script src =http://code.jquery.com/jquery- latest.min.jstype =text / javascript>< / script> 


I wanted to stick the 2nd div when we scroll down the page and when the 2nd div meets the top boundary. When it's fixed, it should scroll along with the other pages. How can I achieve this?

#settings{
    width:100%;
    background:#383838;
    height:60px;
}
#menu{
    width:100%;
    position:relative;
    height:100px;
    background:#aaa;
}
#body-content{
    height:900px;
    position:relative;
}

and the HTML

<body>
<div id="top">
    <div id="settings">
    </div>
    <div id="menu">
    </div>
</div>
<div id="body-content">
</div>

</body>

Here in this example http://jsfiddle.net/WBur3/ , the 2nd div should be fixed when we scroll the page. When we scroll up, should turn into the previous state itself. Please help me.

解决方案

You can get this effect with jquery

$(function(){
        // Check the initial Poistion of the Sticky Header
        var stickyHeaderTop = $('#stickyheader').offset().top;

        $(window).scroll(function(){
                if( $(window).scrollTop() > stickyHeaderTop ) {
                        $('#stickyheader').css({position: 'fixed', top: '0px'});
                        $('#stickyalias').css('display', 'block');
                } else {
                        $('#stickyheader').css({position: 'static', top: '0px'});
                        $('#stickyalias').css('display', 'none');
                }
        });
  });

DEMO HERE

NOTE: Don't forget to include jquery library link in your page (assuming you as beginner)

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

这篇关于我怎么能坚持div后向下滚动一点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 14:47