CSS 不透明度过渡不适用于添加 Class

CSS Opacity Transition not Working on Adding Class

我在选择器 #loginForm > div:first-child h1 上执行简单的 css 不透明度转换时遇到问题,只需使用 javascript 将 class .animated 添加到该元素即可。虽然它不起作用。下面是代码:

document.querySelector('#loginForm > div:nth-child(2)').style.display = 'none';
document.querySelector('#loginForm > div:first-child').style.display = 'block';
var verified_text = document.querySelector('#loginForm > div:first-child h1');
verified_text.classList.add('animated');
#loginForm {
  position: relative;
  padding: 40px;
  background-color: #fcde84;
  border-radius: 5px;
  margin-top: 75px;
  width: 462px;
}

#loginForm>div:first-child {
  height: 380px;
  border: 0;
  filter: drop-shadow( 5px 8px 8px rgba(0, 0, 0, 0.1));
}

#loginForm>div:first-child div {
  margin-top: 165px;
}

#loginForm>div div {
  width: 150px;
  height: 150px;
  margin: 0 auto;
  overflow: hidden;
}

#loginForm>div:first-child h1 {
  font-family: 'Arial Rounded MT';
  color: rgb(109, 204, 91);
  text-align: center;
  display: block;
  transition: all 5s ease-out;
  opacity: 0;
}

#loginForm>div:first-child h1.animated {
  opacity: 1;
}

#loginForm h2 {
  font-size: 34px;
  text-transform: uppercase;
  line-height: 24px;
  color: #2f4e71;
  letter-spacing: -2px;
  font-weight: 700;
  font-family: 'Montserrat', sans-serif;
}

#loginForm p {
  font-size: 17px;
  line-height: 40px;
  color: #333850;
  display: block;
  margin: 0 0 10px;
  font-weight: 700;
}

#loginForm>div:nth-child(2) div {
  border-radius: 50%;
  border: solid 2.5px #3A5E77;
}

#loginForm ul {
  padding: 10px 15px 10px 30px;
  background-color: #fc7f77;
  color: white;
  border-radius: 7.5px;
  margin: 12px 0;
  display: none;
}

#loginForm li {
  text-align: center;
  display: block;
  font-size: 24px;
}

#loginForm label {
  margin: 0 0 12px;
  display: block;
  font-size: 1.25em;
  color: #217093;
  font-weight: 700;
}

#loginForm input {
  border-radius: 32.5px;
  height: 65px;
  width: 100%;
  font-weight: 600;
  font-size: 1.55em;
  transition: box-shadow .2s linear, border-color .25s ease-out, background-color .2s ease-out;
}

#email,
#password {
  margin: 0 0 24px;
  padding: 0 1em 0;
  background-color: #f3fafd;
  border: solid 2px #217093;
}

#email:focus,
#password:focus {
  outline: none;
  box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.1);
  border: solid 2px #4eb8dd;
}

#loginForm input[type="submit"] {
  padding: .65em 1em 1em;
  background-color: #4eb8dd;
  color: #FFF;
}

#loginForm input[type="submit"]:hover {
  background-color: #217093;
}
<form autocomplete="on" method="post" id="loginForm">
  <div style="display: none;">
    <div></div>
    <h1>Verified</h1>
  </div>
  <div>
    <h2>Join our community</h2>
    <p>Login to get instant access to our video courses</p>
    <div>SVG goes here</div>
    <ul></ul>
    <label for="email">Username</label>
    <input type="text" id="email" name="username" form="loginForm" required autofocus/>
    <label for="password">Password</label>
    <input type="password" id="password" name="password" form="loginForm" required/>
    <input type='submit' value="Login" name="login" form="loginForm">
  </div>
</form>

问题视频: https://youtu.be/OGZWfBaG_aM

我希望验证字词淡入

因为就浏览器而言,CSS 和 JavaScript 几乎同时加载,因此未检测到 transition。将 JavaScript 的最后一行更改为:

setTimeout(()=>{
  verified_text.classList.add('animated');
}, 100);

我在 React 项目中偶然发现了类似的东西。起初令人头疼,但上面提到的@StackSlave 给了我一个很好的提示,说明我遇到的问题。

我有一个父组件渲染一个子组件,我试图在某个点击事件中淡出子组件。 没用

起初我有以下设置:

const Parent = () =>{

const Child = () =>( 
    <Image
    ...
    className={clsx(s.image, !showPoster && s.hidden)}
    ...
    />
)

 return(
  <>
    ...
    <Child/>
    ...
  </>)
}

然后我将上面的内容修改为以下内容,CSS 转换工作顺利。


const Parent = () =>{

 return(
  <>
    ...
    <Image
    ...
    className={clsx(s.image, !showPoster && s.hidden)}
    ...
    />
    ...
  </>)
}

样式

.image {
  ...
  opacity: 1;
  transition: all 0.2s ease-out !important;
}

.hidden {
  opacity: 0;
}