我在弹出 div 时遇到问题。我希望能够相对于主体定位弹出窗口,以便它们不居中。我遇到的问题是它们嵌套在导航 div 中,我只能相对于它定位它们,因为它必须绝对定位,因为我希望它出现在屏幕的中心。
所以我的两个问题是:
- 不能相对于身体定位 Div。
- 当 div 出现时,它们会将导航项推到不合适的位置。
解决方案:
- 找到一种方法让 div 忽略它的当前父 div 并将其自身定位到与 body div 相关的位置。
您可以在下面的 jsFiddle 中找到我的代码,并提前感谢您的帮助。
HTML
<div id="menu">
<div id="name"><a href="" title="">name</a></div>
<ul id="nav">
<li><a class="pop-up-link" href="#" title="about">about</a> |
<div class='about'><a href='#' title="close" class='close'><img src='img/close.png' alt='close' height="20" width="20" /></a>
<p>content</p>
</div>
</li>
<li><a class="pop-up-link" href="#" title="press">press</a> |
<div class='press'><a href='#' title="close" class='close'><img src='img/close.png' alt='close' height="20" width="20" /></a>
<p>content</p>
</div>
</li>
<li><a class="pop-up-link" href="#" title="diary">contact</a> |
<div class='contact'><a href='#' title="close" class='close'><img src='img/close.png' alt='close' height="20" width="20" /></a>
<p>content</p>
</div>
</li>
<li><a class="pop-up-link" href="#" title="contact">diary</a>
<div class='diary'><a href='#' title="close" class='close'><img src='img/close.png' alt='close' height="20" width="20" /></a>
<p>content</p>
</div>
</li>
</ul>
</div>
CSS
body {
position: relative;
width: 100%;
height: 100%;
}
#menu {
position:fixed;
overflow: visible;
width: 400px;
height: 200px;
left: 50%;
top: 50%;
margin-left: -200px;
margin-top: -100px;
text-align: center;
}
#name {
font-size: 26px;
line-height: 50px;
vertical-align: middle;
border-bottom: 1px solid #000;
}
#nav li{
font-size: 14px;
width: 300px;
line-height: 25px;
vertical-align: middle;
display: inline;
}
.close {
float: right;
position: relative;
z-index: 99999;
margin: 3px 6px 0;
}
.about {
position: relative;
padding: 5px;
background: #F7E39A;
display:none;
width:500px;
margin: 0 auto;
min-height: 200px; }
.press {
position: relative;
float: right;
top: -200px;
right: 500px;
padding: 5px;
background: #DEA1B7;
display:none;
width:250px;
min-height: 200px;
}
.contact {
position: relative;
padding: 5px;
background: #6666FA;
display:none;
width:500px;
margin: 0 auto;
min-height: 200px;
}
.diary {
position: relative;
float: left;
top: 0px;
padding: 5px;
background: none;
border: #291DFA 1px solid;
display:none;
width:100px;
min-height: 200px;
}
jQuery
$(function() {
var height = $(document).height();
var width = $(document).width();
var spanHeight = $('.popup').height();
var spanWidth = 500;
$('.pop-up-link').click(function() {
$(this).next()
.css({ "top" : height/2 - spanHeight/2 }) // Centre Pop Up
.css({ "left" : width/2 - spanWidth/2 })
.fadeIn(500);
});
$(".close").click(function () {
$('.pop-up-link').next().fadeOut(500);
});
});
解决方案
我重新编写了代码以允许弹出窗口成为主体标签的子级,以便我可以相对于主体定位它们。我正在使用 SimpleModels 并使用百分比定位 div。
jQuery(function ($) {
$('#menu .about').click(function (e) {
$('#about-content').modal({position: ["20%","65%"]});
return false;
});
});