Table of Contents
Custom CSS
Home Web Front-end JS Tutorial jQuery Dialog popup layer dialog plug-in_jquery

jQuery Dialog popup layer dialog plug-in_jquery

May 16, 2016 pm 06:21 PM
dialog Pop-up layer

The principle is very simple. Dynamically build a div layer through JS, insert it into the body, and then adjust the CSS property of position to absolute or fixed to separate it from the original document flow position. Then it can be beautified through appropriate processing.

Copy code The code is as follows:

This is Structure of two div layers, the first background mask layer is only created when needed. Each div defines a CSS class, making it easy to customize its appearance.

Implementation of some basic functions
Move the frame
As long as in the mousemove event, calculate the difference between the two mouse positions, plus the original top and left of the moved frame, that is The new location of the dialog box. The mousemove event only needs to be triggered when the mouse presses the title bar, so the mousemove event is only bound when the mousedown event of the title bar is triggered, and the mousemove event is also unbound when the mouse is released.

mousemove and mouseup that unbind the mousemove event are not bound to the title bar, but to the document. The reason for this is because sometimes when the mouse moves too fast, it will move out of the title bar range. At this time If it is bound to the event on the title bar, it will be invalid, but if it is bound to the document, it will not.
Copy code The code is as follows:

var mouse={x:0,y:0} ;
function moveDialog(event)
{
var e = window.event || event;
var top = parseInt(dialog.css('top')) (e.clientY - mouse. y);
var left = parseInt(dialog.css('left')) (e.clientX - mouse.x);
dialog.css({top:top,left:left});
mouse.x = e.clientX;
mouse.y = e.clientY;
};
dialog.find('.bar').mousedown(function(event){
var e = window.event || event;
mouse.x = e.clientX;
mouse.y = e.clientY;
$(document).bind('mousemove',moveDialog);
});
$(document).mouseup(function(event){
$(document).unbind('mousemove', moveDialog);
});

Positioning
Center positioning is easy to achieve. A series of attributes such as clientWidth and offsetWidth under IE seem a bit different from other browsers, so don’t use these attributes. You can directly use the width() function under jQuery:
Copy code The code is as follows:

var left = ($(window).width() - dialog.width ()) / 2;
var top = ($(window).height() - dialog.height()) / 2;
dialog.css({top:top,left:left});IE6 There is no fixed mode, but it can be simulated through the window.onscroll event:

// top The distance from the dialog box to the top of the visual area.
var top = parseInt(dialog.css('top')) - $(document).scrollTop();
var left = parseInt(dialog.css('left')) - $(document). scrollLeft();
$(window).scroll(function(){
dialog.css({'top':$(document).scrollTop() top,'left':$(document).scrollLeft () left});
});

z-index
In order to allow multiple dialog boxes to coexist, a static zIndex variable is used to create a new dialog box each time Each time, an auto-increment operation is implemented and the new value is assigned to the z-index of the newly created dialog box, so as to ensure that the last created dialog box is always at the front.

External interface
The plug-in is called in the following way:
Copy code The code is as follows:

var dlg = new Dialog(content, options);
dlg.show(); Of course, if it is just for general use, it can be simpler:

new Dialog(content, options ).show();
// or
dialog(content, options); You can also use the following four functions to further control the plug-in:

show(): Display the dialog box
hide(): Hide the dialog box, but does not delete the content in the dialog box.
close(): Close the dialog box and completely delete its content.
setContent(content): Change the content in the dialog box.
Constructor parameters
The constructor has two parameters. content and options. content represents the content of the dialog box; options represents the various configuration options of the dialog box.

content can be a string representing the displayed content. Or an Object type. If it is an Object type, it needs to contain the following two attributes: type and value. Type represents the data type, and value is the content corresponding to type. type accepts the following types:

id: displays the content of a certain ID, but does not include the content of the ID itself. value corresponds to the ID value of a certain HTML element.
img: Display an image. value corresponds to the uri of the image.
url: Display the content of a certain URL through ajax, so it must be under the same domain name. value is the corresponding URL address.
iframe: Display the content specified by a URL into an iframe. It removes some restrictions on AJAX calls (with the same domain name, the returned HTML code cannot contain headers, etc.). value is the corresponding URL.
options are specific settings for Dialog behavior and appearance:
选项
名称 描述 默认值
title 标题栏的文本 标题
closeText 关闭按钮文字 [关闭]
showTitle 是否显示标题栏,若为否,则标题和关闭按钮都将不显示。 true
draggable 是否可以拖动框体。 true
modal 模态对话框,若为是,则不可操作背景层。 true
center 是否居中显示,若为否,则通过CSS中的内容进行定位。 true
fixed 对话框是否跟随滚动条移动。 true
time 自动关闭对话框时间,单位毫秒,若为0,表示不会自动关闭。 0
id 对话框的ID。若为false,则表示自动产生。 false
回调函数
名称 描述 返回值类型
beforeShow 在显示之前调用,若返回false,则不显示对话框。 bool
afterShow 显示之后调用。
beforeHide 在隐藏之前调用,若返回false,则不隐藏对话框。 bool
afterHide 隐藏之后调用。
beforeClose 在关闭之前调用,若返回false,则不关闭对话框。 bool
afterClose 关闭之后调用。

Custom CSS

The plug-in provides a CSS class name for each part of the dialog box, making it easy to customize the CSS:

类名 描述
.dialog-overlay 模态对话框时,的背景遮盖层。
.dialog 对话框的CSS。
.dialog .bar 标题栏的CSS。包含了标题和关闭按钮。
.dialog .bar .title 标题栏的标题部分。
.dialog .bar .close 标题栏的关闭按钮部分。
.dialog .content 内容部分。

You can directly modify these CSS classes to make global modifications, or you can modify a certain dialog box by adding the id and class name.

Copy code The code is as follows:

/* Only modify the dialog box with the id #dialog. */
#dialog1 .bar
{
text-transform:lowercase;
}
// Specify the id of the dialog box through the id attribute.
new Dialog('text',{id:'dialog1'});

Online demo code
Code package download
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles