標題使聽起來很容易,但我保證它不是。我有一個網站http://sarahnwatson.com。我已經通過點擊關於和聯繫標籤打開了模式窗口。選項卡全部位於左側的無序列表中。 Modal窗口直接編碼在應該打開它的標籤下。jQuery和CSS位置元素在屏幕中心
我似乎無法弄清楚的是如何在我的jQuery代碼和我的CSS代碼之間定位不同大小的模態窗口在屏幕中心。因爲它們在位於左側的元素內,所以他們試圖留在那個元素中。
下面是標籤的CSS和模態窗口:
ul.st_navigation{
position:relative;
width:100%;
top:140px;
left:-300px;
list-style:none;
}
ul.st_navigation li {
float:left;
clear:both;
margin-bottom:8px;
position:relative;
width:100%;
}
ul.st_navigation li span.st_link{
background: rgba(0,0,0,.8);
float:left;
position:relative;
line-height:50px;
padding:0px 20px;
-moz-box-shadow:0px 0px 2px #000;
-webkit-box-shadow:0px 0px 2px #000;
box-shadow:0px 0px 2px #000;
}
ul.st_navigation li span.st_arrow_down,
ul.st_navigation li span.st_arrow_up{
position:absolute;
margin-left:20px;
width:40px;
height:50px;
cursor:pointer;
-moz-box-shadow:0px 0px 2px #000;
-webkit-box-shadow:0px 0px 2px #000;
box-shadow:0px 0px 2px #000;
}
ul.st_navigation li span.st_arrow_down{
background: rgba(0,0,0,.8) url(../images/icons/down.png) no-repeat center center;
}
ul.st_navigation li span.st_arrow_up{
background: rgba(0,0,0,.8) url(../images/icons/up.png) no-repeat center center;
}
ul.st_navigation li span.st_modal{
position:absolute;
margin-left:20px;
width:40px;
height:50px;
cursor:pointer;
-moz-box-shadow:0px 0px 2px #000;
-webkit-box-shadow:0px 0px 2px #000;
box-shadow:0px 0px 2px #000;
background: rgba(0,0,0,.8) url(../images/icons/modal.png) no-repeat center center;
}
這裏是jQuery的:
// Create a modal window
function createModal(elm) {
var $this = $(this);
var $body = $('body');
var winHeight = $(window).height();
var winWidth = $(window).width();
$body.prepend('<div id="blackout">');
$("#blackout").fadeIn(1000, function() {
$(elm).css({ left: '-9999px', display: 'block' });
var modalHeight = $(elm).height();
var modalWidth = $(elm).width();
var offsetH1 = winHeight/2;
var offsetH2 = (winHeight-modalHeight)/2;
var offsetW = (winWidth-modalWidth)/2;
$(elm)
.css({ top:offsetH1, left:offsetW, height:'0px' })
.animate({ top:offsetH2, height:modalHeight });
});
});
});
}
$list.find('.st_modal').live('click',function(){
var $this = $(this);
hideThumbs();
$this.closest('li').animate({ left: "0px" }, function() {
var $elem = $this.parent().next('div#modal_window');
createModal($elem);
});
});
的HTML結構如下:
<ul id="st_nav" class="st_navigation">
<li>
<span class="st_link">About<span class="st_modal"></span></span>
<div id="modal_window" style="width:600px;">
<div class="about">
<span class="line"><span class="left">Name: </span>Sarah Watson</span>
<span class="line"><span class="left">Age: </span>16</span>
<span class="line"><span class="left">Genre: </span>Folk, Pop, Alternative</span>
<span class="line"><span class="left">Bio: </span></span><br />
I am 16yrs old and an aspiring singer/songwriter. I live in Northern California. My goals and ambitions in life are to someday have a popular song on the radio, whether I am singing it or someone else is. I love to hear just about any kind of music, learning something new is always fun!
</div>
</div>
</li>
<li>
<span class="st_link">Contact<span class="st_modal"></span></span>
<div id="modal_window" style="width: 450px;">
<h2>Contact</h2>
<form id="contact_form" method="POST" action="#">
<label>Name:</label><br />
<input type="text" name="name" /><br />
<label>Email:</label><br />
<input type="text" name="email" /><br />
<label>Reason:</label>
<label><input type="radio" name="reason" value="praise" checked='checked' /> Praise</label> <label><input type="radio" name="reason" value="booking" /> Booking</label><br />
<label>Message:</label><br />
<textarea name="name"></textarea><br />
<input type="submit" value="Submit" name="submit" /> <span class="status_message"></span>
</form>
</div>
</li>
</ul>
把它全部放入一個jsFiddle演示將有助於每個人。 – Sparky
只是爲了指出你在正確的方向,設置你的CSS位置:固定,左:50%,頂部:50%,並計算一個負的頂部和左邊距,分別是彈出窗口高度和寬度的一半。 – lsuarez
我試過了jsFiddle。這個網站太複雜了。謝謝@lthibodeaux,我會嘗試。你的意思是將#modal_window設置爲這些規範的權利? – watzon