5

jQuery derived from javascript have beautiful method of creating fade effect.

But how can I achieve same height using my own custom javascript code?

I wanted the most simple one.. which fades out to zero.

Zed
  • 3,387
  • 19
  • 14
HTweaks
  • 81
  • 1
  • 1
  • 4
  • 5
    why you want to reinvent the wheel by yourself? – Furqan Hameedi Feb 24 '11 at 11:36
  • 7
    Maybe to get a lightweight app, jquery is "kind of" heavy comparing to a 10 line script ... reinvent the wheel no, but avoid killing a bee with a flame-thrower yes ... – yent Feb 24 '11 at 11:41
  • I'll forward the same question to the one who've assigned me this project ;). And obviously, it will be credited to you. – HTweaks Feb 24 '11 at 11:41
  • Possible duplicate of [Fade HTML element with raw javascript](http://stackoverflow.com/questions/2695462/fade-html-element-with-raw-javascript) – rogerdpack Apr 17 '17 at 17:04
  • If you want to know how to write the fade effect in pure JavaScript then I recommend reading [this source code](https://github.com/jquery/jquery/blob/master/src/effects.js#L107) because they did it really well. – Zed Feb 24 '11 at 11:44

5 Answers5

4

Here is a pure implementation of fade in and fade out effect using JavaScript.

  1. Make use of CSS Opacity property

  2. During fade out process, we reduce the opacity value from 0.9 to 0

  3. Similarly fade in process increase the 0.0 to 0.9

  4. For IE its 10 to 90

  5. Use setTimeout() function to call the fade function recursively with delay and increase / decrease opacity value to achieve the fade effect

      function fadeOut(id,val){ if(isNaN(val)){ val = 9;}
      document.getElementById(id).style.opacity='0.'+val;
      //For IE
      document.getElementById(id).style.filter='alpha(opacity='+val+'0)';
      if(val>0){
        val--;
        setTimeout('fadeOut("'+id+'",'+val+')',90);
      }else{return;}
    }
    
    function fadeIn(id,val){
    // ID of the element to fade, Fade value[min value is 0]
      if(isNaN(val)){ val = 0;}
      document.getElementById(id).style.opacity='0.'+val;
      //For IE
      document.getElementById(id).style.filter='alpha(opacity='+val+'0)';
      if(val<9){
        val++;
        setTimeout('fadeIn("'+id+'",'+val+')',90);
      }else{return;}
    }
    

Cheers -Clain

Clain Dsilva
  • 1,631
  • 3
  • 25
  • 34
1

Try this :

function fade(what, duration) {
  what.opct = 100;
  what.ih = window.setInterval(function() {
    what.opct--;
    if(what.opct) {
      what.MozOpacity = what.opct / 100;
      what.KhtmlOpacity = what.opct / 100;
      what.filter = "alpha(opacity=" + what.opct + ")";
      what.opacity = what.opct / 100;
    }else{
      window.clearInterval(what.ih);
      what.style.display = 'none';
    }
  }, 10 * duration);
}

Use it like :

fade(htmlobject, 2); // delay is in second
yent
  • 1,303
  • 1
  • 8
  • 10
1

Creating animations is quite a delicate task. You have to take care of browser differences in handling CSS properties. Then you have to be sure you know how to work with timers, because they are usually not very accurate in Javascript. In short it will be easy to write a simple fading effect, but it will take a fair amount of work to make one that is comparable to jQuery.

You can read this (well, you have to wait for it to be finished) to have a better idea of how jQuery is structured, and then try to rool your own.

Andrea
  • 20,253
  • 23
  • 114
  • 183
1

you can define a color with hexadcimal system or ordinary decimal system. An example that uses the hexadecimal system

BODY {background-color:#FF00FF;}

and an example that uses the decimal system

BODY {background-color:rgb(51,0,102)}

The definition rgb(255,255,255) represents the color white, the code rgb(0,0,0) represents black.

So how you can made a fade effekt? Well, the easiest way is to use the way with decimal code:

<script type="text/javascript">
 var red = 255;
 var green = 255;
 var blue = 255;
 var interVal = window.setInterval("fadeEffect()", 1000);
 function fadeEffect() {
  if (red > 0 && green > 0 && blue > 0) red--;
  if (red == 0 && green > 0 && blue > 0) green--;
  if (red == 0 && green == 0 && blue > 0) blue--;
  if (red == 0 && green == 0 && blue == 0) window.clearInterval(interVal);

  //Creates the new code of color
  colorAttr = "rgb("+red+","+green+","+blue+")";
  //However or whereever you make the new color public
  ...

  //Reset the first two colors
  if (red == 0) red == 255;
  if (green == 0) green = 255;
 }
</script>

Hopefully it will answer your question or help you to come up with your own idea. If you want to use hexadecimal numbers, then you had to convert into hexa code before you had created the new argument.

Reporter
  • 3,897
  • 5
  • 33
  • 47
0

Why not using CSS3 transitions?

...

opacity: 100;
-webkit-transition: opacity .5s ease-out;
-moz-transition: opacity .5s ease-out;
-o-transition: opacity .5s ease-out;
transition: opacity .5s ease-out;
SI Web Design
  • 785
  • 5
  • 3