简体   繁体   中英

Positioning div to top of viewport using jQuery

How do I position the #lightbox div to always be at the top of the viewport?

$(document).ready(function(){
    $('.mehr').click(function() {
    $("body").css("overflow", "hidden");
    $('#lightbox').css({'visibility' : 'visible'}).animate({'background-color': 'rgba(0, 0, 0, 0.9)', 'opacity': '1'}, 500);
});

Many thanks for any help!

CSS :

#lightbox {
  position: absolute;
  z-index: 999;
}

设定position:absolutez-index:1000

position: fixed; top:0; z-index:999999;

You can use this with simple HTML / CSS : http://jsfiddle.net/wZDXg/

HTML:

<div id="lightbox">Light box</div>
<div id="other">Other content</div>

CSS:

body
{
    margin:0;    
}

#lightbox
{
    background-color:gray;
    width : 100%;
    height: 150px;
    position : fixed;
    top      : 0;
    margin-bottom:150px;
}

#other
{
    margin-top : 150px; 
    height     : 1500px;
    background-color:#999999;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM