0

I am trying to create this animation where the title is visible in the page initially then when you scroll down you trigger the title to slowly fade away and a subtitle fades in right after. I have the title part working but I can't seem to get the subtitle to appear with a smooth transition. At first I have my subtitle at "visibility:hidden" then when I scroll and the javascript adds the transition in class, it just abruptly comes in disregarding the transition property I gave it. Here is the fiddler I set up. Below is the javascript and css (respectively) i'm using to get this animation to work. Of course if there area any easier ways to achieve this feel free to let me know. Any advice or help will be GREATLY appreciated I have been researching and trying things to no avail.

Javascript

const element = document.getElementById('title');
const element2 = document.getElementById('subtitle');

window.onscroll = function() {

  console.log("document element");
  console.log(document.documentElement.scrollTop);
  console.log("scrolling elemnent");

  if (window.scrollY > 0) {
    element.classList.add('fadeout');
    element2.classList.add('fadein');
    console.log("hello");
  }
}
.fadeout {
  visibility: hidden;
  opacity: 0;
  transition: visibility 0s 2s, opacity 2s linear;
}

.two {
  visibility: hidden;
}

#subtitle {
  transition: opacity 2s linear;
}

.fadein {
  visibility: visible;
  opacity: 1;
  transition: opacity 2s linear;
}

1 Answer 1

2

Currently your subtitle is at full opacity when you are fading it in (Because the visibility property does not set the opacity it just makes the element invisible)

Add opacity:0; to the .two CSS so that it will fade in.

Updated fiddle https://jsfiddle.net/s2cban6q (line 32 of CSS changed)

Sign up to request clarification or add additional context in comments.

4 Comments

Ahh makes sense! can't believe I missed that thank you so much!
Glad I could help!
Sorry to bother but real quick, why is it that I can't slap on the fade out class on the subtitle? like it fades in then lets say after 600px I want it to fade out so I add the fade out class to it but nothing happens?
Currently .fadein will override .fadeout this is because in your CSS file the declaration of .fadein come BEFORE .fadeout. Which ever comes first will be overridden. You COULD swap the location in the file of the CSS but I would recommend instead removing the .fadein from the element when you want it to fade out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.