You can use setTimeout or setInterval to delay the hiding of your div. Below is the basic setup. In your_func put logic to hide your div. There are slight differences between these two.
//Your function will be called only once 5 seconds after invocation
setTimeout(function() { your_func(); }, 5000);
Or
//If you want your function to be called repeatedly every 5 seconds after invocation
setInterval( function(){your_func(); }, 5000);
Below is the code you can use for what you’re attempting to do. elementId is the
Id of the element you will click. You just need to create the logic for fadeout and
remove element.
As you’ve probably figured out by now 5000 is 5 milliseconds or 5 seconds. This
means that your function will execute 5 seconds after the element is clicked.
Just google how to fadeout what ever it is you want to fadeout and google how
to remove an element using vanilla javascript and put the logic where you see
my comments.
var yourElement = document.getElementById(“elementId”);
yourElement.addEventListener(“click”, function(){
setTimeout(function(){
// fadeout logic goes here
// remove an element logic goes here
}, 5000);
});
The usual way to achieve a fadeout is to just to add a classname on the element, and let CSS handle it for you. Trying to do it all with JS results in a much worse experience. CSS is the right tool to achieve the fadeout.
cover.classList.add("fadeout");
That’s all the JS code you’ll need. The rest is up to CSS transition code.
I recommend that you contact the HTML & CSS forum about using the CSS transition technique to achieve the fadeout.
While it can work with JavaScript, it is the worst tool for the job. Doing it in JS is janky too.
Don’t hammer in a nail with a bloody screwdriver. That’s what you are attempting now to do.
CSS is for the presentation of the page. Fading things in and out is what CSS is great at.
Can I see how it would be written, then decide between the two. I want to see how both work separately. Being able to see how both work visually would help.
There is no easy way to convert jquery animations to javascript. It’s a lot of bloody work with no real benefit coming from it. I will not be helping you to cut off your own leg either.
Use CSS to animate fade techniques is my best advice.
I didn’t know it was something that was hard to do. It was a short code so it seemed like it was something simple. As I am not familiar with jquery, and don’t use it in codes. I guess short codes that may seem simple to the eye, some are more difficult than others.
Does that mean you don’t want to slide and break the curtains in half anymore?
Or do you want them to slide and then fade out at the same time? There’s no point in fading them out after they have left the building as you won’t see them.
You can fade and slide in that same key frame by just adding opacity into the key frame at appropriate points.
You can also handle multiple elements with css if the class is added to a common parent. You don’t need multiple classes added if the effects are all related.
You can slide the curtains and once that is finished you can do something to another element using a delay on its animation.
Unfortunately I am away until Friday and on mobile only so can’t offer code you until I return but I’m sure if you state your use case clear enough there are people here who can help
Ok I couldn’t remember what was currently being used as jsfiddle doesn’t really work on a mobile.
We can probably achieve the same thing with transitions as you can transition properties individually and they can have their own timing and delays.
Opacity could still be included in the transition but to make the element take up no space once everything is finished would require it being moved absolutely off screen after all the effects have finished. That can also be added into the transition but with a delay that causes it to only be moved immediately after x number of seconds (the duration of the fade effect).
It all depends if I am understanding the requirements properly ?