最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Javascript centering a div on the page - Stack Overflow

programmeradmin2浏览0评论

For javascript I use the jQuery framework but can easily integrate any javascript functions into it. The problem I have is I have a div that fade's in when I click a link. How can I get it to align in the center of the page and stay even when scrolling.

Below is an example of what I currently have implemented:

HTML code:

<div id="dividname">
    <h2>Heading Goes Here</h2>
    <p>content goes here</p>

    <p><a href="#" class="close-box">Close Box</a></p>
</div>

CSS code:

#dividname {
    position:absolute; z-index:100; width:600px; height:600px; display:none;
}

jQuery code:

$(document).ready(
    function() {

        // on click show div
        $('a.popup').click(
            function() {
                $('#dividname').fadeIn('slow');
            }
        }

    }
);

For javascript I use the jQuery framework but can easily integrate any javascript functions into it. The problem I have is I have a div that fade's in when I click a link. How can I get it to align in the center of the page and stay even when scrolling.

Below is an example of what I currently have implemented:

HTML code:

<div id="dividname">
    <h2>Heading Goes Here</h2>
    <p>content goes here</p>

    <p><a href="#" class="close-box">Close Box</a></p>
</div>

CSS code:

#dividname {
    position:absolute; z-index:100; width:600px; height:600px; display:none;
}

jQuery code:

$(document).ready(
    function() {

        // on click show div
        $('a.popup').click(
            function() {
                $('#dividname').fadeIn('slow');
            }
        }

    }
);
Share Improve this question edited Jul 23, 2009 at 23:29 cointilt asked Jul 15, 2009 at 19:08 cointiltcointilt 7282 gold badges8 silver badges18 bronze badges 2
  • Yes, it must work in IE6. I don't want to support IE6 anymore but some of my clients analytics show that enough people access the site with IE6 – cointilt Commented Jul 16, 2009 at 19:30
  • Also thanks everyone for your answers, I have not had a chance to test any of them out yet. I will let you all know how well they work. – cointilt Commented Jul 16, 2009 at 19:30
Add a ment  | 

4 Answers 4

Reset to default 4

Try this:

#dividname {
    position: fixed;
    top: 50%;
    left: 50%;
    margin-top: -300px;
    margin-left: -300px;
    z-index: 100;
    width: 600px;
    height: 600px;
}

Where margin-top and margin-left is half of height and width respectively.

try this modal div for jquery

This tool will take care of the moving for you if the user scrolls

// to show
$.blockUI({ message: $('[id$=div]'),
    css: 
    {
        top: 50%;
        left: 50%;
        margin-top: -300px;
        margin-left: -300px;
    }
 });

// to hide                                    
$.unblockUI({ message: $('[id$=div]') });

Try changing your style to this,

#dividname {

z-index:100; width:600px; height:600px;
position: fixed;
top: 50%;
left: 50%;

}

Hope this helps!

Edit:

Btw, here's a hack to get it to also work in IE6,

* html #dividname {
 position: absolute;
 top: expression((document.documentElement.scrollTop || document.body.scrollTop) +
 Math.round(17 * (document.documentElement.offsetHeight || document.body.clientHeight) /
 100) + 'px');
}

(Taken from the jqModal Style Sheet)

In order to keep it centered with scrolling you will need to attach a function to move it to your scroll position.

You can do that with:

$(window).scroll(resize())

Get your current position with:

$(window).scrollTop();

This will ply with IE6 issues.

发布评论

评论列表(0)

  1. 暂无评论