Jump to content
Search Community

Need help to add animation when change dispay none to block

Amir-Ali-Basic test
Moderator Tag

Recommended Posts

Hey everyone,

 

I need help to add transition on when i change display none to display block i have simple code right now , and i am new to gsap.

I have website with simular effect so can i share link of website here ?

 

const showCar = document.querySelector("#car_1");
const car = document.querySelector(".car");

showCar.addEventListener("click", () => {
if (car.style.display === "block") {
car.style.display = "none";
} else {
car.style.display = "block";
}
});

 

Link to comment
Share on other sites

As far as i know display can not be animated, what you could do is animate opacity to fade in the element.

if you could make a codepen demo that would make it a lot easier for us to help you. 

And i believe it is not a problem to share a link that shows the effect you want. As long as it is relevant to your question.

  • Like 2
Link to comment
Share on other sites

Hey Amir. As Patrick shared, the display property itself is not animatable, regardless of whether or not you're using GSAP. However, GSAP makes it easy to animate in elements that have a display of hidden. You simply need to pair a property to be animated (usually opacity or autoAlpha) with the display. For example:

// If the elem has display: none and opacity: 0
gsap.to(".hiddenElem", { display: "block", opacity: 1 });

// If the elem has display: none and opacity: 1
gsap.fromTo(".hiddenElem", { opacity: 0 }, { display: "block", opacity: 1 });

Also as Patrick said, if you'd like additional coding help please create a minimal demo using the guide that he linked to.

  • Like 1
Link to comment
Share on other sites

I'd also suggest that you use other attributes or a class to Hold the information which car is shown or hidden. Also If you use the style.display property checking for 'block' always comes with the risk that the displaced is inline-block etc. 

You really should think of a more structural approach.   

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...