Skip to content

Instantly share code, notes, and snippets.

@victorpolko
Last active August 26, 2015 17:01
Show Gist options
  • Save victorpolko/7fcfaf48898b7f18d235 to your computer and use it in GitHub Desktop.
Save victorpolko/7fcfaf48898b7f18d235 to your computer and use it in GitHub Desktop.
jQuery plugin for uniform scroll animation
# @param [jQuery Object] target Dom element to scroll to
# @param [Integer] speed Speed (pixels per second)
$.fn.scrollWithSpeed = (target, speed = 10000, easing, complete) ->
@stop().animate(
scrollTop: target.offset().top
, Math.abs($(window).scrollTop() - target.offset().top) / speed * 1000
, easing
, complete )
@victorpolko
Copy link
Author

Using this plugin with anchors:

$('a[href*=#]:not([href=#])').click ->
    $target = $(@hash) # I use dollar sign to know when it is a jQuery object
    $target =
      if $target.length > 0 # To look for anchor by id
        $target
      else # To look by name
        $("[name=#{@hash.slice(1)}]")

    $('body, html').scrollWithSpeed($target)

    false # optionally prevent default

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