Created
February 28, 2014 10:29
-
-
Save Ahrengot/9268753 to your computer and use it in GitHub Desktop.
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
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