【轉】JS寫圖層方法.document.createElement("div")


<script language="javascript">
var docEle = function() {
return document.getElementById(arguments[0]) || false;
}
function openNewDiv(_id) {
var m = "mask";
if (docEle(_id)) document.removeChild(docEle(_id));
if (docEle(m)) document.removeChild(docEle(m));
// 新激活圖層
var newDiv = document.createElement("div");
newDiv.id = _id;
newDiv.style.position = "absolute";
newDiv.style.zIndex = "9999";
newDiv.style.width = "500px";
newDiv.style.height = "300px";
newDiv.style.top = "50px";
//newDiv.style.left = (parseInt(document.body.scrollWidth) - 300) / 2 + "px"; // 屏幕居中
newDiv.style.background = "#EFEFEF";
newDiv.style.border = "1px solid #860001";
newDiv.style.padding = "5px";
newDiv.innerHTML = "新激活圖層內容    ";
document.body.appendChild(newDiv);
// mask圖層
var newMask = document.createElement("div");
newMask.id = m;
newMask.style.position = "absolute";
newMask.style.zIndex = "1";
newMask.style.width = document.body.scrollWidth + "px";
newMask.style.height = document.body.scrollHeight + "px";
newMask.style.top = "0px";
newMask.style.left = "0px";
//newMask.style.background = "#000";
newMask.style.filter = "alpha(opacity=40)";
newMask.style.opacity = "0.40";
document.body.appendChild(newMask);

// 關閉mask和新圖層
var newA = document.createElement("a");
newA.href = "#";
newA.innerHTML = "關閉";
newA.onclick = function() {
   document.body.removeChild(docEle(_id));
   document.body.removeChild(docEle(m));
   return false;
}
newDiv.appendChild(newA);
}
</script>

<body>
<a href="#" οnclick="openNewDiv('newDiv');return false;">激活新層</a>
<p>網頁內容網頁內容網頁內容</p>
<p>網頁內容網頁內容網頁內容</p>
</body>

該文章轉載自網絡大本營:http://www.xrss.cn/Info/14120.Html




如果當前窗口含有滾動條,要讓生成的div居中顯示:

var w = jQuery(myAlert).innerWidth(),
		h = jQuery(myAlert).innerHeight();
	jQuery(myAlert).css({
		'position':'fixed',
		'top':'50%',
		'left':'50%',
		'margin-top':-(h/2),
		'margin-left':-(w/2)
	}).show();

myAlert是要彈出的div,fixed表示不隨滾動條滾動,w和h算出來的是div的高和寬

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章