Omar G. Goda
Omar G. Goda

Reputation: 41

CSS Opacity Transition not Working on Adding Class

I a problem doing a simple css opacity transition on the selector #loginForm > div:first-child h1 by simply adding the class .animated to that element using javascript. Although it is not working. Below is the code:

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>

video of the problem: https://youtu.be/OGZWfBaG_aM

I want the word verified to fade in

Upvotes: 0

Views: 650

Answers (2)

KeshavDulal
KeshavDulal

Reputation: 4144

I stumbled on something similar on a React project. It was head-scratching at first but what @StackSlave mentioned above gave me a good hint about the issue I was having.

I had a parent component rendering a child component and I was trying to fade out the child component on a certain click event. It wasn't working

I had the following setup at first:

const Parent = () =>{

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

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

I then modified above to the following and the CSS transition worked smoothly.


const Parent = () =>{

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

styles

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

.hidden {
  opacity: 0;
}

Upvotes: 0

StackSlave
StackSlave

Reputation: 10627

Because the CSS and JavaScript load at virtually the same time as far as the Browser is concerned, a transition is not detected. Change that last line of JavaScript to something like:

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

Upvotes: 3

Related Questions