Skip to content

Instantly share code, notes, and snippets.

@minodisk
Created June 7, 2012 08:38
Show Gist options
  • Select an option

  • Save minodisk/2887443 to your computer and use it in GitHub Desktop.

Select an option

Save minodisk/2887443 to your computer and use it in GitHub Desktop.
autoslide and swipe
$scroller = $('#scroller')
$children = $scroller.find('*')
childWidth = $($children[0]).width()
numChildren = $children.length
scrollerWidth = childWidth * numChildren
#autoslide
fixPos = (delta)->
left = parseFloat($scroller.css('left'))
if delta > 0 and left <= -scrollerWidth
while left <= -scrollerWidth
left += scrollerWidth
else if delta < 0 and left >= 0
while left >= 0
left -= scrollerWidth
$scroller.css('left', left)
slide = (delta = 1)->
selectedIndex += delta
while selectedIndex < 0
selectedIndex += numChildren
$scroller.stop(true, true)
fixPos delta
do (delta)->
$scroller
.animate({
left: -childWidth * selectedIndex
}, 1000, 'easeOutExpo', ->
fixPos delta
)
selectedIndex %= numChildren
$blightDot.remove()
for dot, i in $pager.find('li')
if i is selectedIndex
$blightDot.insertBefore(dot)
return
$blightDot.appendTo($pager)
autoSlideID = null
startAutoSlide = ->
stopAutoSlide()
autoSlideID = setInterval slide, 6000
stopAutoSlide = ->
if autoSlideID? then clearInterval autoSlideID
autoSlideID = null
startAutoSlide()
#swipe
startX = null
startSwipe = (e)->
e.preventDefault()
stopAutoSlide()
startX = getX e
$(window)
.bind('mousemove', progressSwipe)
.bind('touchmove', progressSwipe)
.bind('mouseup', stopSwipe)
.bind('touchend', stopSwipe)
progressSwipe = (e)->
e.preventDefault()
x = getX e
if (deltaX = x - startX) < -50
slide 1
stopSwipe e
else if deltaX > 50
slide -1
stopSwipe e
stopSwipe = (e)->
e.preventDefault()
startAutoSlide()
$(window)
.unbind('mousemove', progressSwipe)
.unbind('touchmove', progressSwipe)
.unbind('mouseup', stopSwipe)
.unbind('touchend', stopSwipe)
getX = (e)->
if e.originalEvent.touches?[0]?.pageX? then e.originalEvent.touches[0].pageX else e.pageX
@minodisk

minodisk commented Jun 7, 2012

Copy link
Copy Markdown
Author

何回も書いてる気がする

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment