This article mainly introduces the simple implementation code of jQuery pop-up window in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.
Today I talked about the composition and usage of Jquery pop-up window:
First write the code of the reference file:
// 每个弹窗的标识 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 = "<p 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;'></p>"; $("body").append(nr); //加载弹窗标题 var content ="<p id='title"+x+"' class='title' bs='"+x+"'>"+this.config.title+"<p id='close"+x+"' class='close' bs='"+x+"'>×</p></p>"; //加载弹窗内容 var nrh = this.config.height - 75; content = content+"<p id='content"+x+"' bs='"+x+"' class='content' style='width:100%; height:"+nrh+"px;'>"+this.config.content+"</p>"; //加载按钮 content = content+"<p id='btnx"+x+"' bs='"+x+"' class='btnx'>"+this.config.buttons+"</p>"; //将标题、内容及按钮添加进窗口 $('#'+idzt[x]).html(content); //创建遮罩层 if(this.config.isMask) { var zz = "<p id='zz'></p>"; $("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).attr("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++; //标识增加 }
This JS file puts the pop-up window The content, style, position, button, and mask layer have all been processed. Take a good look at the code inside before quoting. It is best to understand it. Detailed comments are also made in it. I hope it can help you.
The following is the CSS style sheet:
.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; }
This style sheet has written each tag and the required style, which can save the amount of code on the main page and make the main page It looks very neat. If you want to change it, you only need to modify it in the CSS style sheet. Note: No matter what file you want to reference, the Jquery file must be placed at the front! ! !
The following is the main page code:
<!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="external nofollow" rel="stylesheet" type="text/css" /> <style type="text/css"> *{ margin: 0px auto; } </style> </head> <body style="background-color:#999"> <p style="width:200px; margin-top:10px"> <input type="button" value="弹出窗口" id="btntc" style="width:100px; height:30px; font-size:18px;" /> </p> </body> <script type="text/javascript"> $(document).ready(function(e) { $('#btntc').click(function(){ var html = "<p style='color:red'>这是测试的弹窗</p>"; var button ="<input type='button' value='确定' /><input type='button' value='取消' />"; var win = new Window({ width : 400, //宽度 height : 300, //高度 title : '测试弹窗', //标题 content : html, //内容 isMask : false, //是否遮罩 buttons : button, //按钮 isDrag:true, //是否移动 }); }) }); </script> </html>
Similarly, detailed comments are also added to the main page to facilitate future understanding. I hope it can help myself and everyone. Let’s take a look at the effect:
The effect after clicking on the pop-up window:
Detailed explanation on how to add pop-up window information to Dreamweaver webpage
javascript, html5, css3 custom pop-up window
Comprehensive use and techniques of JS pop-up windows
The above is the detailed content of jQuery implementation of simple pop-up window example. For more information, please follow other related articles on the PHP Chinese website!

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
