我有以下的jQuery代碼jQuery代碼運行時,第二次沒有得到相同的結果
$(function(){
function darkBox(div){
var w = (div.attr('width')) ? div.attr('width') : div.width();
var h = (div.attr('height')) ? div.attr('height') : div.height();
var box = $('<div></div>').addClass('darkCover');
$('body').prepend(box);
box.fadeTo('fast', 0.8);
$(this).keydown(function(e){
if(e.keyCode == 27){
box.hide();
contentBox.hide();
}
});
var contentBox = $('<div></div>').html(div.html());
contentBox.addClass('darkContent');
var x = $(window).width()/2;
var y = $(window).height()/2;
var startW = h-y/2;
var startH = w-x/2;
var endTop = y - h/2;
var endLeft = x - w/2;
contentBox.css("left", x+"px");
contentBox.css("top", startW+"px");
contentBox.css("z-index", "910");
contentBox.css("width", w+"px");
contentBox.css("height", h+"px");
$('body').prepend(contentBox);
contentBox.animate({
opacity: 1,
width:w+"px",
height:h+"px",
top:endTop+"px",
left:endLeft+"px"
}, 1000, "easeOutExpo");
}
$('.darkBox').each(function(){
var div = $($(this).attr('data-target'));
div.hide();
$(this).click(function(){
darkBox(div);
});
});
});
編輯 HTML:
<a href="javascript:;" data-target="#useThisDiv1" class="btn blue btn-xs darkBox">Show Box1</a>
<div id="useThisDiv1" width="500" height="500">
<h3 class="breadcrumb">Div1</h3>
<table>
<tr>
<td>
array index and a corresponding array value each time. (The value can also be accessed through the this keyword,
</td>
</tr>
<tr>
<td>
array index and a corresponding array value each time. (The value can also be accessed through the this keyword,
</td>
</tr>
<tr>
<td>
array index and a corresponding array value each time. (The value can also be accessed through the this keyword,
</td>
</tr>
</table>
</div>
.darkContent{
position: fixed;
background-color: white;
border: 5px solid black;
padding: 8px;
overflow: hidden;
color: #333;
font-family: arial;
}
.darkCover{
position: fixed;
left: 0px;
top: 0px;
z-index: 900;
background-color: black;
opacity: 0;
width: 100%;
height: 100%;
}
編輯完
上面的代碼,如果我第一次點擊它,我得到了80%的不透明度的黑色背景,但如果我再次點擊它,我會得到白色背景。我會告訴你這兩個屏幕截圖
圖像的不透明背景
圖像的白色背景
併成爲珍貴的代碼,一旦我猜
下面的代碼只執行var box = $('<div></div>').addClass('darkCover');
$('body').prepend(box);
box.fadeTo('fast', 0.8);
我可以運行上面的代碼,每次我點擊按鈕打開彈出窗口...任何建議嗎?
Regards
隱藏它後刪除
?或者你只是預先安裝新的? – 2014-09-25 14:33:35此代碼之外沒有任何事情發生,有一個錨標記和一對div標記與一個唯一標識 – Sharif 2014-09-25 14:35:35
您可以發佈一些示例HTML代碼以使用darkBox函數嗎? – 2014-09-25 14:41:59