jQuery Dialog popup layer dialog plug-in_jquery
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.
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.
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:
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:
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.
/* 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

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











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.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
