Skip to content

Instantly share code, notes, and snippets.

@jescalan
Created February 23, 2012 21:59
Show Gist options
  • Select an option

  • Save jescalan/1895286 to your computer and use it in GitHub Desktop.

Select an option

Save jescalan/1895286 to your computer and use it in GitHub Desktop.
imgfade
$.fn.imgFade = (time) ->
# set default time
time ?= 500
# wrap it
this.wrap("<div class='#{this.attr('class')}' />")
wrapper =
this
.parent()
.css({ 'background-image': 'none', position: 'relative' })
this.css({ margin: 0, padding: 0, position: 'absolute', top: 0, left: 0 })
hover_state =
this
.clone()
.css({ margin: 0, padding: 0, position: 'absolute', top: 0, left: 0, opacity: 0, 'background-position': "0 -#{this.height()}px" })
.appendTo(wrapper)
wrapper.hover =>
this.stop(true, false).delay(time/2).animate({ opacity: 0 }, time)
hover_state.stop(true, false).animate({ opacity: 1 }, time)
, =>
this.stop(true, false).animate({ opacity: 1 }, time)
hover_state.stop(true, false).delay(time/2).animate({ opacity: 0 }, time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment