【问题标题】:CSS Opacity Transition not Working on Adding ClassCSS不透明度过渡不适用于添加类
【发布时间】:2020-10-15 21:14:16
【问题描述】:

我在选择器#loginForm > div:first-child h1 上进行简单的css 不透明度转换时遇到问题,只需使用javascript 将.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

我希望验证字淡入

【问题讨论】:

  • 我把你的逻辑放在一个可运行的 sn-p 中。什么不工作?
  • 似乎工作正常
  • 我希望通过将动画类添加到其 h1 元素来验证单词使用其不透明度属性淡入
  • 我会附上问题的视频
  • 尝试在添加动画类之前添加这一行document.body.offsetHeight;

标签: javascript html css web web-frontend


【解决方案1】:

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

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

【讨论】:

  • 哦,谢谢,太棒了,但是 CSS 不应该在页面顶部加载吗?
  • 你会认为这是有道理的。现在你只需要说服浏览器供应商。
【解决方案2】:

我在一个 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-27
    • 2020-07-31
    • 2020-06-26
    • 2021-07-26
    • 2020-05-29
    • 2020-11-22
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多