博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js原生popup_js来实现popup的生成,带钟罩,可移动popup,点击body可自动消失
阅读量:5108 次
发布时间:2019-06-13

本文共 4017 字,大约阅读时间需要 13 分钟。

核心代码

var errorCode="111";

var errorMsg = "error";

var neirong = "

"

Error" +

"

"+

"

CodeMessage"+

"

"+ errorCode+ ""+ errorMsg+ ""+

"

"+

"

";

cwxbox.box.show(neirong);

function fnClose(){

cwxbox.box.hide();

}

var cwxbox = {};

cwxbox.box = function(){

var bg,wd,cn,ow,oh,o = true,time = null;

return {

show:function(c,t,w,h){

if(o){

bg = document.createElement('div'); bg.id = 'cwxBg';

wd = document.createElement('div'); wd.id = 'cwxWd';

cn = document.createElement('div'); cn.id = 'cwxCn';

document.body.appendChild(bg);

document.body.appendChild(wd);

wd.appendChild(cn);

bg.onclick = cwxbox.box.hide;

window.onresize = this.init;

window.onscroll = this.scrolls;

o = false;

}

if(w && h){

var inhtml = '';

}else{

var inhtml = c;

}

cn.innerHTML = inhtml;

oh = this.getCss(wd,'offsetHeight');

ow = this.getCss(wd,'offsetWidth');

this.init();

this.alpha(bg,50,1);

this.drag(wd);

if(t){

time = setTimeout(function(){cwxbox.box.hide()},t*1000);

}

},

hide:function(){

cwxbox.box.alpha(wd,0,-1);

clearTimeout(time);

},

init:function(){

bg.style.height = cwxbox.page.total(1)+'px';

bg.style.width = '';

bg.style.width = cwxbox.page.total(0)+'px';

var h = (cwxbox.page.height() - oh) /2;

wd.style.top=(h+cwxbox.page.top())+'px';

wd.style.left=(cwxbox.page.width() - ow)/2+'px';

},

scrolls:function(){

var h = (cwxbox.page.height() - oh) /2;

wd.style.top=(h+cwxbox.page.top())+'px';

},

alpha:function(e,a,d){

clearInterval(e.ai);

if(d==1){

e.style.opacity=0;

e.style.filter='alpha(opacity=0)';

e.style.display = 'block';

}

e.ai = setInterval(function(){cwxbox.box.ta(e,a,d)},40);

},

ta:function(e,a,d){

var anum = Math.round(e.style.opacity*100);

if(anum == a){

clearInterval(e.ai);

if(d == -1){

e.style.display = 'none';

if(e == wd){

this.alpha(bg,0,-1);

}

}else{

if(e == bg){

this.alpha(wd,100,1);

}

}

}else{

var n = Math.ceil((anum+((a-anum)*.5)));

n = n == 1 ? 0 : n;

e.style.opacity=n/100;

e.style.filter='alpha(opacity='+n+')';

}

},

getCss:function(e,n){

var e_style = e.currentStyle ? e.currentStyle : window.getComputedStyle(e,null);

if(e_style.display === 'none'){

var clonDom = e.cloneNode(true);

clonDom.style.cssText = 'position:absolute; display:block; top:-3000px;';

document.body.appendChild(clonDom);

var wh = clonDom[n];

clonDom.parentNode.removeChild(clonDom);

return wh;

}

return e[n];

},

drag:function(e){

var startX,startY,mouse;

mouse = {

mouseup:function(){

if(e.releaseCapture)

{

e.οnmοusemοve=null;

e.οnmοuseup=null;

e.releaseCapture();

}else{

document.removeEventListener("mousemove",mouse.mousemove,true);

document.removeEventListener("mouseup",mouse.mouseup,true);

}

},

mousemove:function(ev){

var oEvent = ev||event;

e.style.left = oEvent.clientX - startX + "px";

e.style.top = oEvent.clientY - startY + "px";

}

}

e.onmousedown = function(ev){

var oEvent = ev||event;

startX = oEvent.clientX - this.offsetLeft;

startY = oEvent.clientY - this.offsetTop;

if(e.setCapture)

{

e.οnmοusemοve= mouse.mousemove;

e.οnmοuseup= mouse.mouseup;

e.setCapture();

}else{

document.addEventListener("mousemove",mouse.mousemove,true);

document.addEventListener("mouseup",mouse.mouseup,true);

}

}

}

}

}()

cwxbox.page = function(){

return{

top:function(){return document.documentElement.scrollTop||document.body.scrollTop},

width:function(){return self.innerWidth||document.documentElement.clientWidth||document.body.clientWidth},

height:function(){return self.innerHeight||document.documentElement.clientHeight||document.body.clientHeight},

total:function(d){

var b=document.body, e=document.documentElement;

return d?Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight)):

Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))

}

}

}()

CSS控制popup的大小,可以根据需求来调节

#cwxBg{ position:absolute; display:none; background:#000; width:100%; height:100%; left:0px; top:0px; z-index:1000;}

#cwxWd{ position:absolute; display:none; border:3px solid #CCC; padding:10px;background:#FFF; z-index:1500;width:450px;height:350px;}

#cwxCn{ background:#FFF; display:block;}

转载地址:http://sqjdv.baihongyu.com/

你可能感兴趣的文章
[视频]K8飞刀 一键免杀 IE神洞网马教程
查看>>
正则表达式常用操作符
查看>>
Java环境变量的配置
查看>>
python基础 递归函数
查看>>
stm32 定时器与占空比
查看>>
C# CsvFile 类
查看>>
ECNU1328
查看>>
centos7 yum安装LAMP
查看>>
Logstash常用filter插件
查看>>
加减与有符号和无符号库
查看>>
关于FIR的modelsim
查看>>
Using Post-Form Trigger In Oracle Forms
查看>>
Redis 简介
查看>>
MySQL基本操作
查看>>
业界难题-“跨库分页”的四种方案
查看>>
CCLink
查看>>
asp.net Hessian 服务的注册
查看>>
IIS7.5上的服务的Update和Delete操作发生HTTP Error 405.0 - Method Not Allowed 解决方法
查看>>
(1.2)学习笔记之mysql体系结构(数据库文件)
查看>>
asp.net c#过滤html代码,净化DIV SPAN等
查看>>