js實現div彈出層執行個體代碼

來源:互聯網
上載者:User

這個不用多說了,直接貼代碼吧.代碼有注釋:

 代碼如下 複製代碼

/*
 * 彈出DIV層
*/
function showDiv()
{
var Idiv     = document.getElementById("Idiv");
var mou_head = document.getElementById('mou_head');
Idiv.style.display = "block";
//以下部分要將彈出層置中顯示
Idiv.style.left=(document.documentElement.clientWidth-Idiv.clientWidth)/2+document.documentElement.scrollLeft+"px";
Idiv.style.top =(document.documentElement.clientHeight-Idiv.clientHeight)/2+document.documentElement.scrollTop-50+"px";
 
 
//以下部分使整個頁面至灰不可點擊
var procbg = document.createElement("div"); //首先建立一個div
procbg.setAttribute("id","mybg"); //定義該div的id
procbg.style.background = "#000000";
procbg.style.width = "100%";
procbg.style.height = "100%";
procbg.style.position = "fixed";
procbg.style.top = "0";
procbg.style.left = "0";
procbg.style.zIndex = "500";
procbg.style.opacity = "0.6";
procbg.style.filter = "Alpha(opacity=70)";
//背景層加入頁面
document.body.appendChild(procbg);
document.body.style.overflow = "hidden"; //取消捲軸
 
//以下部分實現彈出層的拖拽效果
var posX;
var posY;
mou_head.onmousedown=function(e)
{
if(!e) e = window.event; //IE
posX = e.clientX - parseInt(Idiv.style.left);
posY = e.clientY - parseInt(Idiv.style.top);
document.onmousemove = mousemove;
}
document.onmouseup = function()
{
document.onmousemove = null;
}
function mousemove(ev)
{
if(ev==null) ev = window.event;//IE
Idiv.style.left = (ev.clientX - posX) + "px";
Idiv.style.top = (ev.clientY - posY) + "px";
}

}

function closeDiv() //關閉彈出層
{
var Idiv=document.getElementById("Idiv");
Idiv.style.display="none";
document.body.style.overflow = "auto"; //恢複頁面捲軸
var body = document.getElementsByTagName("body");
var mybg = document.getElementById("mybg");
body[0].removeChild(mybg);
}

<!--彈出層開始-->

 代碼如下 複製代碼

<div id="Idiv" style="display:none; position:absolute; z-index:1000; background:#67a3d9;">

    <div id="mou_head" style="width="100px; height=10px;z-index:1001; position:absolute;">這個是用來實現拖層</div>

    <input type="button" value="關閉" onclick="closeDiv();" />

</div>

<!--結束-->

至於一些美化效果,大家可以自己去修修改改了 ...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.