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

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

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.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software