Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
164
rated 0 times [  171] [ 7]  / answers: 1 / hits: 6128  / 11 Years ago, mon, february 10, 2014, 12:00:00

Hey everyone I have a homework question,



I need to fade in and out an image gallery-style using JavaScript. Note: NOT JQuery. I cannot use JQuery, per the assignment outline.



So there's a grid of images (32 of em if you care) they're all 100x100px thumbnails. Each one is in its own div, and the whole thing is nested inside another div, like so:



gallery.html



<div id=imageContent>

<div id=img class=imageWhite
onclick=fade(1984)
onmouseover=highlightWhiteFunction(this)
onmouseout=unHighlightFunction(this)>
<img src=../Media/Thumbs/1984.jpg/>
</div>

...31 others just like that

</div> //End of the whole container


So when you click on one of these images, it should fade that image in over the top of everything else. The width of this picture should be 500px, but the height can vary so distortion doesn't occur. Again, I CANNOT use JQuery for this...and yes, I know that'd make life a lot easier.



So far I only have a debug thing to detect that I can at least find which one is clicked on:



gallery.js



function fade(name) {
var theName = name;
console.debug(Clicked + theName);
}


If the user clicks anywhere on this image, it needs to fade out. If the user clicks another thumbnail, it doesn't need to fade out, it can just disappear, but the other one needs to start fading in.



My thoughts:
Obviously I need a hidden div with width 500, and when these actions occur, I hide/unhide the div as necessary. The gist I've gotten from the professor is that to use JavaScript, you change the opacity in relation to a passage of time that you get from the system.



What I'm looking for in an answer here is maybe some clearer (more detailed) hints on how to go about this. I know how it needs to look, and I'm pretty sure I know the high-level of how to do it, I just don't know how to start doing it with code.



Any help would be appreciated, and I'll be around to answer any follow-up questions.



Again: NO JQuery! :)


More From » html

 Answers
7

Something like this should work



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

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();
}


Here is an example:
http://jsfiddle.net/cEDbs/



Just bind the image onclick to call that method with the element.


[#47903] Saturday, February 8, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaydon

Total Points: 651
Total Questions: 103
Total Answers: 100

Location: Norway
Member since Mon, May 23, 2022
2 Years ago
;