Skip to content

Instantly share code, notes, and snippets.

@Incognito
Forked from joshuasiler/robotButton.js
Created November 4, 2011 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Incognito/1339425 to your computer and use it in GitHub Desktop.
Save Incognito/1339425 to your computer and use it in GitHub Desktop.
Javascript function that adds a little surprise to your HTML elements.
/* USAGE EXAMPLE
<style>
.example { padding:15px; border: 2px solid #888; width:100px;float:left;margin-left: 5px; font-size: 50px; margin-top: 50px; }
</style>
<div id="example1" class="example">1</div>
<div id="example2" class="example">2</div>
<div id="example3" class="example">3</div>
<div id="example4" class="example">4</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script type="text/javascript" src="/javascript/robotButton.js"></script>
<script>
$(document).ready(function() {
initRobotButton($('#example1'));
initRobotButton($('#example2'));
initRobotButton($('#example3'), function() { $('#example3').css("color","green");} );
initRobotButton($('#example4'), function() { $('#example4').css("color","gold");} );
});
</script>
*/
function initRobotButton(item, clicked_fn) {
var notclicked = true;
var detectDist = 200;
var popupHeight = 85;
item.after('<div id="littlerobot-' + item.attr("id") + '">&nbsp;</div>');
var lilrobot = $('#littlerobot-' + item.attr("id"));
lilrobot.css({
"z-index": 100,
"background-position": "0px -540px",
"position": "absolute",
"background-image": "url(/images/robot_sprites_sm.png)",
"font-size": "0em",
"padding": popupHeight + "px 75px 0px 0px",
"background-repeat": "no-repeat"
});
$(document).mousemove(function (e) {
if (notclicked) {
var item_loc = item.offset();
var item_dim = {
h: item.height(),
w: item.width()
};
var item_center = {
y: item_loc.top + (item_dim.h / 2),
x: item_loc.left + (item_dim.w / 2)
};
lilrobot.offset({
top: item.offset().top - popupHeight,
left: item.offset().left + (item_dim.w / 2)
});
var distY = Math.abs(e.pageY - item_center.y) - (item_dim.h / 2);
var distX = Math.abs(e.pageX - item_center.x) - (item_dim.w / 2);
var dist = (distY < distX ? distX : distY);
if (distX < detectDist && distY < detectDist) {
lilrobot.css("background-position", "0px " + ((dist / detectDist) * popupHeight) + "px");
} else {
lilrobot.css("background-position", "0px -540px");
}
}
});
item.click(function (e) {
notclicked = false;
lilrobot.css("background-position", "-80px 0px");
if (typeof clicked_fn === "function") {
clicked_fn();
}
});
}
/*
Copyright (c) 2011 Joshua Siler
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment