Created
August 11, 2011 22:43
-
-
Save hamstarr/1140991 to your computer and use it in GitHub Desktop.
rumble js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This jQuery plugin will cause the targeted | |
// elements to rumble when they are moused over. | |
(function($) { | |
// Default config options | |
$.fn.rumble = function(conf){ | |
var config = $.extend({}, $.fn.rumble.defaults, conf); | |
// Loop over each of the elements in the jQuery | |
// stack so that we can set up the properties | |
// for the individual elements. | |
return this.each(function(){ | |
var o = $(this); | |
var trigger = $(config.trigger); | |
var intTop = 0; | |
var intLeft = 0; | |
var isRumbling = false; | |
var updatePosition = function(){ | |
var intCurrentTop = parseInt(o.css( "top" )); | |
var intCurrentLeft = parseInt(o.css( "left" )); | |
if (isRumbling){ | |
if (Math.random() > 0.5){ | |
intCurrentTop = (intCurrentTop > intTop) ? (intTop - config.distance) : (intTop + config.distance); | |
} else { | |
intCurrentLeft = (intCurrentLeft > intLeft) ? (intLeft - config.distance) : (intLeft + config.distance); | |
} | |
// Set a timeout to call this method again and update position. | |
setTimeout(updatePosition, config.speed); | |
} else { | |
// Reset position. | |
intCurrentLeft = intLeft; | |
intCurrentTop = intTop; | |
} | |
// Update the position. | |
o.css("top", (intCurrentTop + "px")); | |
o.css("left", (intCurrentLeft + "px")); | |
} | |
trigger.mouseover(function(){ | |
isRumbling = true; | |
var rumbleTimeOut = ''; | |
// IE users dont get to see the rumble as I can't be arsed trying to figure out what the problem is! | |
$.each($.browser, function(i) { | |
if(!$.browser.msie){ | |
rumbleTimeOut = window.setTimeout(updatePosition, config.delay); | |
} | |
}); | |
setTimeout("jQuery('.snow-flakes').fadeOut(4000);"); | |
setTimeout("jQuery('.xmas-text').fadeOut(2000);",1000); | |
setTimeout("jQuery('.xmas-card-front').fadeOut(2000, function(){isRumbling = false;});",3000); | |
//if (rumbleTimeOut != '') { | |
; | |
//}; | |
}); | |
}); | |
}; | |
$.fn.rumble.defaults = { | |
speed: 60, | |
distance: 3, | |
trigger: '', | |
delay: 0 | |
}; | |
})(jQuery); | |
jQuery(document).ready(function($) { | |
$('.snow-flakes, .xmas-text').rumble({ | |
trigger: '.xmas-card-front, .snow-flakes, .xmas-text' | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment