Skip to content

Instantly share code, notes, and snippets.

@spencermathews
Last active February 5, 2016 00:51
Show Gist options
  • Save spencermathews/e9ebb05c26e21bc59c16 to your computer and use it in GitHub Desktop.
Save spencermathews/e9ebb05c26e21bc59c16 to your computer and use it in GitHub Desktop.
image demo hover preload
<img src="http://www.redrocketmedia.com/des157/images/butterfly.png" height="151" width="305" alt="butterfly" id="img1" />
<img src="http://www.redrocketmedia.com/des157/images/butterfly.png" height="151" width="305" alt="butterfly" id="img2" />
<img src="http://www.redrocketmedia.com/des157/images/butterfly.png" height="151" width="305" alt="butterfly" id="img3" />
$(document).ready(function() {
console.log("ready");
//preload
//create array to hold images
var butterflyImages = ["http://www.redrocketmedia.com/des157/images/butterfly_h.png", "http://www.redrocketmedia.com/des157/images/butterfly_hgif.gif"];
//create an empty array to hold the images as objects
var imgs = [];
//traverse the array for preloading images
for (var i = 0; i < butterflyImages.length; i++) {
//create a new image object at position of i in the array
//this is the magic that makes the image download...putting it into an object
imgs[i] = new Image();
//assign the src for each new image
imgs[i].src = butterflyImages[i];
}
//hover on butterfly img1
$('#img1').hover(function() {
//console.log("hover over");
//change source
$('#img1').attr('src', 'http://www.redrocketmedia.com/des157/images/butterfly_h.png');
$('#img1').css('opacity', 1)
}, function() { // second function
//console.log("hover out");
$('#img1').attr('src', 'http://www.redrocketmedia.com/des157/images/butterfly.png');
$('#img1').css('opacity', .5)
});
//hover on butterfly img2
$('#img2').hover(function() {
//console.log("hover over");
//fade up
$('#img2').animate({
'opacity': 1
}),
//change source
$('#img2').attr('src', 'http://www.redrocketmedia.com/des157/images/butterfly_h.png')
}, function() { // second function
//console.log("hover out");
$('#img2').animate({
'opacity': .5
}),
$('#img2').attr('src', 'http://www.redrocketmedia.com/des157/images/butterfly.png');
});
//hover on butterfly img3
$('#img3').hover(function () {
//console.log("hover over");
//fade up
$('#img3').animate({
'opacity': 1
}),
//change source
$('#img3').attr('src', 'http://www.redrocketmedia.com/des157/images/butterfly_hgif.gif')
}, function () { // second function
//console.log("hover out");
$('#img3').animate({
'opacity': .5
}),
$('#img3').attr('src', 'http://www.redrocketmedia.com/des157/images/butterfly.png');
});
//close jQuery ready function
});
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
* {
margin: 0px;
padding: 0px;
}
body {
background-color: white;
overflow: hidden;
}
img {
opacity: .5;
}
#img1,
#img2,
#img3 {
position: relative;
}
#img1 {
top: 1in;
}
#img2 {
top: 3in;
}
#img3 {
top: 4.5in;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment