admin管理员组文章数量:1325135
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
4 Answers
Reset to default 4Try 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.
本文标签: jqueryJavascript centering a div on the pageStack Overflow
版权声明:本文标题:jquery - Javascript centering a div on the page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742177606a2427869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论