我對網路開發有點陌生。作為我的第一個 JS 專案,我決定創建一個簡單的按鈕,如果有人點擊它,它會顯示一個自定義彈出視窗。但問題是關閉彈出視窗后,我必須重繪 頁面才能再次單擊按鈕。
到目前為止,這是我的代碼:
const btn = document.querySelector('.btn');
const cloBtn = document.querySelector('.close');
const popUp = document.querySelector('.popup');
const overlay = document.querySelector('.overlay');
btn.addEventListener('click', () => {
popUp.classList.add('open');
overlay.classList.add('overlay-open');
});
cloBtn.addEventListener('click', () => {
popUp.classList.add('popClose');
overlay.classList.add('overlay-close');
})
body {
margin: 0%;
}
.btn {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
button {
border: none;
cursor: pointer;
}
.btn button {
background-color: blue;
padding: 15px 45px 15px 45px;
font-size: large;
font-weight: 700;
color: rgb(255, 255, 255);
border-radius: 10px;
}
.popup {
width: 300px;
background-color: rgb(228, 228, 228);
height: 150px;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
gap: 15px;
border-radius: 10px;
z-index: 5;
position: absolute;
top: 30%;
left: 50%;
transform: scale(1) translate(-50%, -50%);
visibility: hidden;
}
.popup button {
background-color: rgb(255, 0, 55);
padding: 10px 30px 10px 30px;
font-size: medium;
font-weight: 700;
color: rgb(0, 0, 0);
border-radius: 10px;
}
.overlay {
height: 100%;
width: 100%;
background-color: rgba(133, 131, 131, 0.555);
position: absolute;
z-index: 2;
visibility: hidden;
}
.overlay-open {
visibility: visible;
}
.overlay-close {
visibility: hidden;
}
.open {
visibility: visible;
animation: boxPop 0.3s ease-in;
}
@keyframes boxPop {
from {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
}
to {
position: absolute;
top: 30%;
left: 50%;
transform: scale(1) translate(-50%, -50%);
}
}
.popClose {
animation: boxClo 0.3s ease-in;
transform: translate(-50%, -50%) scale(0);
}
@keyframes boxClo {
from {
position: absolute;
top: 30%;
left: 50%;
transform: scale(1) translate(-50%, -50%);
}
to {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="btn"><button>Press Me</button></div>
<div class="popup">
<div class="sentence">Hello Nice to Meet You!</div>
<div class="close"><button>Close</button></div>
</div>
<div class="overlay"></div>
<script src="app.js"></script>
</body>
</html>
uj5u.com熱心網友回復:
問題是,兩個按鈕都按下添加類但沒有洗掉計數器類。最簡單的解決方案是使用toggle
. 在這種情況下,您可以將相同的 eventListener 系結到兩個按鈕并大大縮短 JS 代碼:
const btn = document.querySelectorAll('.btn');
const popUp = document.querySelector('.popup');
const overlay = document.querySelector('.overlay');
btn.forEach(el => el.addEventListener('click', () => {
popUp.classList.toggle('open');
overlay.classList.toggle('overlay-open');
}));
body {
margin: 0%;
}
.btn {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
button {
border: none;
cursor: pointer;
}
.btn button {
background-color: blue;
padding: 15px 45px 15px 45px;
font-size: large;
font-weight: 700;
color: rgb(255, 255, 255);
border-radius: 10px;
}
.popup {
width: 300px;
background-color: rgb(228, 228, 228);
height: 150px;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
gap: 15px;
border-radius: 10px;
z-index: 5;
position: absolute;
top: 30%;
left: 50%;
transform: scale(1) translate(-50%, -50%);
visibility: hidden;
}
.popup button {
background-color: rgb(255, 0, 55);
padding: 10px 30px 10px 30px;
font-size: medium;
font-weight: 700;
color: rgb(0, 0, 0);
border-radius: 10px;
}
.overlay {
height: 100%;
width: 100%;
background-color: rgba(133, 131, 131, 0.555);
position: absolute;
z-index: 2;
visibility: hidden;
}
.overlay-open {
visibility: visible;
}
.overlay-close {
visibility: hidden;
}
.open {
visibility: visible;
animation: boxPop 0.3s ease-in;
}
@keyframes boxPop {
from {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
}
to {
position: absolute;
top: 30%;
left: 50%;
transform: scale(1) translate(-50%, -50%);
}
}
.popClose {
animation: boxClo 0.3s ease-in;
transform: translate(-50%, -50%) scale(0);
}
@keyframes boxClo {
from {
position: absolute;
top: 30%;
left: 50%;
transform: scale(1) translate(-50%, -50%);
}
to {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%) scale(0);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="btn"><button>Press Me</button></div>
<div class="popup">
<div class="sentence">Hello Nice to Meet You!</div>
<div class="btn"><button>Close</button></div>
</div>
<div class="overlay"></div>
<script src="app.js"></script>
</body>
</html>
uj5u.com熱心網友回復:
這是因為您正在添加open
和overlay-open
類并且永遠不會洗掉它們。所以在接近之后,你有open
and popClose
on <div >
, and overlay-open
and overlay-close
on <div >
。
而 CSS的特殊性使得第二類適用。你可以做如下,有2個類而不是4個。你只需要open
和overlay-open
。
const btn= document.querySelector('.btn');
const cloBtn=document.querySelector('.close');
const popUp=document.querySelector('.popup');
const overlay=document.querySelector('.overlay');
btn.addEventListener('click',()=>{
popUp.classList.add('open');
overlay.classList.add('overlay-open');
});
cloBtn.addEventListener('click',()=>{
popUp.classList.remove('open');
overlay.classList.remove('overlay-open');
})
body{
margin: 0%;
}
.btn{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50% ,-50%);
}
button{
border: none;
cursor: pointer;
}
.btn button{
background-color: blue;
padding: 15px 45px 15px 45px;
font-size: large;
font-weight: 700;
color: rgb(255, 255, 255);
border-radius: 10px;
}
.popup{
width: 300px;
background-color: rgb(228, 228, 228);
height: 150px;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
gap: 15px;
border-radius:10px ;
z-index: 5;
position: absolute;
top: 30%;
left: 50%;
transform:scale(1) translate(-50% ,-50%);
visibility: hidden;
}
.popup button{
background-color: rgb(255, 0, 55);
padding: 10px 30px 10px 30px;
font-size: medium;
font-weight: 700;
color: rgb(0, 0, 0);
border-radius: 10px;
}
.overlay{
height: 100%;
width: 100%;
background-color: rgba(133, 131, 131, 0.555);
position: absolute;
z-index: 2;
visibility: hidden;
}
.overlay-open{
visibility: visible;
}
/*
.overlay-close{
visibility: hidden;
}*/
.open{
visibility: visible;
animation: boxPop 0.3s ease-in;
}
@keyframes boxPop {
from {
position: absolute;
top: 50%;
left: 50%;
transform:translate(-50% ,-50%) scale(0);
}
to {
position: absolute;
top: 30%;
left: 50%;
transform:scale(1) translate(-50% ,-50%);
}
}
/*
.popClose{
animation: boxClo 0.3s ease-in;
transform:translate(-50% ,-50%) scale(0);
}*/
@keyframes boxClo {
from {
position: absolute;
top: 30%;
left: 50%;
transform:scale(1) translate(-50% ,-50%);
}
to {
position: absolute;
top: 50%;
left: 50%;
transform:translate(-50% ,-50%) scale(0);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="btn"><button>Press Me</button></div>
<div class="popup">
<div class="sentence">Hello Nice to Meet You!</div>
<div class="close"><button>Close</button></div>
</div>
<div class="overlay"></div>
<script src="app.js"></script>
</body>
</html>
uj5u.com熱心網友回復:
您必須在每個函式執行后洗掉舊類才能正常作業。
const btn= document.querySelector('.btn');
const cloBtn=document.querySelector('.close');
const popUp=document.querySelector('.popup');
const overlay=document.querySelector('.overlay');
btn.addEventListener('click',()=>{
popUp.classList.add('open');
overlay.classList.add('overlay-open');
popUp.classList.remove('popClose');
overlay.classList.remove('overlay-close')
});
cloBtn.addEventListener('click',()=>{
popUp.classList.remove('open');
overlay.classList.remove('overlay-open');
popUp.classList.add('popClose');
overlay.classList.add('overlay-close');
})
body{
margin: 0%;
}
.btn{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50% ,-50%);
}
button{
border: none;
cursor: pointer;
}
.btn button{
background-color: blue;
padding: 15px 45px 15px 45px;
font-size: large;
font-weight: 700;
color: rgb(255, 255, 255);
border-radius: 10px;
}
.popup{
width: 300px;
background-color: rgb(228, 228, 228);
height: 150px;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
gap: 15px;
border-radius:10px ;
z-index: 5;
position: absolute;
top: 30%;
left: 50%;
transform:scale(1) translate(-50% ,-50%);
visibility: hidden;
}
.popup button{
background-color: rgb(255, 0, 55);
padding: 10px 30px 10px 30px;
font-size: medium;
font-weight: 700;
color: rgb(0, 0, 0);
border-radius: 10px;
}
.overlay{
height: 100%;
width: 100%;
background-color: rgba(133, 131, 131, 0.555);
position: absolute;
z-index: 2;
visibility: hidden;
}
.overlay-open{
visibility: visible;
}
.overlay-close{
visibility: hidden;
}
.open{
visibility: visible;
animation: boxPop 0.3s ease-in;
}
@keyframes boxPop {
from {
position: absolute;
top: 50%;
left: 50%;
transform:translate(-50% ,-50%) scale(0);
}
to {
position: absolute;
top: 30%;
left: 50%;
transform:scale(1) translate(-50% ,-50%);
}
}
.popClose{
animation: boxClo 0.3s ease-in;
transform:translate(-50% ,-50%) scale(0);
}
@keyframes boxClo {
from {
position: absolute;
top: 30%;
left: 50%;
transform:scale(1) translate(-50% ,-50%);
}
to {
position: absolute;
top: 50%;
left: 50%;
transform:translate(-50% ,-50%) scale(0);
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div class="btn"><button>Press Me</button></div>
<div class="popup">
<div class="sentence">Hello Nice to Meet You!</div>
<div class="close"><button>Close</button></div>
</div>
<div class="overlay"></div>
<script src="app.js"></script>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/494037.html
標籤:javascript html css