Created
June 4, 2011 19:28
-
-
Save stephancom/1008248 to your computer and use it in GitHub Desktop.
unobtrusive rollover coffeescript vs original
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
// purpose of script is to handle rollovers unobtrusively, e.g. | |
// <img src='original.png' data-rollover='rolled.png'> | |
$(function(){ | |
$('img[data-rollover]').live('mouseover', function(){ | |
if($(this).data('rollover-original') == null) { | |
$(this).data('rollover-original', $(this).attr('src')) | |
} | |
$(this).attr('src', $(this).data('rollover')) | |
}).live('mouseout', function(){ | |
$(this).attr('src', $(this).data('rollover-original')) | |
}) | |
}) | |
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
# coffeescript version | |
# eh, it's not that much nicer. Maybe if I could replace '$(this)' with something like '$@' ? | |
# but then it starts looking like perl :( | |
$ -> | |
$('img[data-rollover]').live 'mouseover', -> | |
$(this).data 'rollover-original', $(this).attr('src') unless $(this).data('rollover-original')? | |
$(this).attr 'src', $(this).data('rollover') | |
.live 'mouseout', -> $(this).attr 'src', $(this).data('rollover-original') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is worth noting that I am now a coffeescript convert - today I would no sooner use plain JS instead of coffeescript than I would use html or erb instead of haml or slim.