-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.jFloatable.coffee
147 lines (112 loc) · 3.9 KB
/
jquery.jFloatable.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# verify if the element already has the jFloatable plugin actived
$.isJfloatable = (el) ->
$(el).data("jFloatable") != undefined;
$.Jfloatable = (el, options) ->
# To avoid scope issues, use 'base' instead of 'this' to reference this
# class from internal events and functions.
base = this
#acess to jQuery and DOM versions of element.
base.$el = $(el)
base.el = el;
base.$parentRelative = base.$el.parents(options.parentRelativeSelector).first()
#add a reverse reference to the DOM object.
base.$el.data("jFloatable", base)
# properties
target = base.$el
maxTop = null
originalData =
offset: null
style: null
base.isActive = true
getScrollTop = ->
( if (window.pageYOffset != undefined) then window.pageYOffset else document.documentElement.scrollTop )
targetTop = ->
target.offset().top - parseFloat(target.css('marginTop').replace(/auto/, 0)) - base.options.top
setFix = ()->
cssBase =
position: 'fixed'
top: base.options.top
left: originalData.offset.left
css = $.extend({}, cssBase, base.options.fixedCss);
target.css(css)
setAbsolute = (limit)->
cssBase =
position: 'absolute'
top: (limit - base.$parentRelative.offset().top) + base.options.top
css = $.extend({}, cssBase, base.options.absoluteCss);
target.css(css)
getLimit = ->
# if limit was setted
if base.options.limit > 0
base.options.limit - target.outerHeight() - base.options.top
else
$(document).outerHeight() - target.outerHeight() - base.options.top
windowScroll = ->
# if it is not active should stop
return false unless base.isActive
windowScrollTop = getScrollTop()
limit = getLimit();
# if the scroll passed the original top
# maxTop is the targetOriginalTop
if windowScrollTop >= maxTop
if windowScrollTop < limit
if !target.hasClass("jFloatable-fixed")
# add the fixed class and remove the style created by the scrolling
# it detach the target from the screen
target.removeClass("jFloatable-absolute").addClass("jFloatable-fixed").removeAttr("style");
setFix()
else if !target.hasClass("jFloatable-absolute")
# it reach the limit
target.removeClass("jFloatable-fixed").addClass("jFloatable-absolute").removeAttr("style")
setAbsolute(limit)
else
setInitialPosition()
getInitialPosition = ->
originalData.offset = target.offset()
originalData.style = target.attr("style")
setInitialPosition = ->
target.removeClass("jFloatable-fixed").removeClass("jFloatable-absolute").removeAttr("style")
target.attr("style", originalData.style)
originalData.offset = target.offset()
reset = ->
setInitialPosition();
if target.is(":visible") and base.isActive
windowScroll();
turnOn = (e) ->
e.preventDefault()
e.stopPropagation()
base.isActive = true
reset()
turnOff = (e) ->
e.preventDefault()
e.stopPropagation()
base.isActive = false
setInitialPosition()
# initializer
base.init = ->
base.options = $.extend({}, $.Jfloatable.defaultOptions, options);
$window = $(window)
getInitialPosition()
maxTop = targetTop()
# fix or unfix the target when the window scrolls
$window.bind('scroll.jFloatable', windowScroll)
$window.bind('resize.jFloatable', reset)
target.bind('resize.jFloatable', windowScroll)
target.bind('reset.jFloatable', reset)
$window.bind('reset.jFloatable', reset)
target.bind('off.jFloatable', turnOff)
$window.bind('off.jFloatable', turnOff)
target.bind('on.jFloatable', turnOn)
$window.bind('on.jFloatable', turnOn)
windowScroll()
base.init()
# the default options
$.Jfloatable.defaultOptions =
limit: 0
top: 0
parentRelativeSelector: ".jFloatable-relative"
fixedCss: {}
absoluteCss: {}
$.fn.jFloatable = (options) ->
this.each ->
new $.Jfloatable(this, options)