Created
December 17, 2012 11:44
-
-
Save geta6/4317674 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # | |
| # Usage: | |
| # ($ '#main').smoothsnap snap: 'h3' | |
| # or | |
| # ($ '#main').smoothsnap snap: $ 'h3' | |
| ( | |
| ($) -> | |
| class SmoothSnap | |
| contructor: (selector) -> | |
| selector = $ selector | |
| for i in [0...selector.length] | |
| @points.push selector[i].offsetTop | |
| points: [] | |
| point: (point) -> | |
| return null if @points.length is 0 | |
| return @points[0] if @points.length is 1 | |
| dist = null | |
| dist_p = Math.abs point - @points[0] | |
| for i in [0...@points.length] | |
| dist = Math.abs point - @points[i] | |
| return @points[i - 1] if dist_p < dist | |
| dist_p = dist | |
| return @points[@points.length - 1] | |
| distance: (num) -> | |
| if num > 0 then return Math.exp num / 30 | |
| if num < 0 then return -1 * Math.exp Math.abs(num) / 30 | |
| return 0 | |
| defaults = | |
| target: ($ 'body') | |
| snap: null | |
| state = | |
| mouse: null | |
| target: null | |
| mousePress: no | |
| $.fn.smoothsnap = (opts, callback) -> | |
| $.extend defaults, opts | |
| callback or= -> | |
| smoothsnap = new SmoothSnap opts.snap | |
| @css | |
| '-webkit-user-select': 'none' | |
| '-moz-user-select': 'none' | |
| @on 'mousedown', (event) -> | |
| state.mouse = event.screenY | |
| state.target = window.scrollY | |
| state.mousePress = on | |
| @on 'mouseup', (event) -> | |
| state.mouse = null | |
| state.mousePress = off | |
| @on 'mousemove', (event) -> | |
| if state.mousePress | |
| dy = event.screenY - state.mouse | |
| y = state.target + smoothsnap.distance dy | |
| window.scrollTo window.scrollX, smoothsnap.point y | |
| return this | |
| ) jQuery |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment