6

I am a new-newbie in JavaScript and I am trying to write a code that fades-in a picture in commands that I am familiar with. I've seen several examples here but they didn't work. This is what I tried to do:

 function myFunction() {
   for (i = 1; i < 20; i++) {
     setTimeout(function() {
       o = 1 - 1 / i
     }, 200); //this should increase the opacity
     document.getElementById("dog").style.opacity = o
   }
 }
img {
  opacity: 0;
  filter: alpha(opacity=40);
}
<center>
  <img id="dog" src="dog.jpg" draggable="true" ondragstart="drag(event)" width="500" height="500">
</center>

<button onclick="myFunction()">Lets Rock</button>

when I ran it, it doesn't do fade in. it starts with blank screen (as it should), but than instead of fading in after I click the button, it just pops out (without fading in) after a few clicks.

thank you very much for help given
Ariel

5
  • 2
    How old is this example? You shouldn't use the <center> tag, it's deprecated Commented Nov 3, 2015 at 17:09
  • 1
    This might help: jsfiddle.net/mejyj2w9/1 Commented Nov 3, 2015 at 17:15
  • All your timeout are fired at the same time, aka 100 milliseconds after they are declared. Try: setTimeout(function(){o=1-1/i}, 100 * i); And dd the opacity thing in the function of the timeout. Commented Nov 3, 2015 at 17:20
  • thank you very much for the help Commented Nov 3, 2015 at 17:35
  • 1
    Possible duplicate of How to do fade-in and fade-out with JavaScript and CSS Commented Nov 3, 2015 at 17:37

2 Answers 2

6

Try this : http://jsfiddle.net/kishoresahas/4wg8zcsg/

function fadeIn(el) {
    el.style.opacity = 0;
    var tick = function () {
        el.style.opacity = +el.style.opacity + 0.01;
        if (+el.style.opacity < 1) {
            (window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16)
        }
    };
    tick();
}

function myFunction() {
    var el = document.getElementById("dog");
  console.log(el);
    fadeIn(el);
}
img {
    opacity: 0;
    filter: alpha(opacity=40);
}
<button onclick="myFunction()">Lets Rock</button>

<center>
    <img id="dog" src="https://i.sstatic.net/NHlV8.jpg" draggable="true" ondragstart="drag(event)" width="500" height="500">
</center>

Sign up to request clarification or add additional context in comments.

2 Comments

Scratch that, your SO snippet simply requires a scroll down. Maybe switch the button with the image position? Just for clarity. otherwise +1
@somethinghere the button click not getting triggered in snippet , but works perfectly in jsfiddle. any idea?
5

You might find it easier to control the opacity using CSS. Its less work for the browser and only requires minimal javascript to toggle the class.

var img = document.getElementById("dog"),
    btn = document.getElementById("button");

btn.addEventListener("click", function( event ) {
    img.className = (img.className==="in")?"out":"in";
}, false);
#dog {
  opacity: 0;
  -webkit-transition: opacity 2s ease;
  -moz-transition: opacity 2s ease;
  -ms-transition: opacity 2s ease;
  -o-transition: opacity 2s ease;
  transition: opacity 2s ease;
}
#dog.in {
    opacity: 1;
}
<button id="button">Lets Rock</button><br />
<img id="dog" src="http://lorempixel.com/500/500/cats/" class="off" draggable="true" ondragstart="drag(event)" width="500" height="500">

3 Comments

@cale_b Although I agree I still upvoted as this is the kind of proper solution people should aim for. I do agree that the OP asked for JS.
@cale_b Noted, although part of learning javascript is knowing when not to use it ;)
@somethinghere - While you are both correct and I recognize this is a far simpler / superior solution, this logic also means that this question is fair game for jQuery or other framework solutions, yes? I guess my point is - let's answer the question the way they asked, or else (in comments) tell them there's a better way. The OP could simply be asking the question in an attempt to learn how to do things, not just to achieve the outcome. (There is value in writing your own "fade-in" in vanilla javascript, if for no other reason to recognize the value of using jQuery or CSS3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.