|
4 | 4 | * github.com/psteiger/animate-on-scroll |
5 | 5 | ### |
6 | 6 |
|
7 | | -(($, window, document) -> |
| 7 | +animateOnScroll = ($) -> |
| 8 | + "use strict" |
8 | 9 |
|
9 | | - AnimateOnScroll = (el, options) -> |
10 | | - @el = el |
| 10 | + pluginName = "animateOnScroll" |
| 11 | + _defaults = |
| 12 | + container: "window" |
| 13 | + event: "scroll" |
| 14 | + fade: true |
| 15 | + scale3d: false |
| 16 | + offset_top: 0 |
| 17 | + offset_bottom: 0 |
11 | 18 |
|
12 | | - @options = $.extend({}, defaults, options) |
| 19 | + class AnimateOnScroll |
13 | 20 |
|
14 | | - @_defaults = defaults |
| 21 | + constructor: (@el, options) -> |
| 22 | + @options = $.extend({}, _defaults, options) |
| 23 | + |
| 24 | + @_defaults = _defaults |
15 | 25 | @_name = pluginName |
16 | 26 | @init() |
17 | 27 |
|
18 | | - instances = [] |
19 | | - pluginName = "animateOnScroll" |
20 | | - defaults = |
21 | | - container: "window" |
22 | | - event: "scroll" |
23 | | - fade: true |
24 | | - scale3d: false |
25 | | - offset_top: 0 |
26 | | - offset_bottom: 0 |
27 | | - |
28 | | - AnimateOnScroll:: = |
29 | | - init: -> |
30 | | - self = @ |
31 | | - container = @options.container |
32 | | - event = @options.event |
33 | | - |
34 | | - if container == "window" && event == "scroll" |
35 | | - $(window).scroll -> self.update() |
36 | | - else |
37 | | - $(container).on event, -> self.update() |
38 | | - |
39 | | - return self |
40 | | - |
41 | | - update: -> |
42 | | - if @elIsOnTop() |
43 | | - @animateOut() |
44 | | - else if @elIsOnBottom() |
45 | | - @animateIn() |
46 | | - |
47 | | - get_transform_value: (y_1, y_0) -> |
48 | | - a = 1 / (y_1 - y_0) |
49 | | - x = @get_el_center() |
50 | | - b = -y_0 / (y_1 - y_0) |
51 | | - result = a * x + b |
52 | | - |
53 | | - Math.min (Math.max result, 0), 1 |
54 | | - |
55 | | - get_el_center: -> |
56 | | - bounds = $(@el).get(0).getBoundingClientRect() |
57 | | - |
58 | | - return (bounds.top + bounds.bottom) / 2 |
59 | | - |
60 | | - animate: (y_1, y_0) -> |
61 | | - el = @el |
62 | | - v = @get_transform_value(y_1, y_0) |
63 | | - |
64 | | - #if offset_top > $(window).height() / 2 || offset_bottom < $(window).height() / 2 |
65 | | - # return |
66 | | - |
67 | | - if @options.fade |
68 | | - $(el).css 'opacity', v |
69 | | - |
70 | | - if @options.scale3d |
71 | | - $(el).css 'transform', 'scale3d(' + v + ',' + v + ',' + v + ')' |
72 | | - |
73 | | - animateIn: -> |
74 | | - el_center = $(@el).innerHeight() / 2 |
75 | | - y_1 = $(window).height() - el_center - @options.offset_bottom |
76 | | - y_0 = $(window).height() + el_center - @options.offset_bottom |
77 | | - @animate y_1, y_0 |
78 | | - |
79 | | - animateOut: -> |
80 | | - el_center = $(@el).innerHeight() / 2 |
81 | | - y_1 = @options.offset_top + el_center |
82 | | - y_0 = @options.offset_top - el_center |
83 | | - @animate y_1, y_0 |
84 | | - |
85 | | - elIsOnTop: -> |
86 | | - rect = $(@el).get(0).getBoundingClientRect() |
87 | | - rect.bottom >= 0 and rect.bottom < $(window).height() / 2 |
88 | | - |
89 | | - elIsOnBottom: -> |
90 | | - rect = $(@el).get(0).getBoundingClientRect() |
91 | | - rect.top > $(window).height() / 2 and rect.top <= $(window).height() |
92 | | - |
93 | | - # Plugin wrapper preventing against multiple instantiations. |
94 | | - $.fn[pluginName] = (options) -> |
95 | | - @each -> |
96 | | - instance = $.data @, "plugin_" + pluginName |
97 | | - |
98 | | - if instance |
99 | | - $.extend true, instance.options, options |
100 | | - else |
101 | | - $.data @, "plugin_" + pluginName, new AnimateOnScroll(@, options) |
102 | | - |
103 | | - # Prevents CoffeeScript to return a value from plugin wrapper. |
104 | | - return |
105 | | -) jQuery, window, document |
106 | | - |
| 28 | + init: -> |
| 29 | + self = @ |
| 30 | + container = @options.container |
| 31 | + event = @options.event |
| 32 | + |
| 33 | + if container == "window" && event == "scroll" |
| 34 | + $(window).scroll -> self.update() |
| 35 | + else |
| 36 | + $(container).on event, -> self.update() |
| 37 | + |
| 38 | + return self |
| 39 | + |
| 40 | + update: -> |
| 41 | + if @elIsOnTop() |
| 42 | + @animateOut() |
| 43 | + else if @elIsOnBottom() |
| 44 | + @animateIn() |
| 45 | + |
| 46 | + get_transform_value: (y_1, y_0) -> |
| 47 | + a = 1 / (y_1 - y_0) |
| 48 | + x = @get_el_center() |
| 49 | + b = -y_0 / (y_1 - y_0) |
| 50 | + result = a * x + b |
| 51 | + |
| 52 | + Math.min (Math.max result, 0), 1 |
| 53 | + |
| 54 | + get_el_center: -> |
| 55 | + bounds = $(@el).get(0).getBoundingClientRect() |
| 56 | + |
| 57 | + return (bounds.top + bounds.bottom) / 2 |
| 58 | + |
| 59 | + animate: (y_1, y_0) -> |
| 60 | + el = @el |
| 61 | + v = @get_transform_value(y_1, y_0) |
| 62 | + |
| 63 | + #if offset_top > $(window).height() / 2 || offset_bottom < $(window).height() / 2 |
| 64 | + # return |
| 65 | + |
| 66 | + if @options.fade |
| 67 | + $(el).css 'opacity', v |
| 68 | + |
| 69 | + if @options.scale3d |
| 70 | + $(el).css 'transform', 'scale3d(' + v + ',' + v + ',' + v + ')' |
| 71 | + |
| 72 | + animateIn: -> |
| 73 | + el_center = $(@el).innerHeight() / 2 |
| 74 | + y_1 = $(window).height() - el_center - @options.offset_bottom |
| 75 | + y_0 = $(window).height() + el_center - @options.offset_bottom |
| 76 | + @animate y_1, y_0 |
| 77 | + |
| 78 | + animateOut: -> |
| 79 | + el_center = $(@el).innerHeight() / 2 |
| 80 | + y_1 = @options.offset_top + el_center |
| 81 | + y_0 = @options.offset_top - el_center |
| 82 | + @animate y_1, y_0 |
| 83 | + |
| 84 | + elIsOnTop: -> |
| 85 | + rect = $(@el).get(0).getBoundingClientRect() |
| 86 | + rect.bottom >= 0 and rect.bottom < $(window).height() / 2 |
| 87 | + |
| 88 | + elIsOnBottom: -> |
| 89 | + rect = $(@el).get(0).getBoundingClientRect() |
| 90 | + rect.top > $(window).height() / 2 and rect.top <= $(window).height() |
| 91 | + |
| 92 | + # Plugin wrapper preventing against multiple instantiations. |
| 93 | + $.fn.animateOnScroll = (options) -> |
| 94 | + @each -> |
| 95 | + instance = $.data @, "plugin_" + pluginName |
| 96 | + if instance |
| 97 | + $.extend true, instance.options, options |
| 98 | + else |
| 99 | + $.data @, "plugin_" + pluginName, new AnimateOnScroll @, options |
| 100 | + |
| 101 | + # Prevents CoffeeScript to return a value from plugin wrapper. |
| 102 | + return |
| 103 | + |
| 104 | +# A factory that uses AMD, CommonJS or window globals to |
| 105 | +# create the jQuery plugin. |
| 106 | +do (plugin = animateOnScroll, window) -> |
| 107 | + hasDefine = typeof define is "function" and define.amd? |
| 108 | + hasExports = typeof module isnt "undefined" and module.exports? |
| 109 | + |
| 110 | + # AMD. |
| 111 | + if hasDefine |
| 112 | + define ["jquery"], plugin |
| 113 | + |
| 114 | + # NodeJS/CommonJS. |
| 115 | + else if hasExports |
| 116 | + module.exports = plugin require "jquery" |
| 117 | + |
| 118 | + # Window globals. |
| 119 | + else |
| 120 | + plugin window.jQuery or window.$ |
| 121 | + |
| 122 | +$('.fade-on-scroll').animateOnScroll { |
| 123 | + offset_top: 60 |
| 124 | + fade: true |
| 125 | +} |
| 126 | + |
| 127 | +$('.scale3d-on-scroll').animateOnScroll({ |
| 128 | + scale3d:true |
| 129 | +}) |
0 commit comments