admin管理员组

文章数量:1316013

On vanilla-js they show an example of a fade out.

var s = document.getElementById('thing').style;
s.opacity = 1;
(function fade(){(s.opacity-=.1)<0?s.display="none":setTimeout(fade,40)})();

I tried this example but it didn't work (it doesn't pletly fade out: /

Also i didn't quite understand what this exactly does: (s.opacity-=.1)<0 .. what does the < sign do?

I really liked this code snippet. Does someone know more resources of jquery stuff done in javascript instead of the unminified jquery...?

Thanks

On vanilla-js. they show an example of a fade out.

var s = document.getElementById('thing').style;
s.opacity = 1;
(function fade(){(s.opacity-=.1)<0?s.display="none":setTimeout(fade,40)})();

I tried this example but it didn't work (it doesn't pletly fade out: http://jsfiddle/qvKW7/2/

Also i didn't quite understand what this exactly does: (s.opacity-=.1)<0 .. what does the < sign do?

I really liked this code snippet. Does someone know more resources of jquery stuff done in javascript instead of the unminified jquery...?

Thanks

Share Improve this question edited Jul 15, 2013 at 14:09 user1081577 asked Jul 15, 2013 at 13:22 user1081577user1081577 4692 gold badges8 silver badges25 bronze badges 2
  • What do you mean by "doesn't pletely fade"? It works fine for me. What browser are you using? – Bergi Commented Jul 15, 2013 at 13:43
  • @bergi it doesn't pletly fadeout in chrome version 28.0.1500.71 m. It stops at opacity: 0.10000000000000014 – user1081577 Commented Jul 15, 2013 at 14:05
Add a ment  | 

5 Answers 5

Reset to default 6

Explaining the function:

(//this and the bottom closure executes the function inside.
 function fade(){//the function is named fade
  (s.opacity-=.1)//sets the opacity to current opacity-0.1, 
   <0?  // if the opacity - 0.1 is smaller than 0 ('<' = smaller then)
  s.display="none" // set display to none
   : // else
  setTimeout(fade,40) // set a timer of 40 ms and execute the function fade again
 }
)();

this is a nice javascript way to play around with a style but:

1.it's written enterly in javascript

with css3 you have such animations without the need of javascript and the cool part of it is that the browser (with some tricks) uses hw gpu acceleration and so the animation is very fluid. if you need this type of animation in very old browsers with no css3 support then yeah u need this but also some polyfills and the use of ie filter to set the opacity.

2.it sets the display to none

setting an element to display:none is not a good way to do it if u plan to reuse the element. becaue if u want to show it again it has to redraw it.

3.it uses a setTimeout

setTimeout is as bad choice always.

Now to the answer:

i don't know exactly what do you wanna achieve but

look at this example (this works in chrome and safari ,android and ios) but can be changed towork on most browsers.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>example</title>
<style>
div{
 background-color:#fc0;
 -webkit-transition:opacity 1500ms ease;
 opacity:1;
}
div.hide{
 opacity:0;
}
</style>
<script>
var changeclass=function(){
 this.classList.add('hide');
}
window.onload=function(){
 var firstDiv=document.getElementsByTagName('div')[0];
 firstDiv.addEventListener('mouseover',changeclass,false);
}
</script>
</head>
<body>
<div>Hello</div>
</body>
</html>

i create a css class for my element. div

inside this div i set the css3 property transition toanimate the opacity with a easing and 1500 milliseconds

and an extra class to hide it div.hide

this contains only the opacity set to 0.

now in my example i hide the element on mouseover.

so when the my div element has loaded (window.onload) i add an eventhandler (mouseover)

to change the class of my div element, which i called changeclass.

What browser are you testing in? It works for me in Firefox 21, but the < in there is part of the ternary statement. The code you have posted is the same as:

var s = document.getElementById('thing').style; // s holds the style of 'thing'
s.opacity = 1; // sets the opacity to be fully opaque
(function fade() { // function will automatically execute itself
    if ((s.opacity-=.1) < 0) // decrements the opacity by .1 AND checks if the opacity is less than 0
        s.display="none"; // if the opacity has dropped below 0, hide the element altogether
    else
        setTimeout(fade,40);  // otherwise, run this function again in 40ms
})();

Also, maybe try (s.opacity-=.1) < .1

You could try decrementing a variable and setting the opacity to that value:

var s = document.getElementById('thing').style;
var o = 1;
(function fade(){
    s.opacity = o-=0.1;
    o < 0 ? s.display="none" : setTimeout(fade,40);
})();

(s.opacity-=.1)<0 .. what does the < sign do?

It's the mon smaller-than parison. The author wants to check when the opacity reaches zero. Yet, floating point math is inprecise, so ==0 won't do it.

This is probably late but I didn't find the answer to the original question here and no one seems to have addressed it anywhere else.

When fading out, it is safer to use 0.1 as the minimum value in the conditional statement like this:

(s.opacity-=.1) < .1 

because

(s.opacity-=.1)

should actually evaluate to zero at the end of the animation and not a negative value (which is required for this to work:

(s.opacity-=.1) < 0 // s.opacity cannot be < 0... 

You could also use:

(s.opacity-=.1) <= 0 

but I prefer:

(s.opacity-=.1) < .1

I also prefer to replace

s.display="none" 

with

s.opacity=0

Final code:

// fade out...
// This way you do not end up with an inplete fade...

var s = document.getElementById('thing').style;
s.opacity = 1;

(function fade(){
  (s.opacity-=.1)<0.1 ? s.opacity=0 : setTimeout(fade,40) })();

When fading in, use 0.9 for the maximum opacity value like this:

(s.opacity+=.1) > .9 

Because

(s.opacity+=.1) 

can not evaluate to a value greater than 1. As before you can also do this:

(s.opacity+=.1) >= 1 // but I prefer > .9 ...

And finally set s.opacity to 1 at the end of the animation.

// fade in...

var s = document.getElementById('thing').style;
s.opacity = 0;

(function fade(){ (s.opacity+=.1) > 0.9 ? s.opacity=1 : setTimeout(fade,40) })();

本文标签: javascript fade out (vanilla js example) not working for meStack Overflow