js確認(rèn)刪除對話框效果的示例代碼

css文件delcss.css
代碼如下:
*{ margin:0; padding:0;}
#div1{ width:300px; height:100px; border-radius:10px; background:#f60; box-shadow:5px 5px 10px #ccc; position:absolute; left:50%; margin-left:-150px; z-index:2; opacity:0; filter:alpha(opacity:0); display:none;}
#div1 h3{ height:20px; background:#60f;}
#overlay{ width:100%; height:100%; background:#ccc; position:absolute; left:0; top:0; opacity:0; z-index:1; display:none;}
#closeConfirm{ width:80px; height:20px; line-height:20px; background:#ccc; border-radius:5px; display:block; text-decoration:none; color:#000; text-align:center; position:absolute; bottom:10px; right:10px;}
#trueConfirm{width:80px; height:20px; line-height:20px; background:#ccc; border-radius:5px; display:block; text-decoration:none; color:#000; text-align:center; position:absolute; bottom:10px; right:100px;}
#confirmCon{ text-align:center; color:#ccc; height:40px; line-height:40px;}
js文件confirmPop.js
代碼如下:
function ConfirmPop(delObj,confirmMain,overlay,binkTitle,closeConfirm,trueConfirm){
this.oDelObj=document.getElementById(delObj);
this.oDiv=document.getElementById(confirmMain);
this.oOverlay=document.getElementById(overlay);
this.oTitle=document.getElementById(binkTitle);
this.oCloseConfirm=document.getElementById(closeConfirm);
this.oTrueConfirm=document.getElementById(trueConfirm);
this.oConStart=0;
this.oConItarget=0;
this.init();
}
ConfirmPop.prototype={
init:function(){
this.oConStart=parseInt((document.documentElement.clientHeight-this.oDiv.offsetHeight)/2-100);
this.oConItarget=parseInt((document.documentElement.clientHeight-this.oDiv.offsetHeight)/2-70);
this.oDiv.style.top=this.oConStart+'px';
document.title=this.oConStart+', '+this.oConItarget;
var _this=this;
this.oDelObj.onclick=function(){
_this.confirmShow();
}
this.oOverlay.onclick=function(){
_this.titleBink();
}
this.oCloseConfirm.onclick=this.oTrueConfirm.onclick=function(){
_this.confirmHide();
}
},
confirmShow:function(){
this.oOverlay.style.display='block';
this.oDiv.style.display='block';
striveMove(this.oOverlay,{ opacity:30});
striveMove(this.oDiv,{top:this.oConItarget, opacity:100})
},
titleBink:function(){
var _this=this;
var iLightTimer=null;
var i=0;
iLightTimer=setInterval(function(){
if(i%2){
_this.oTitle.style.background='#60f';
}else{
_this.oTitle.style.background='#ccc';
}
i++;
if(i>5){
clearInterval(iLightTimer);
}
},50)
},
confirmHide:function(){
striveMove(this.oOverlay,{opacity:0});
this.oOverlay.style.display='none';
striveMove(this.oDiv,{top:this.oConStart, opacity:0});
}
}
function striveMove(obj,json,fn){clearInterval(obj.iTimer);obj.iTimer=setInterval(function(){var bStop=true;for(var attr in json){var iCur=0;if(attr=='opacity'){iCur=Math.round(parseFloat(getStyle(obj,attr))*100);}else{iCur=parseInt(getStyle(obj,attr));}var iSpeed=(json[attr]-iCur)/8;iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);if(attr=='opacity'){obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';obj.style.opacity=(iCur+iSpeed)/100;}else{obj.style[attr]=iCur+iSpeed+'px';}if(iCur!==json[attr]){bStop=false;}}if(bStop){clearInterval(obj.iTimer);if(fn){fn();}}},30)}function getStyle(obj,attr){return obj.currentStyle?obj.currentStyle[attr]:getComputedStyle(obj,false)[attr];}
使用方法
1.引入文件
<link rel="stylesheet" type="text/css" href="delcss.css"/>
<script src="confirmPop.js"></script>
2.調(diào)用方法
<script>
window.onload=window.onresize=function(){
new ConfirmPop('btn','div1','overlay','title','closeConfirm','trueConfirm');
}
</script>
3.做好準(zhǔn)備工作
<div id="div1">
<h3 id="title"></h3>
<div id="confirmCon">
您確定要進(jìn)行刪除操作么?
</div>
<a href="###" id="trueConfirm">確定</a>
<a href="###" id="closeConfirm">取消</a>
</div>
<div id="overlay"></div>
這段代碼是提供空間的,自己寫的div代碼。其中確定框中的a標(biāo)簽中,可以把刪除的地址填入其中。點(diǎn)擊確定時(shí),就跳轉(zhuǎn)到刪除的頁面方法中了。
這段代碼可以放入任何位置。不占空間。作者很犀利。
4.導(dǎo)火線,觸發(fā)器
<a id="btn" ><img src="/images/default/right/ico_del.gif" /></a>
將刪除圖標(biāo)加上id="btn",這樣就會觸發(fā)刪除效果了。
如果想修改樣式,可以到css中進(jìn)行修改,調(diào)整。
小結(jié):這樣的js刪除就比瀏覽器自帶的彈出框美觀一些了。作者用短短3k的js就寫出這種效果,真心厲害。
ps,我只是拿來的。
缺點(diǎn),只能針對一個(gè)內(nèi)容,進(jìn)行刪除。因?yàn)樗械亩际轻槍d的,單一化了。
相關(guān)文章
淺談?lì)愃朴?function(){}).call()的js語句
這篇文章主要介紹了淺談?lì)愃朴?function(){}).call()的js語句,的相關(guān)資料,需要的朋友可以參考下2015-03-03JavaScript實(shí)現(xiàn)給定時(shí)間相加天數(shù)的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)給定時(shí)間相加天數(shù)的方法,涉及JavaScript針對日期與時(shí)間操作相關(guān)技巧,需要的朋友可以參考下2016-01-01微信小程序?qū)崿F(xiàn)注冊登錄功能(表單校驗(yàn)、錯(cuò)誤提示)
這篇文章主要介紹了微信小程序 實(shí)現(xiàn)注冊、登錄功能(表單校驗(yàn)、錯(cuò)誤提示),本文通過代碼給大家詳細(xì)介紹,需要的朋友可以參考下2019-12-12利用Javascript判斷操作系統(tǒng)的類型實(shí)現(xiàn)不同操作系統(tǒng)下的兼容性
在通過Javascript實(shí)現(xiàn)客戶端和服務(wù)端的交互時(shí),有時(shí)候需要對操作系統(tǒng)進(jìn)行判斷,以便實(shí)現(xiàn)不同操作系統(tǒng)下的兼容性;從而實(shí)現(xiàn)網(wǎng)站在跨平臺瀏覽時(shí)候保持良好的用戶體驗(yàn),感興趣的朋友可以了解下啊,或許對你有所幫助2013-01-01微信小程序?qū)崿F(xiàn)組件頂端固定或底端固定效果(不隨滾動而滾動)
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)組件頂端固定或底端固定效果(不隨滾動而滾動),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04JS高級調(diào)試技巧:捕獲和分析 JavaScript Error詳解
前端工程師都知道 JavaScript 有基本的異常處理能力。我們可以 throw new Error(),瀏覽器也會在我們調(diào)用 API 出錯(cuò)時(shí)拋出異常。但估計(jì)絕大多數(shù)前端工程師都沒考慮過收集這些異常信息2014-03-03