Skip to content

Instantly share code, notes, and snippets.

@joshuasiler
Created November 2, 2011 00:05
Show Gist options
  • Save joshuasiler/1332343 to your computer and use it in GitHub Desktop.
Save joshuasiler/1332343 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", "height" : popupHeight, "padding": "0px 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.
*/
@Incognito
Copy link

Issues:

FIFY: https://gist.github.com/1339425

Didn't fify:

  • Use a class instead of .css. Changing css is painfully slow in the DOM instead of changing the class (which is up to 95% faster).
  • Semantically this should be a plugin, and support the selector rather than locked to IDs.
  • Probably better suited to a smaller library such as zepto or ender JS for better performance, they're mostly designed to mimic the jQuery API.
  • You need to be careful, if you implement this on too many elements your browser's going to cry dealing with all of the events being bound multiple times (each call of this implements a new mousemove event, so your demo page is doing the same thing 10 times, if it did it once you could scale it out to dozens of these), Similar issue for .click exists, but less damaging as it's bound to the specific element (where as document gets bound the event n times).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment