JavaScript弹出窗口?

a5g8bdjr  于 2023-03-28  发布在  Java
关注(0)|答案(2)|浏览(153)

使用HTML、CSS和JavaScript构建一个弹出计算器。我基本完成了设计方面的工作,但我无法让我的打开计算器按钮工作。我错过了什么来使这个工作?我假设一旦我找到了我出错的地方,我就可以在我的计算器的退出/关闭按钮上使用这个概念。

let calc = document.getElementById("calculator");
let overlay = document.getElementById("overlay");
let var1 = document.getElementById('var1');
let var2 = document.getElementById('var2');

function openCalc() {
  calc.classlist.add(".calculator .active");
  overlay.clastlist.add('active');
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

.container {
  width: 100%;
  height: 100vh;
  background-image: url("./bg.png");
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn {
  padding: 10px 60px;
  background: #fff;
  border: 0;
  outline: none;
  cursor: pointer;
  font-size: 22px;
  font-weight: 500;
  border-radius: 25px;
}

.calculator {
  width: 500px;
  background-color: #7ea2d4;
  border-radius: 10px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  padding: 0 30px 30px;
  border: 1px solid black;
  max-width: 80%;
  visibility: hidden;
  transition: 400ms ease-in-out;
}

.calculator .active {
  transform: translate(-50%, -50%) scale(1);
  visibility: visible;
}

.calculator .header {
  padding: 10px 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid black;
}

.calculator img {
  width: 100px;
  border-radius: 50%;
  box-shadow: 0 2px 5px rgb(255, 255, 255);
}

.calculator .close-button {
  cursor: pointer;
  border: none;
  outline: none;
  background: none;
  font-size: 1.5rem;
  font-weight: bold;
  color: red;
  width: 30px;
  height: 40px;
  margin-bottom: 50px;
}

.calculator .close-button:hover {
  cursor: pointer;
  border-radius: 5px;
  background-color: red;
  color: white;
  transition: 400ms ease-in-out;
}

.calc-body {
  padding: 10px 15px;
}

.calc-body .operator {
  margin-top: 15px;
  border: 0;
  outline: 0;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: whitesmoke;
  font-size: 20px;
  color: black;
  cursor: pointer;
  margin: 10px;
}

.calc-body .display {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

#overlay {
  position: fixed;
  opacity: 0;
  transition: 400ms ease-in-out;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, .5);
  pointer-events: none;
}

#overlay.active {
  opacity: 1;
  pointer-events: all;
}
<!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">
  <title>Calculator</title>
</head>

<body>
  <div class="container">
    <button type="submit" class="btn" onclick="openCalc()">Start Calculator</button>

    <div class="calculator" id="calculator">
      <div class="header">
        <img src="calc.png">
        <h2>Your Calculator App!</h2>
        <button type="button" class="close-button" id="closeCalc">&times;</button>
      </div>

      <div class="calc-body">
        Enter Number 1: <input type="text" id="var1"><br> Enter Number 2: <input type="text" id="var2"><br>
        <input type="button" value="+" class="operator" onclick="add()">
        <input type="button" value="-" class="operator" onclick="sub()">
        <input type="button" value="*" class="operator" onclick="multi()">
        <input type="button" value="/" class="operator" onclick="divi()">
        <input type="button" value="%" class="operator" onclick="modulo()">
      </div>

      <div class="result" id="popup">
        <h2>The answer is...</h2>
        <div class="display">
          <input type="text" name="display">
        </div>
      </div>
    </div>
  </div>

  <div id="overlay"></div>

  <script>
  </script>
</body>

</html>
ldioqlga

ldioqlga1#

你在原始代码中犯了一些错误,让我们一个接一个地检查一下:
1.要操作DOM元素类,应该使用el.classList.add("a-class-name")而不是classlist
1.在向元素添加新类名时,不需要在类名前包含点。因此,使用classList.add("active")代替classList.add(".active")
1.当一个元素有多个类名时,使用.first-class-name.second-class-name在CSS中选择该元素,选择器之间没有任何空格。
最后,我将z-index添加到弹出窗口中,这样它就不会显示在覆盖元素下面。
下面是代码的固定版本:

let calc = document.getElementById('calculator');
let overlay = document.getElementById('overlay');
let var1 = document.getElementById('var1');
let var2 = document.getElementById('var2');

function openCalc() {
  calc.classList.add('active');
  overlay.classList.add('active');
}
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Poppins', sans-serif;
}

.container {
  width: 100%;
  height: 100vh;
  background-image: url('./bg.png');
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn {
  padding: 10px 60px;
  background: #fff;
  border: 0;
  outline: none;
  cursor: pointer;
  font-size: 22px;
  font-weight: 500;
  border-radius: 25px;
}

.calculator {
  width: 500px;
  background-color: #7ea2d4;
  border-radius: 10px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  padding: 0 30px 30px;
  border: 1px solid black;
  max-width: 80%;
  visibility: hidden;
  transition: 400ms ease-in-out;
  z-index: 10;
}

.calculator.active {
  transform: translate(-50%, -50%) scale(1);
  visibility: visible;
}

.calculator .header {
  padding: 10px 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid black;
}

.calculator img {
  width: 100px;
  border-radius: 50%;
  box-shadow: 0 2px 5px rgb(255, 255, 255);
}

.calculator .close-button {
  cursor: pointer;
  border: none;
  outline: none;
  background: none;
  font-size: 1.5rem;
  font-weight: bold;
  color: red;
  width: 30px;
  height: 40px;
  margin-bottom: 50px;
}

.calculator .close-button:hover {
  cursor: pointer;
  border-radius: 5px;
  background-color: red;
  color: white;
  transition: 400ms ease-in-out;
}

.calc-body {
  padding: 10px 15px;
}

.calc-body .operator {
  margin-top: 15px;
  border: 0;
  outline: 0;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: whitesmoke;
  font-size: 20px;
  color: black;
  cursor: pointer;
  margin: 10px;
}

.calc-body .display {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

#overlay {
  position: fixed;
  opacity: 0;
  transition: 400ms ease-in-out;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  pointer-events: none;
}

#overlay.active {
  opacity: 1;
  pointer-events: all;
}
<!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" />
  <title>Calculator</title>
</head>

<body>
  <div class="container">
    <button type="submit" class="btn" onclick="openCalc()">
        Start Calculator
      </button>

    <div class="calculator" id="calculator">
      <div class="header">
        <img src="calc.png" />
        <h2>Your Calculator App!</h2>
        <button type="button" class="close-button" id="closeCalc">
            &times;
          </button>
      </div>

      <div class="calc-body">
        Enter Number 1: <input type="text" id="var1" /><br /> Enter Number 2: <input type="text" id="var2" /><br />
        <input type="button" value="+" class="operator" onclick="add()" />
        <input type="button" value="-" class="operator" onclick="sub()" />
        <input type="button" value="*" class="operator" onclick="multi()" />
        <input type="button" value="/" class="operator" onclick="divi()" />
        <input type="button" value="%" class="operator" onclick="modulo()" />
      </div>

      <div class="result" id="popup">
        <h2>The answer is...</h2>
        <div class="display">
          <input type="text" name="display" />
        </div>
      </div>
    </div>
  </div>

  <div id="overlay"></div>
</body>

</html>
pdsfdshx

pdsfdshx2#

添加它的css和改变你的功能

function openCalc(){
    calc.classList.add("active");
    overlay.classList.add('active');
}
#overlay.active{
    opacity: 1;
    pointer-events: all;
    z-index: -1;
}

.active {
    visibility: visible;
    z-index: 1;
}

相关问题