Skip to content

Instantly share code, notes, and snippets.

@Ahrengot
Created August 2, 2013 12:04
Show Gist options
  • Save Ahrengot/6139388 to your computer and use it in GitHub Desktop.
Save Ahrengot/6139388 to your computer and use it in GitHub Desktop.
class SliderControl
constructor: (@el) ->
# Setup references
@progbar = @el.find '.progress'
@handle = @el.find '.handle'
@progX = @el.offset().left
@progW = @el.width()
@percent = 0
@progress = 0
@isDragging = no
@mouseDown = no
# Bind events
@handle.on 'mousedown', @startDrag
@el.on 'scrubbing:tick', @updateProgress
@el.on 'click', @drag
$(window).on 'resize', self: this, @handleResize
startDrag: (e) =>
@isDragging = yes;
@mouseDown = yes;
@el.off 'click'
$(window).on 'mousemove', @drag
$(window).on 'mouseup', @stopDrag
stopDrag: (e) =>
@mouseDown = no;
$(window).off 'mousemove', @drag
$(window).off 'mouseup', @stopDrag
setTimeout =>
@el.on 'click', @drag
, 500
drag: (e) =>
newPerc = (e.pageX - @progX) / @progW
# Round to 1 decimal. ie 68.3% instead of just 68%
newPerc = Math.round(newPerc * 1000) / 10
if newPerc < 0 then newPerc = 0
else if newPerc > 100 then newPerc = 100
if e.type is 'mousemove'
# User is scrubbing
@percent = newPerc;
TweenMax.to this, 0.5,
progress: newPerc
overwrite: 'all'
onComplete: =>
return if @mouseDown
@$isDragging = no
@el.trigger 'scrubbing:complete'
onUpdate: =>
@el.trigger 'scrubbing:tick', this.progress
ease: Power2.easeOut
else
# User clicked
@percent = newPerc
TweenMax.to this, 0.6,
progress: newPerc
overwrite: 'all'
onComplete: =>
@el.isDragging = false
@el.trigger 'scrubbing:complete'
onUpdate: =>
@el.trigger 'scrubbing:tick', this.progress
ease:Back.easeOut
handleResize: (e) =>
@progX = @el.offset().left
@progW = @el.width()
updateProgress: =>
if @progress > 100 then @progress = 100
else if @progress < 0 then @progress = 0
@progbar.css 'width', @progress + '%'
@handle.css 'left', @progress + '%'
setProgress: (prog) ->
@progress = prog
@updateProgress()
destroy: ->
clearInterval @addedToStageListener
$(window).off 'resize', @handleResize
@el.off()
@progbar = null;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment