Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
518 views
in Technique[技术] by (71.8m points)

jquery - div above another div, but should scroll beneath the later one

there is a div (namely div_fixed) which is fixed in position. Another div namely other_content is beneath the div_fixed. The other_content div has a padding-top property so that when the page is scrolled only the other_content is scrolled beneath the fixed div.

The fiddle is here

HTML :

<div class="div_fixed">FIXED</div>
<div class="other_content">
    content goes here<br/>
    content goes here<br/>
    content goes here<br/>
</div>

CSS:

     div { color: #fff }
        .div_fixed { position: fixed;  
    width: 100%; 
    height: 100px; 

    
    }

.other_content { 
background: #f00; 
height: 5000px; 
color: #fff; 
padding-top: 100px; 

margin:0 auto;

}

But I want the non-fixed div to stay upon the fixed div and want the non-fixed div to disappear just beneath its upper edge i.e. position of the non-fixed div's upper edge will remain fixed but it's content will start disappearing on page scroll just the way it happened when it was staying beneath the fixed div.

So i had a little edit in the html ( just 2 breaks) before the non-fixed div :

    <div class="div_fixed">FIXED</div>
<br><br/>
        <div class="other_content">
            content goes here<br/>
            content goes here<br/>
            content goes here<br/>
        </div>

The addition in css is:

  .fixed_div{
    
    z-index:-1;
    }
    
    .other_content{
    
    
    width:60%;
    z-index:99099900;

    
    }

But the upper edge of the non-fixed div does not stay at the position I want.

How to achieve that ?

EDIT :

suppose I add a background image for the non-fixed div. Is it possible that part of background image of the fixed div over which the other div will scroll will have a z-index higher than that of the non-fixed div ? Will the problem be solved in that way ?

EDIT2

Let use suppose that fixed_div is the header, other_content is the content body of a webpage. Let us add a footer div with id footer. No scroll should be present in other_content. other_content should scroll when the page is scrolled.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this is what you're looking for. You'll want to position the fixed div in a fixed fashion, but the second div doesn't need special positioning. Just give it a margin-top:100px where 100px is the height of the fixed div.

The trick is to use z-index and to add position:relative; to the content div

Working Demo: http://jsfiddle.net/KyP8L/3/

.div_fixed{
    height:100px;
    width:100%;    
    position:fixed;
    top:0;
    left:0;
    background:#ff0;
    z-index:500;
}
.other_content {
    width:60%;    
    background:#0ff;
    margin-top:100px;
    z-index:600;
    position:relative;
}
?
?

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...