Imitating Bootstrap, Bootstrap modal adds fixed to the outer layer, and then uses adaptive top-centering method for the content. The article I’m sharing today does exactly that.
html structure
Considering that the content area needs to be centered, there must be at least one div to position and display the background, and then use a div to center the content. The content area wants to be divided into header, body and footer.
<div class="rs-dialog" id="myModal1"> <div class="rs-dialog-box"> <a class="close" href="#">×</a> <div class="rs-dialog-header"> <h3 id="标题">标题</h3> </div> <div class="rs-dialog-body"> <p>内容</p> </div> <div class="rs-dialog-footer"> <input type="button" class="close" value="Close" style="float:right"> </div> </div> </div>
Add style
Transparent background is achieved with background and opacity. You can also choose rgba, but browsers IE8 and below do not support it. In order to make a div with fixed position cover the entire window, you can set its top, right, left, and bottom attributes to 0.
When the content area is too long, simulate the browser's vertical scroll bar by setting the overflow attribute of the body (or HTML) to hidden, disabling the browser's real scroll bar from appearing, and then setting the outermost div of the pop-up window. overflow-y:auto, uses the scroll bar of this div to simulate the browser scroll bar.
The scrolling effect when the pop-up window is opened and closed is implemented using css3 transition.
.dialog-open{ overflow-y:hidden !important; } .rs-overlay{ background:#000; opacity:.5; filter: alpha(opacity=50); position: fixed; z-index: 1000; top:0; bottom:0; left:0; right:0; display: none; } .rs-dialog{ display: none; opacity: 0; overflow: hidden; position: fixed; top:0; bottom:0; left:0; right:0; z-index: 1040; -webkit-overflow-scrolling: touch; outline: 0; /*background: rgba(0,0,0,.5);*/ -webkit-transition: opacity .15s linear; -o-transition: opacity .15s linear; transition: opacity .15s linear; } .dialog-open .rs-dialog{ overflow-x:hidden; overflow-y:auto; } .rs-dialog.in{ opacity: 1; } .rs-dialog .rs-dialog-box { -webkit-transform: translate(0, -25%); -ms-transform: translate(0, -25%); -o-transform: translate(0, -25%); transform: translate(0, -25%); -webkit-transition: -webkit-transform 0.3s ease-out; -o-transition: -o-transform 0.3s ease-out; transition: transform 0.3s ease-out; } .rs-dialog.in .rs-dialog-box { -webkit-transform: translate(0, 0); -ms-transform: translate(0, 0); -o-transform: translate(0, 0); transform: translate(0, 0); } .rs-dialog .rs-dialog-box{ position: relative; margin:30px auto; width: 600px; background-color: #ffffff; border-radius:10px; border: 1px solid #999999; border: 1px solid rgba(0, 0, 0, 0.2); -webkit-box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5); box-shadow: 0 3px 9px rgba(0, 0, 0, 0.5); } .logged-in .rs-dialog .rs-dialog-box{ margin-top:60px; } .rs-dialog-box a.close{ position: absolute; top: -12px; right: -12px; width: 25px; height: 25px; padding: 0; line-height: 25px; font-size:20px; font-family:Arial,sans-serif; font-weight:bold; text-decoration:none; text-align:center; text-shadow: 0 1px 0 #ffffff; color: #fff; background-color:#8b8b8b; border:2px solid #fff; border-radius: 25px; box-shadow:0 0 3px 1px #999; outline: none; } .rs-dialog-box a.close:hover{ background-color:#444; } .rs-dialog-header{ padding: 20px; border-bottom: 1px solid #e5e5e5; } .rs-dialog-header h3{ font-size: 18px; } .rs-dialog-body{ padding: 20px; line-height: 1.4 } .rs-dialog-body p{ margin-bottom:10px; } .rs-dialog-footer{ padding: 20px; border-top:1px solid #e5e5e5; overflow: hidden; } @media (max-width: 767px) { .rs-dialog .rs-dialog-box { width: auto; margin: 30px 20px; } }
Add control script
Most of them are implemented with css, so the script can control the switch through simple addClass and removeClass.
You can also add the function of clicking outside the pop-up window to close the window.
jQuery(document).ready(function($){ $('body').append('<div class="rs-overlay" />'); $("a[rel='rs-dialog']").each(function(){ var trigger = $(this); var rs_dialog = $('#' + trigger.data('target')); var rs_box = rs_dialog.find('.rs-dialog-box'); var rs_close = rs_dialog.find('.close'); var rs_overlay = $('.rs-overlay'); if( !rs_dialog.length ) return true; // Open dialog trigger.click(function(){ //Get the scrollbar width and avoid content being pushed var w1 = $(window).width(); $('html').addClass('dialog-open'); var w2 = $(window).width(); c = w2-w1 + parseFloat($('body').css('padding-right')); if( c > 0 ) $('body').css('padding-right', c + 'px' ); rs_overlay.fadeIn('fast'); rs_dialog.show( 'fast', function(){ rs_dialog.addClass('in'); }); return false; }); // Close dialog when clicking on the close button rs_close.click(function(e){ rs_dialog.removeClass('in').delay(150).queue(function(){ rs_dialog.hide().dequeue(); rs_overlay.fadeOut('slow'); $('html').removeClass('dialog-open'); $('body').css('padding-right', ''); }); return false; }); // Close dialog when clicking outside the dialog rs_dialog.click(function(e){ rs_close.trigger('click'); }); rs_box.click(function(e){ e.stopPropagation(); }); }); });
Prevent webpage content from shifting
Add the overflow:hidden attribute to the body when opening a pop-up window, causing the scroll bar to disappear. At this time, the content of the web page will move to the right. When the pop-up window opens and the scroll bar appears again, it will Will recover, the visual effect is not friendly. If you know the width of the scroll bar when there is one, adding the padding-right attribute to the body can offset this cheap effect.
Trigger the pop-up window
Finally, according to the above script, the link that triggers the pop-up window is as follows
<a href="#" rel="rs-dialog" data-target="myModal">Launch Demo Modal</a>
rel="rs-dialog" means that this is the link that triggers the pop-up window
data-target="myModal" means that you want to open HTML Pop-up window with ID myModal.
For more articles related to Bootstrap Modal mask pop-up layer (full version), please pay attention to the PHP Chinese website!

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.

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.

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.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

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

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

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.

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.


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft