Skip to content

Instantly share code, notes, and snippets.

@Ahrengot
Created February 28, 2014 10:29
Show Gist options
  • Save Ahrengot/9268753 to your computer and use it in GitHub Desktop.
Save Ahrengot/9268753 to your computer and use it in GitHub Desktop.
define ["underscore"], (_) ->
class ResponsiveImagesController
currImgSize: "small"
breakpoint: 480
constructor: (@images) ->
@debouncedHandleResize = _.debounce( @handleResize, 500 )
@bindEvents()
@handleResize()
bindEvents: ->
$(window).on( "resize.responsive-images", @debouncedHandleResize )
handleResize: =>
w = window.innerWidth
if w < @breakpoint and @currImgSize isnt "small"
@currImgSize = "small"
@updateImageSrc()
else if w >= @breakpoint and @currImgSize isnt "large"
@currImgSize = "large"
@updateImageSrc()
updateImageSrc: ->
targetSrc = if @currImgSize is "small" then "data-src" else "data-src-lg"
@images.each -> @setAttribute( "src", @getAttribute targetSrc )
$(document).trigger( "vh/update_responsive_images", [@currImgSize] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment