Jquery implements pop-up layer plug-in_jquery
There are still many applications for pop-up layers, such as login and some operations on the same page. Others' belongings belong to others, and your own belongs to you, so I have always wanted to write a pop-up layer plug-in. Without further ado, let’s get started!
1: Mask layer
To pop up a layer, you must first use a mask layer to block the page below. This mask layer is full screen and the page needs to scroll, so set position: fixed; it also has a transparency effect. Here is my definition The mask layer css is named mask
.mask
{
position: fixed;
width: 100%;
height: 100%;
background-color: white;
Overflow: scroll;
Filter: alpha(opacity=50);
-moz-opacity: 0.5;
Opacity: 0.5;
z-index: 20;
Overflow: auto;
top: 0px;
Right: 0px;
}
2: Main parameters of the plug-in
tag: Why do you need tag? You can use tags to specify hidden elements that need to be popped up. You can specify tags as the selector "#*", so that the specified elements can be popped up. Here I set the default to this.
mainContent: Is this parameter required? I don't think it's very useful. I set it mainly for server controls. If all are added to the body, the form cannot be submitted. But after clicking submit, the page will refresh and the pop-up layer disappears, so I think it is still useless...
$.fn.xsPop = function (options) {
var defaults = {//Default value
title: "Pop-up window", //Window title
width: 500,
Heigth: 400,
tag: this, //Pop up the elements that need to be loaded
close: "Close",
mainContent: "body"//Container, in order to submit the form, but submit will refresh the page...
};
var options = $.extend(defaults, options); //Override with passed parameters
This.each(function () {
xsMain(options.title, options.width, options.heigth, options.tag, options.close, options.mainContent); //The main entrance of the plug-in
});
};
3: Use the xsMain function to add elements and bind events
There is a process here, which is to control the height and width. If the setting exceeds the screen height and width, it will adapt to the screen, thus preventing the pop-up layer from being too large to operate. The other is to add html normally, I use string addition
//According to the incoming data, add a mask layer and pop up a prompt box
Function xsMain(title, width, height, tag, close, mainContent) {
var divmask = "";
$(mainContent).append(divmask);
var xsPop1 = "
var xsPop2 = " " title " " close "";
var xsPop3 = " var xsPop5 = "
var allHtml = xsPop1 xsPop2 xsPop3 xsPop5;
$(mainContent).append(allHtml);
$(tag).show();
$(tag).appendTo("#xsPopMain");
//Get the height and width of the browser and make subsequent judgments (height exceeds, dragging border limit)
clientHeight = window.screen.height;
clientWidth = window.screen.width;
If (height > clientHeight) {
height = clientHeight - 100;
}
If (width > clientWidth) {
width = clientWidth - 100;
}
$("#xsPop").css({
"heigth": height "px",
"width": width "px",
"margin-top": "-" (height / 2) "px",
"margin-left": "-" (width / 2) "px"
});
$("#xsPopMain").css("height", height - $("#xsPopHead").height());
xsdrag("#xsPopHead", "#xsPop"); //Bind drag action
$("#xsColse").bind("click", function () { ClosePop(tag, mainContent); }); //Bind close action
}
4: Close action
Here you need to give the tag to the container first, otherwise it will be removed together when you remove it later, and the tag will not be found the second time it pops up.
Function ClosePop(tag, mainContent) {
$(mainContent).append(tag); //Save, otherwise $("#xsPop").remove() in step 4 will clear the tag
$(tag).hide();
$(".mask").remove();
$("#xsPop").remove();
}
5: Drag effect
Method 1: The first time I find it is an event that uses elements, but it is easy to lose elements and the effect is not ideal
//Drag and drop the pop-up layer (failed method, object loss will occur)
//Control is the drag element, tag is the action element, generally the control is within the tag
// function drag(control, tag) {
// // var isMove = false;
// var abs_x = 0, abs_y = 0;
// // $(control).mousedown(
// // function (event) {
// // var top = $(tag).offset().top;
// // var left = $(tag).offset().left;
// abs_x = event.pageX - left;
// // abs_y = event.pageY - top;
// // isMove = true;
}).mouseleave(function () {
// // isMove = false;
// // });
// // $(control).mouseup(function () {
// // isMove = false;
// // });
// // $(document).mousemove(function (event) {
// // if (isMove) {
// // $(tag).css({
// // 'left': event.pageX - abs_x $(tag).width() / 2 - 1,
// // 'top': event.pageY - abs_y $(tag).height() / 2 - 1
// // // // }
// // return false;
// // });
// }
I also found a problem. If I drag the pop-up layer directly to the top of the screen and let go, haha, you will never be able to drag it back or click to close it. I took a look at the pop-up layer on Baidu's homepage. They also have this phenomenon, but after zooming in and out of the window, the pop-up layer will return to the center. I also tried to do this, but when I bound onresize, it appeared that it could not move to the bottom. The event they used was definitely not onresize. So I directly judged that the mouse position cannot be less than 0.
//Drag and drop pop-up layer
//Control is the drag element, tag is the action element, generally the control is within the tag
Function xsdrag(control, tag) {
$(control).mousedown(function (e)//emouse event
{
var offset = $(this).offset(); //The position of DIV on the page
var x = e.pageX - offset.left; //Get the distance between the mouse pointer and the left border of the DIV element
var y = e.pageY - offset.top; //Get the distance between the mouse pointer and the upper boundary of the DIV element
$(document).bind("mousemove", function (ev)//Bind the mouse movement event, because the cursor must also have an effect outside the DIV element, so the document event must be used instead of the DIV element event
{
If (EV.Pagey & Lt; = 0) {Return;} // Prevent the frame from being unable to close and drag
$(tag).css({
'left': ev.pageX - x $(tag).width() / 2, // I need to add this to my layout
‘top’: ev.pageY - y $(tag).height() / 2
});
});
});
$(document).mouseup(function () {
$(this).unbind("mousemove");
});
}
6: Style sheet
The layout of the pop-up layer uses top and left margin-top negative values, so I add half of the height and width in my js
.mask
{
position: fixed;
width: 100%;
height: 100%;
background-color: white;
Overflow: scroll;
Filter: alpha(opacity=50);
-moz-opacity: 0.5;
Opacity: 0.5;
z-index: 20;
Overflow: auto;
top: 0px;
Right: 0px;
}
.PopUp
{
Padding: 0px;
Position: absolute;
z-index: 21 !important;
Background-color: White;
Border-style: solid solid solid solid;
Border-width: 1px;
Border-color: #C0C0C0;
Left: 50%;
top: 50%;
}
.PopHead
{
Background-color: #F0F0F0;
Border-bottom-style: solid;
Border-bottom-width: 1px;
Border-bottom-color: #C0C0C0;
Height: 30px;
Cursor: move;
clip: rect(0px, auto, auto, 0px);
}
.PopHead b
{
float: left;
Display: block;
Color: #C0C0C0;
font-family: System;
font-size: medium;
Line-height: 30px;
text-indent: 2em;
}
.PopHead span
{
float: right;
Display: block;
Text-align: right;
Line-height: 30px;
Cursor: pointer;
Text-indent: 5px;
Color: #FF0000;
font-size: 12pt;
}
.PopMain
{
Padding: 10px;
Overflow: auto;
}
7: Use of pages
Test server controls can submit forms
$(document).ready(function () {
$("#btnPop").click(function () {
var options = {
title: "my pop",
width: 500,
Heigth: 400,
close: "close",
mainContent: "form"
}
$("#pop1").xsPop(options);
});
});
Okay, that’s it. I originally wanted to make a border pull to change the size, but found that it would take some time, so I stopped doing it. In fact, to be honest, I think dragging doesn't make much sense, and border control size doesn't make much sense either, because I set the overflow and scroll bars will appear.

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

When users use the Edge browser, they may add some plug-ins to meet more of their needs. But when adding a plug-in, it shows that this plug-in is not supported. How to solve this problem? Today, the editor will share with you three solutions. Come and try it. Method 1: Try using another browser. Method 2: The Flash Player on the browser may be out of date or missing, causing the plug-in to be unsupported. You can download the latest version from the official website. Method 3: Press the "Ctrl+Shift+Delete" keys at the same time. Click "Clear Data" and reopen the browser.

What is the Chrome plug-in extension installation directory? Under normal circumstances, the default installation directory of Chrome plug-in extensions is as follows: 1. The default installation directory location of chrome plug-ins in windowsxp: C:\DocumentsandSettings\username\LocalSettings\ApplicationData\Google\Chrome\UserData\Default\Extensions2. chrome in windows7 The default installation directory location of the plug-in: C:\Users\username\AppData\Local\Google\Chrome\User

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

How does Google Chrome allow animation plugins to run? Google Chrome is very powerful. Many friends like to use this browser to watch video animations. However, if you want to watch various animated videos, you need to install animation plug-ins in the browser. Many friends use Google Chrome. After installing the animation plug-in, I still cannot care about the video. How should I deal with this problem? Next, let the editor show you the specific steps to allow the animation plug-in to run in Google Chrome. Friends who are interested can come and take a look. Specific steps for Google Chrome to allow animation plug-ins to run: 1. First run Google Chrome on your computer, and click the main menu button in the upper right corner of the homepage (as shown in the picture). 2. After opening the main menu, select the "Settings" option below (as shown in the picture). 3. In settings

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
