Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure JavaScript fade in function

Hi friends i want to fade in a div when i click on another div and for that i am using following code. Code1 works fine but i require to use the Code2.

I know there is jQuery but i require to do this in JavaScript

Can you guide me that what kind of mistake i am doing or what i need change...

Code1 --- Works Fine

function starter() { fin(); }

function fin()
{
    for (i = 0; i <= 1; i += 0.01)
    {
        i=Math.round(i*100)/100;
        setTimeout("seto(" + i + ")", i * 1000);
    }
}

function seto(opa)
{
    var ele = document.getElementById("div1");
    ele.style.opacity = opa;
}

Code2 --- Does not work

function starter()
{
    var ele = document.getElementById("div1");
    fin(ele);
}
function fin(ele)
{
    for (i = 0; i <= 1; i += 0.01)
    {
        i=Math.round(i*100)/100;
        setTimeout("seto(" + ele + "," + i + ")", i * 1000);
    }
}

function seto(ele,opa)
{
    ele.style.opacity = opa;
}
like image 400
kanudo Avatar asked Apr 23 '14 12:04

kanudo


People also ask

How do I fade something in JavaScript?

The jQuery fadeIn() method is used to fade in a hidden element. Syntax: $(selector). fadeIn(speed,callback);

How do I fade a box in JavaScript?

Use JavaScript (element. style. display = "none") to remove the fadeout element from document flow.

What is a fade function?

The Less Fade function is used to set the transparency of the color for selected elements. The parameters used for fade function are: color: It is used to specify color object. amount: Its percentage varies between 0 - 100%.

What are the variables declared in fadein () function in JavaScript?

Now declaring 3 variables in fadeIn () function: fade: It is to fetch the part on which fade-in effect to be applied. opacity: It is to deal with the opacity of fetched portion. intervalID: It is to deal with complete logic and to terminate logic.

How do I make a fade effect with JavaScript?

A fade effect can be easily done with CSS transition and setting the opacity with Javascript: Attach CSS opacity transition on the element – <div id="demo" style="transition: opacity 1s">DEMO</div>. Set 0 opacity to fade out – document.getElementById ("demo").opacity = 0. Set 1 opacity to fade in – document.getElementById ("demo").opacity = 1.

What is the use of fade-out and fade-in effect in JavaScript?

Fade-out and Fade-in effect is wonderful to use in web projects as it emphasizes on the styling of the web page. We are using setInterval () method and clearInterval () method in these logics.

What can a pure function not do in JavaScript?

There is a set of things that a pure function can’t do A pure function can’t modify a global variable. A pure function should not have its output dependent on an external value or a global variable. A pure function can’t manipulate the DOM (because you are “mutating” something outside of the function scope.)


1 Answers

Based on this site

EDIT-1
Added the functionality so that user can specify the animation duration(@Marzian comment)

You can try this:

function fadeIn(el, time) {
  el.style.opacity = 0;

  var last = +new Date();
  var tick = function() {
    el.style.opacity = +el.style.opacity + (new Date() - last) / time;
    last = +new Date();

    if (+el.style.opacity < 1) {
      (window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
    }
  };

  tick();
}

var el = document.getElementById("div1");
fadeIn(el, 3000); //first argument is the element and second the animation duration in ms

DEMO

like image 53
laaposto Avatar answered Sep 23 '22 11:09

laaposto