javascript實(shí)現(xiàn)點(diǎn)擊按鈕彈出一個(gè)可關(guān)閉層窗口同時(shí)網(wǎng)頁(yè)背景變灰的方法
本文實(shí)例講述了javascript實(shí)現(xiàn)點(diǎn)擊按鈕彈出一個(gè)可關(guān)閉層窗口同時(shí)網(wǎng)頁(yè)背景變灰的方法。分享給大家供大家參考。具體分析如下:
這里點(diǎn)擊按鈕后,彈出一個(gè)可關(guān)閉的層窗口,隨之網(wǎng)頁(yè)背景變灰,在QQ網(wǎng)站上經(jīng)常會(huì)看到QQ登錄的效果,就和這個(gè)很類似,代碼段基于JavaScript,根據(jù)你的情況使用,有時(shí)候是用CSS完成的這種功能。
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>彈出一個(gè)層,頁(yè)面變灰</title> <script language="javascript"> function alertWin(title, msg, w, h){ var titleheight = "23px"; // 窗口標(biāo)題高度 var bordercolor = "#336699"; // 提示窗口的邊框顏色 var titlecolor = "#FFFFFF"; // 窗口標(biāo)題顏色 var titlebgcolor = "#336699"; // 窗口標(biāo)題背景色 var bgcolor = "#FFFFFF"; // 提示內(nèi)容的背景色 var iWidth = document.documentElement.clientWidth; var iHeight = document.documentElement.clientHeight; var bgObj = document.createElement("div"); bgObj.style.cssText = "position:absolute;left:0px;top:0px;width:"+iWidth+"px;height:"+Math.max(document.body.clientHeight, iHeight)+"px;filter:Alpha(Opacity=30);opacity:0.3;background-color:#000000;z-index:101;"; document.body.appendChild(bgObj); var msgObj=document.createElement("div"); msgObj.style.cssText = "position:absolute;font:11px '宋體';top:"+(iHeight-h)/2+"px;left:"+(iWidth-w)/2+"px;width:"+w+"px;height:"+h+"px;text-align:center;border:1px solid "+bordercolor+";background-color:"+bgcolor+";padding:1px;line-height:22px;z-index:102;"; document.body.appendChild(msgObj); var table = document.createElement("table"); msgObj.appendChild(table); table.style.cssText = "margin:0px;border:0px;padding:0px;"; table.cellSpacing = 0; var tr = table.insertRow(-1); var titleBar = tr.insertCell(-1); titleBar.style.cssText = "width:100%;height:"+titleheight+"px;text-align:left;padding:3px;margin:0px;font:bold 13px '宋體';color:"+titlecolor+";border:1px solid " + bordercolor + ";cursor:move;background-color:" + titlebgcolor; titleBar.style.paddingLeft = "10px"; titleBar.innerHTML = title; var moveX = 0; var moveY = 0; var moveTop = 0; var moveLeft = 0; var moveable = false; var docMouseMoveEvent = document.onmousemove; var docMouseUpEvent = document.onmouseup; titleBar.onmousedown = function() { var evt = getEvent(); moveable = true; moveX = evt.clientX; moveY = evt.clientY; moveTop = parseInt(msgObj.style.top); moveLeft = parseInt(msgObj.style.left); document.onmousemove = function() { if (moveable) { var evt = getEvent(); var x = moveLeft + evt.clientX - moveX; var y = moveTop + evt.clientY - moveY; if ( x > 0 &&( x + w < iWidth) && y > 0 && (y + h < iHeight) ) { msgObj.style.left = x + "px"; msgObj.style.top = y + "px"; } } }; document.onmouseup = function () { if (moveable) { document.onmousemove = docMouseMoveEvent; document.onmouseup = docMouseUpEvent; moveable = false; moveX = 0; moveY = 0; moveTop = 0; moveLeft = 0; } }; } var closeBtn = tr.insertCell(-1); closeBtn.style.cssText = "cursor:pointer; padding:2px;background-color:" + titlebgcolor; closeBtn.innerHTML = "<span style='font-size:15pt; color:"+titlecolor+";'>×</span>"; closeBtn.onclick = function(){ document.body.removeChild(bgObj); document.body.removeChild(msgObj); } var msgBox = table.insertRow(-1).insertCell(-1); msgBox.style.cssText = "font:10pt '宋體';"; msgBox.colSpan = 2; msgBox.innerHTML = msg; // 獲得Event對(duì)象,用于兼容IE和FireFox function getEvent() { return window.event || arguments.callee.caller.arguments[0]; } } </script> </head> <body> <input type="button" value="點(diǎn)這里" onclick="alertWin('點(diǎn)擊彈出可關(guān)閉的層窗口,網(wǎng)頁(yè)變灰',290,208);" /> </body> </html>
希望本文所述對(duì)大家的javascript程序設(shè)計(jì)有所幫助。
相關(guān)文章
一文帶你掌握J(rèn)avaScript中的EventLoop機(jī)制
JavaScript是?單線程、非阻塞?的,它通過(guò)事件隊(duì)列?(Event?Loop)?的方式來(lái)實(shí)現(xiàn)異步回調(diào),所以本文小編就帶大家來(lái)深入了解一下JavaScript中的EventLoop機(jī)制,需要的可以了解下2024-02-02小程序云開(kāi)發(fā)實(shí)戰(zhàn)小結(jié)
這篇文章主要介紹了小程序云開(kāi)發(fā)實(shí)戰(zhàn)小結(jié),本文詳細(xì)的介紹了云開(kāi)發(fā)以及環(huán)境的搭建和項(xiàng)目實(shí)踐,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10JS小數(shù)運(yùn)算出現(xiàn)多為小數(shù)問(wèn)題的解決方法
這篇文章主要介紹了JS小數(shù)運(yùn)算出現(xiàn)多為小數(shù)問(wèn)題的解決方法,需要的朋友可以參考下2016-06-06JS常見(jiàn)DOM節(jié)點(diǎn)操作示例【創(chuàng)建 ,插入,刪除,復(fù)制,查找】
這篇文章主要介紹了JS常見(jiàn)DOM節(jié)點(diǎn)操作,結(jié)合實(shí)例形式分析了JavaScript針對(duì)DOM節(jié)點(diǎn)的創(chuàng)建 ,插入,刪除,復(fù)制,查找等操作相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2018-05-05js parseInt("08")未指定進(jìn)位制問(wèn)題
今天在做JS關(guān)于月份的判斷,對(duì)于parseInt("01")到parseInt("07");都能得到正確的結(jié)果,但如果是parseInt("08")或parseInt("09")則返回0,首先看parseInt語(yǔ)法:parseInt(string, radix);2010-06-06簡(jiǎn)單了解微信小程序 e.target與e.currentTarget的不同
這篇文章主要介紹了微信小程序 e.target與e.currentTarget的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09