js写弹窗

Strive-count      2022-02-09     543

关键词:

1.先来看弹窗的模样

点击“弹出窗口”后会弹出下面窗口

 

2.下面是实现弹出窗口的代码,其中引入的jquery一般自己有,没有的话可以从网上下载。tanchuang.js和tanchuang.css写在了下面。

 弹窗的一些参数可以参考tanchuang.js里面的,需要更改的拿到显示页面来修改。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript" src="jquery-1.11.2.min.js">
</script>
<script type="text/javascript" src="tanchuang.js">
</script>
<link href="tanchuang.css" rel="stylesheet" type="text/css" />
<style type="text/css">
*{
    margin: 0px auto;
}
</style>
</head>
<body style="background-color:#999">
<div style="width:200px; margin-top:10px">
<input type="button" value="弹出窗口" id="btntc" style="width:100px; height:30px; font-size:18px;" />
</div>
<div id="tc"></div><!--弹窗容器-->
</body>
<script type="text/javascript">
$(document).ready(function(e) {
    $('#btntc').click(function(){
            var html = "<div style='color:red'>这是测试的弹窗</div>";
            var button ="<input type='button' value='确定' /><input type='button' value='取消' />";
            var win = new Window({//弹窗的一些配置参数
                width : 800, //宽度
                height : 500, //高度
                title : '测试弹窗12345', //标题
                content : html, //内容
                isMask : false, //是否遮罩
                buttons : button, //按钮
                isDrag:true, //是否移动
                });
        })
});
</script>
</html>

 

3.tanchuang.js

// 每个弹窗的标识
var x =0;
var idzt = new Array();

var Window = function(config){
    
    //ID不重复
    idzt[x] = "zhuti"+x;  //弹窗ID
    
    //初始化,接收参数
    this.config = {
        width : config.width || 300, //宽度
        height : config.height || 200, //高度
        buttons : config.buttons || '', //默认无按钮
        title : config.title || '标题', //标题
        content : config.content || '内容', //内容
        isMask : config.isMask == false?false:config.isMask || true, //是否遮罩
        isDrag : config.isDrag == false?false:config.isDrag || true, //是否移动
        };
    
    //加载弹出窗口
    var w = ($(window).width()-this.config.width)/2;
    var h = ($(window).height()-this.config.height)/2;
    
    var nr = "<div class='zhuti' id='"+idzt[x]+"' bs='"+x+"' style='width:"+this.config.width+"px; height:"+this.config.height+"px; background-color:white; left:"+w+"px; top:"+h+"px;'></div>";
    $("body").append(nr);
    
    //加载弹窗标题
    var content ="<div id='title"+x+"' class='title' bs='"+x+"'>"+this.config.title+"<div id='close"+x+"' class='close' bs='"+x+"'>×</div></div>";
    //加载弹窗内容
    var nrh = this.config.height - 75;
    content = content+"<div id='content"+x+"' bs='"+x+"' class='content' style='width:100%; height:"+nrh+"px;'>"+this.config.content+"</div>";
    //加载按钮
    content = content+"<div id='btnx"+x+"' bs='"+x+"' class='btnx'>"+this.config.buttons+"</div>";
    
    //将标题、内容及按钮添加进窗口
    $('#'+idzt[x]).html(content);
    
    //创建遮罩层
    if(this.config.isMask)
    {
        var zz = "<div id='zz'></div>";
        $("body").append(zz);
        $("#zz").css('display','block');
    }
    
    //最大最小限制,以免移动到页面外
    var maxX = $(window).width()-this.config.width;
    var maxY = $(window).height()-this.config.height;
    var minX = 0,
        minY = 0;
    
    //窗口移动
    if(this.config.isDrag)
    {
        //鼠标移动弹出窗
        $(".title").bind("mousedown",function(e){
                
                var n = this.getAttribute("bs"); //取标识
                
                //使选中的到最上层
                $(".zhuti").css("z-index",3);
                $('#'+idzt[n]).css("z-index",4);
                
                //取初始坐标
                var endX = 0, //移动后X坐标
                    endY = 0, //移动后Y坐标
                    startX = parseInt($('#'+idzt[n]).css("left")), //弹出层的初始X坐标
                    startY = parseInt($('#'+idzt[n]).css("top")), //弹出层的初始Y坐标
                    downX = e.clientX, //鼠标按下时,鼠标的X坐标
                    downY = e.clientY; //鼠标按下时,鼠标的Y坐标
                    
                //绑定鼠标移动事件
                $("body").bind("mousemove",function(es){
                    
                    endX = es.clientX - downX + startX; //X坐标移动
                    endY = es.clientY - downY + startY; //Y坐标移动
                    
                    //最大最小限制
                    if(endX > maxX)
                    {
                        endX = maxX;
                    } else if(endX < 0)
                    {
                        endX = 0;
                    }
                    if(endY > maxY)
                    {
                        endY = maxY;
                    } else if(endY < 0)
                    {
                        endY = 0;
                    }
                    
                    $('#'+idzt[n]).css("top",endY+"px");
                    $('#'+idzt[n]).css("left",endX+"px");
                    
                    window.getSelection ? window.getSelection().removeAllRanges():document.selection.empty(); //取消选中文本
                    
                    });
            });
        //鼠标按键抬起,释放移动事件
        $("body").bind("mouseup",function(){
            
                $("body").unbind("mousemove");
            
            });
    }
    
    //关闭窗口
    $(".close").click(function(){
        
            var m = this.getAttribute("bs"); //找标识
            $('#'+idzt[m]).remove(); //移除弹窗
            $('#zz').remove(); //移除遮罩 
        
        })
        
        x++;  //标识增加
        
}

 

4.tanchuang.css

.zhuti
{
    position:absolute;
    z-index:3;
    font-size:14px;
    border-radius:5px;
    box-shadow:0 0 5px white;
    overflow:hidden;
    color:#333;
}
.title
{
    background-color:#3498db;
    vertical-align:middle;
    height:35px;
    width:100%;
    line-height:35px;
    text-indent:1em;
}
.close{
    float:right;
    width:35px;
    height:35px;
    font-weight:bold;
    line-height:35px;
    vertical-align:middle;
    color:white;
    font-size:18px;
    }
.close:hover
{
    cursor:pointer;
}
.content
{
    text-indent:1em;
    padding-top:10px;
}
.btnx
{
    height:30px;
    width:100%;
    text-indent:1em;
}
.btn
{
    height:28px;
    width:80px;
    float:left;
    margin-left:20px;
    color:#333;
}
#zz
{
    width:100%;
    height:100%;
    opacity:0.15;
    display:none;
    background-color:#ccc;
    z-index:2;
    position:absolute;
    top:0px;
    left:0px;
}

 

写弹窗时的一点发现

弹窗是很常见的网页特效,其组成大概分为两部分,其中主要的部分是位于页面中间的弹窗内容,另一部分是遮住页面的背景色。其实之前是有写过弹窗特效的。以为可以三几下把他拿下。写出来之后却发现是这样的。。我一看... 查看详情

iframe在顶级页面弹窗并交互

...页面,通过Portal将子组件渲染到该元素,在iframe内部可以写弹窗内的布局,在window.top添加样式和方法,在找到对应的元素设置监听onclick/oninput方法,然后就可以操作mobx数据,使顶级页面和iframe进行交互。 查看详情

jq弹窗制作相关问题

...实现在弹窗上点击按钮再弹出一个窗口。参考技术A自己写弹框呗,想弹多少弹多少自己写个简单的需要弹框显示的放在页面中,需要弹出的时候定位显示即可,如有需要可加一个遮罩层 参考技术B思路是这样的:自己写一个DIV,... 查看详情

用css怎么写弹出广告代码,js也可以,求大神

其实蛮简单的,你理解了他的原理就会了,简单说一下吧,首先这个广告正常显示同样不影响网页其他的内容的布局,那么他应该是突出来的对吧?这个可以用相对文档的绝对定位或者相对浏览窗口的固定定位,这个会把?然后... 查看详情

js弹窗

除了alert弹窗,js还有confirm弹窗和prompt弹窗vara=confirm("1111");alert(a)弹出确认弹窗,有确认,取消按钮,确认返回true,取消返回falsevarb=prompt("请输入姓名","");alert(b)弹出输入框,一个是输入框的提示文字,一个是默认内容,点击确... 查看详情

js无线弹窗

无限弹窗functionfad(action){alert(‘1‘);ht=setTimeout("fad()",30)}fad();fad()能放在$().ready()里面,效果也一样 查看详情

如何用写弹出层----2017-03-29

<!DOCTYPEhtml><html><head><metacharset="utf-8"><title></title><style>*{margin:0px;padding:0px;}.mask{width:100%;/*height:800px;*/z-index:998;background-color: 查看详情

js弹窗

varhtml="<divstyle=‘color:red‘>这是测试的弹窗</div>";varbutton="<inputtype=‘button‘value=‘确定‘/><inputtype=‘button‘value=‘取消‘/>";varwin=newWindow({width:500,//宽度height:400,//高度title:‘测 查看详情

js弹窗输入

<html><head><title>js输入对话框</title></head><body><scriptlanguage="javascript"><!--age=prompt("请输入你的年龄:","20");if(age!=null){alert("你今年"+age+"岁了!");}else{aler 查看详情

利用js实现popup弹窗

简单实现一个通过一个网页,点击生成一个弹窗,然后点击保存之后自动关闭弹窗的功能。 首先在settings文件中写上两条对应的路由关系。1urlpatterns=[2url(r‘^p1/‘,p1),3url(r‘^p2/‘,p2),4]我们可以肯定的是我们需要一个网页,然... 查看详情

vue2我自己写的弹窗组件,弹窗在页面里显示后,原js文件失效,怎么回事?

vue2我自己写的弹窗组件,弹窗在页面里显示后,原JS文件失效,怎么回事?为啥只能在弹窗的vue文件写JS呢,不能在公共JS里写呢参考技术A框架冲突。(jQ和vue冲突)本回答被提问者采纳 查看详情

js弹窗,父子窗口调用

http://www.runoob.com/jsref/met-win-open.htmlfunctionopenwindow(url,name,iWidth,iHeight){varurl;//转向网页的地址;varname;//网页名称,可为空;variWidth;//弹出窗口的宽度;variHeight;//弹出窗口的高度;//window.screen.height获得屏幕的高,windo 查看详情

耗子大叔弹窗来自百度搜索引擎导流的弹窗js源码赏析(代码片段)

...发布了一段JS代码 当请求来自百度导流过来的链接将弹窗告知警示,下面是那段弹窗JS源码 ,技术人还是关注技术细节,分享给大家:   <scriptsrc="http:// 查看详情

js带有遮罩的弹窗

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>js制作带有遮罩弹出层实现登录小窗口</title><linktype="text/css"rel="stylesheet"href="./css/reset.css"/><scrip 查看详情

js弹窗数据带回

父窗口代码:functionselectCar_Team_Info(){//varurl="<%=basepath_%>ec/jsp/carLoading/carTeamPopInfo.jsp";//varobj=window.showModalDialog(url,null,"dialogWidth=600px;dialogHeight=400px;resizable=yes");w 查看详情

可拖动弹窗的js和jquery两种写法

...现的演示视频,这个demo中的例子是百度首页登录按钮的弹窗,如下图:      当点击左上角的登录按钮时,弹窗出现并自动居中,同时窗口可拖动的范围被限制在了可视区域内,即浏览器视窗的上下左右边... 查看详情

auto.js问题?

autojs中怎么判断app内点击后出现的弹窗是不是我想要出现的内容,对app弹窗信息进行判断的代码,新手1、如果你清楚想要的弹窗内有什么文字,可以用text("你想要的内容”).exists()判断文字是否存在,存在那么弹窗正确。2... 查看详情

78js插件:原生版弹窗(面对对象)

```html:run<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>弹窗</title><style>.dialog{background:grey;width:150px;height:30px;font-size:25px;font 查看详情