Home  >  Article  >  Web Front-end  >  How to design a pop-up window with JavaScript

How to design a pop-up window with JavaScript

WBOY
WBOYOriginal
2023-05-12 17:17:371163browse

Pop-up windows are one of the common interactive elements in modern web design. They can be used to prompt users, display content, or guide users to perform operations. Javascript is an indispensable language in modern web design. Pop-up windows can be designed through Javascript. Below we will introduce how to use Javascript to design pop-up windows.

  1. HTML structure

The pop-up window needs to have an HTML structure first, which can be implemented by selecting tags such as div and section. Inside this tag, you can add the content that needs to be displayed.

HTML structure is as follows:

<div class="dialog-box">
    <div class="dialog-content">这里填写需要展示的内容</div>
    <button class="close-btn">X</button>
</div>
  1. CSS style

CSS style is used to define the appearance of pop-up windows. Some basic styles need to be set, such as pop-up windows. Window width, height, positioning, etc. In addition, you also need to set the initial state of the pop-up window to the hidden state.

CSS style is as follows:

.dialog-box{
    width: 400px;
    height: 300px;
    background-color: white;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    border-radius: 5px;
    z-index: 999;
    box-shadow: 0px 0px 10px rgba(0,0,0,0.5);
    display: none;
}
.dialog-content{
    padding: 20px;
    height: 100%;
    overflow-y: auto;
}
.close-btn{
    position: absolute;
    top: 5px;
    right: 5px;
}
  1. Javascript code

After the HTML structure and CSS style of the pop-up window have been set, you can use Javascript To display and hide pop-up windows.

First, you need to get the elements of the pop-up window and set the style when showing and hiding it.

The Javascript code is as follows:

var dialogBox = document.querySelector('.dialog-box');
var closeBtn = document.querySelector('.close-btn');

function showDialogBox(){
    dialogBox.style.display = 'block';
}
function hideDialogBox(){
    dialogBox.style.display = 'none';
}

Next, you need to add click events to the open button and close button of the pop-up window. When the user clicks these buttons, the pop-up window will open or close accordingly.

The Javascript code is as follows:

var openBtn = document.querySelector('.open-btn');

openBtn.addEventListener('click',showDialogBox);
closeBtn.addEventListener('click',hideDialogBox);

The above is the basic process of using Javascript to design pop-up windows. In actual use, we can add and modify the code according to specific needs to achieve more complex and rich pop-up effects.

The above is the detailed content of How to design a pop-up window with JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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