如何在使用css滚动时将div叠加到其他div上

时间:2013-07-26 12:03:51

标签: jquery css html overlay

我有一个网页标题,当用户向下滚动页面时会向下滑动。

我正在使用jquery这样做。它正在运行,但问题是持有标题的div会落后于页面上的其他div

我试图将其位置设置为绝对或相对,但它对我没用。

这是我的代码。

HTML

 <!--End Toolip-->
    </head>


<div id="headerSlideContainer">

    <div id="headerSlideContent">

        Header content goes here!

    </div>

</div>

<!---Myother divs----->

CSS

<style>
#headerSlideContainer {
    position: fixed;

    top:-50px;
    width: 100%;
    background: black;
}
#headerSlideContent {
    width: 900px;
    height: 50px;
    margin: 0 auto;
    color: white;
}
</style>

JQUERY

<script type="text/javascript">
$(function() {

    var bar = $('#headerSlideContainer');
    var top = bar.css('top');
    $(window).scroll(function() {

        if($(this).scrollTop() > 50) {
            bar.stop().animate({'top' : '5px'}, 500);
        } else {
            bar.stop().animate({'top' : top}, 500);
        }
    });
});
</script>

2 个答案:

答案 0 :(得分:1)

我试过了,我希望这就是你想要的......

click this link for demo

我在需要的地方添加了z-index。现在这就是css和html的外观。

<div id="headerSlideContainer">

    <div id="headerSlideContent">

        Header content goes here!

    </div>
    <div id="new">
        Other Contents
    </div>

</div>

的CSS:

#headerSlideContainer {
        position: absolute;
         top:0px;
        width: 100%;

       height:1000px;
        background: black;
    }
    #headerSlideContent {
        width: 900px;
        height: 50px;
        margin: 0 auto;
        color: white;
        background:Red;
        z-index:10000;
        position:fixed;
    }

    #new{
        top:60px;
        z-index:2;
        height:100px;
        background:Orange;
        position:absolute;
        color: white;

    }

答案 1 :(得分:0)

您可以将要保持在顶部的元素的位置设置为固定。

E.g。如果你想把#headerSlideContainer保持在最顶层那么

#headerSlideContainer{
position: fixed;
}

它会做到这一点。不需要jQuery。