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;
}
The jQuery fadeIn() method is used to fade in a hidden element. Syntax: $(selector). fadeIn(speed,callback);
Use JavaScript (element. style. display = "none") to remove the fadeout element from document flow.
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%.
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.
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.
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.
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.)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With