Fair enough.
One point to bear in mind is that you shouldn’t use divs as buttons.
For one thing a div has little or no semantic meaning. It also doesn’t support the tabindex attribute, which puts keyboard users at a disadvantage.
You can read more here, if you’re interested: http://www.karlgroves.com/2013/05/14/links-are-not-buttons-neither-are-divs-and-spans/
OK, so first thing to do is to include the jQuery library.
This is not strictly necessary (you could of course do all of this in plain JavaScript), but it will make our lives somewhat easier.
We do this at the bottom of the page, just before the closing </body> tag.
We can also add the center() function.
And, while we’re at it, let’s give the image a class of “hidden” and define that rule in the correct place:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
.hidden{
display: none;
}
</style>
</head>
<body>
<button id="summonPokemon">Change pokemon!</div>
<img src="http://static.tumblr.com/46cb5b1d63cf84148220815beac16919/sqjwi15/uQCn06vx6/tumblr_static_tumblr_mmff7iuxxv1qhd8sao1_500.gif" class="hidden" id="pokemon">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$.fn.center = function () {
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
return this;
}
</script>
</body>
</html>
Now, the next thing to do, is to attach an event handler to the button, which deals with displaying the image:
$("#summonPokemon").on("click", function(){
// Code here will be executed when the button is clicked
});
When clicked we need to display the image and center it:
$("#pokemon").fadeIn().center();
Then after x seconds, fade it back out. This is where setTimeout comes in:
setTimeout(function(){
// Code here will execute after x milliseconds
}, x);
which gives us:
setTimeout(function(){
$("#pokemon").fadeOut()
}, 1000);
Complete code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
.hidden{
display: none;
}
</style>
</head>
<body>
<button id="summonPokemon">Change pokemon!</div>
<img src="http://static.tumblr.com/46cb5b1d63cf84148220815beac16919/sqjwi15/uQCn06vx6/tumblr_static_tumblr_mmff7iuxxv1qhd8sao1_500.gif" class="hidden" id="pokemon">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$.fn.center = function () {
this.css("position","absolute");
this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
return this;
}
$("#summonPokemon").on("click", function(){
$("#pokemon").fadeIn().center();
setTimeout(function(){
$("#pokemon").fadeOut()
}, 1000);
});
</script>
</body>
</html>
HTH