简体   繁体   English

仅在第一次加载页面时使 div 不可见

[英]Make div not visible only the first time page loads

I want my .card div on the right side of the page (or at the bottom half when screen is less than 1000px) to appear only after pressing Enter button.我希望页面右侧(或屏幕小于 1000 像素时位于下半部分)的.card div 仅在按下 Enter 按钮后出现。 And then again to appear the same way after each subsequent 'Enter'.然后在随后的每个“输入”之后再次以相同的方式出现。 Animation works correctly, only thing I can't make it work is to have .card hidden first time I load the page. Animation 工作正常,唯一不能让它工作的是在我第一次加载页面时隐藏.card I have tried with opacity: 0 , but in that case.card will disappear right after the animation end.我尝试过opacity: 0 ,但在这种情况下,.card 将在 animation 结束后立即消失。 I have also tried to tweak with height property, but in that case my responsive page didn't look as expected.我也尝试使用 height 属性进行调整,但在这种情况下,我的响应页面看起来不像预期的那样。 How can I fix this?我怎样才能解决这个问题?

html: html:

  <div class="container">
      <div class="intro">
        <code>WEATHER</code> <br />
        <h1>DATA AND FORECAST</h1>
        <br />
        <input
          type="text"
          name="cityInput"
          class="cityInput"
          id="cityInput"
          placeholder="Enter city name and press Enter..."
        />
      </div>
      <div class="output">
        <div class="card"></div>
      </div>
    </div>

css: css:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

.container {
  min-height: 100vh; 
  background-image: url("https://images.unsplash.com/photo-1488866022504-f2584929ca5f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1162&q=80"); /* The image used */
  background-color: #cccccc; 
  background-repeat: no-repeat;
  background-position: center; 
  background-size: cover;
  isolation: isolate;
  display: flex;
}

.intro {
  position: relative;
  width: 40%;

  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
}

.intro::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: #2567a7;
  mix-blend-mode: multiply;
}

code {
  display: block;
  width: max-content;
  background: #fff;
  color: #2567a7;
  padding: 10px;
  margin: 2%;
  font-size: 40px;
  font-weight: bold;
  border-radius: 5px;
}

.intro h1 {
  font-size: 60px;
  font-weight: bold;
  color: #fff;
  margin: 2%;
}

.cityInput {
  outline: none;
  border: none;
  background: white;
  padding: 10px;
  margin: 2%;
  border-radius: 5px;
  font-size: 20px;
  font-weight: bold;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  color: #2567a7;
  text-align: left;
  width: 50%;
}

.cityInput::placeholder {
  color: #2567a7;
  text-align: left;
}

.output {
  width: 67%;
  display: grid;
  place-items: center;
}

.card {
  width: 60%;
  height: 60%;
  background: #030815;
  color: #fff;
  transition: all 2s;
}

.card:hover {
  background: rgba(3, 8, 21, 0.8);
}

@keyframes appear {
  0% {
    opacity: 0;
  }
  100% {
    opacity: 1;
  }
}

@media screen and (max-width: 1000px) {
  .container {
    flex-direction: column;
  }

  .intro {
    width: 100%;
    height: 40%;
    align-items: center;
    padding: 2%;
  }
  code {
    font-size: 20px;
    margin: 1%;
  }
  .intro h1 {
    margin: 1%;
    font-size: 30px;
    font-weight: bold;
  }
  .cityInput {
    margin: 1%;
  }
  .output {
    margin: 0 auto;
    margin-top: 5%;
    width: 80%;
  }
  .card {
    width: 100%;
    height: 55vh;
  }
  .cityInput {
    font-size: 16px;
  }
}

js: js:

const cityInput = document.querySelector("#cityInput");
const card = document.querySelector(".card");

const pressEnter = (e) => {
  let city = cityInput.value;

  if (e.key === "Enter") {
    console.log(city);
    card.innerHTML = `<div><h1>${city}</h1> <h2>${city}</h2></div>`;
    cityInput.value = "";

    card.style.animation = "appear 2s linear";
    card.addEventListener("animationend", function () {
      card.style.animation = "";
    });
  }
};

cityInput.addEventListener("keydown", pressEnter);

Just adding opacity: 0 to your .card class and setting its opacity to 1 in your function works totally fine for me.只需将opacity: 0添加到您的.card class 并将其 opacity 设置为 1 在您的 function 中对我来说完全没问题。

.card {
  width: 60%;
  height: 60%;
  background: #030815;
  color: #fff;
  transition: all 2s;
  opacity: 0;
}
const cityInput = document.querySelector("#cityInput");
const card = document.querySelector(".card");

const pressEnter = (e) => {
  card.style.opacity = "1";
  let city = cityInput.value;

  if (e.key === "Enter") {
    console.log(city);
    card.innerHTML = `<div><h1>${city}</h1> <h2>${city}</h2></div>`;
    cityInput.value = "";

    card.style.animation = "appear 2s linear forward";
    card.addEventListener("animationend", function () {
      card.style.animation = "";
    });
  }
};

cityInput.addEventListener("keydown", pressEnter);

I hope this is what you were searching for.我希望这是你正在寻找的。 Pretty much, the trick to make the card show after the page loads is by applying the following CSS to the element you want it to be affected:几乎,在页面加载后显示卡片的技巧是将以下 CSS 应用于您希望它受到影响的元素:

.fade-in {
    opacity: 1;                         /* We will end showing this */
    animation-name: fadeInOpacity;      /* Lets use this keyframe animation */
    animation-iteration-count: 1;       /* This animation will be run once */
    animation-timing-function: ease-in; /* Lets make it fluid */
    animation-duration: 2s;             /* the property name is self explanatory */
}

@keyframes fadeInOpacity {
    0% {
        opacity: 0; /* We start hiding it */
    }
    100% {
        opacity: 1; /* We end showing it */
    }
}

 .fade-in { opacity: 1; animation-name: fadeInOpacity; animation-iteration-count: 1; animation-timing-function: ease-in; animation-duration: 2s; } @keyframes fadeInOpacity { 0% { opacity: 0; } 100% { opacity: 1; } } * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; }.container { min-height: 100vh; background-image: url("https://images.unsplash.com/photo-1488866022504-f2584929ca5f?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1162&q=80"); /* The image used */ background-color: #cccccc; background-repeat: no-repeat; background-position: center; background-size: cover; isolation: isolate; display: flex; }.intro { position: relative; width: 40%; display: flex; flex-direction: column; align-items: flex-start; justify-content: center; }.intro::after { content: ""; position: absolute; inset: 0; z-index: -1; background: #2567a7; mix-blend-mode: multiply; } code { display: block; width: max-content; background: #fff; color: #2567a7; padding: 10px; margin: 2%; font-size: 40px; font-weight: bold; border-radius: 5px; }.intro h1 { font-size: 60px; font-weight: bold; color: #fff; margin: 2%; }.cityInput { outline: none; border: none; background: white; padding: 10px; margin: 2%; border-radius: 5px; font-size: 20px; font-weight: bold; font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; color: #2567a7; text-align: left; width: 50%; }.cityInput::placeholder { color: #2567a7; text-align: left; }.output { width: 67%; display: grid; place-items: center; }.card { width: 60%; height: 60%; background: #030815; color: #fff; transition: all 2s; }.card:hover { background: rgba(3, 8, 21, 0.8); } @keyframes appear { 0% { opacity: 0; } 100% { opacity: 1; } } @media screen and (max-width: 1000px) {.container { flex-direction: column; }.intro { width: 100%; height: 40%; align-items: center; padding: 2%; } code { font-size: 20px; margin: 1%; }.intro h1 { margin: 1%; font-size: 30px; font-weight: bold; }.cityInput { margin: 1%; }.output { margin: 0 auto; margin-top: 5%; width: 80%; }.card { width: 100%; height: 55vh; }.cityInput { font-size: 16px; } }
 <div class="container"> <div class="intro"> <code>WEATHER</code> <br /> <h1>DATA AND FORECAST</h1> <br /> <input type="text" name="cityInput" class="cityInput" id="cityInput" placeholder="Enter city name and press Enter..." /> </div> <div class="output"> <div class="card fade-in">Fade this card in after loading...</div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM