]>
git.ipfire.org Git - ipfire.org.git/blob - static/js/jquery-scrollto.js
7d47976bf607b57025785e9138f4a77ecaa3f70e
8 $ = window
.jQuery
|| require('jquery')
10 // Fix scrolling animations on html/body on safari
11 $.propHooks
.scrollTop
= $.propHooks
.scrollLeft
= {
12 get: function (elem
, prop
) {
14 if ( elem
.tagName
=== 'HTML' || elem
.tagName
=== 'BODY' ) {
15 if ( prop
=== 'scrollLeft' ) {
16 result
= window
.scrollX
18 else if ( prop
=== 'scrollTop' ) {
19 result
= window
.scrollY
22 if ( result
== null ) {
28 $.Tween
.propHooks
.scrollTop
= $.Tween
.propHooks
.scrollLeft
= {
29 get: function (tween
) {
30 return $.propHooks
.scrollTop
.get(tween
.elem
, tween
.prop
)
32 set: function (tween
) {
34 if ( tween
.elem
.tagName
=== 'HTML' || tween
.elem
.tagName
=== 'BODY' ) {
36 tween
.options
.bodyScrollLeft
= (tween
.options
.bodyScrollLeft
|| window
.scrollX
)
37 tween
.options
.bodyScrollTop
= (tween
.options
.bodyScrollTop
|| window
.scrollY
)
40 if ( tween
.prop
=== 'scrollLeft' ) {
41 tween
.options
.bodyScrollLeft
= Math
.round(tween
.now
)
43 else if ( tween
.prop
=== 'scrollTop' ) {
44 tween
.options
.bodyScrollTop
= Math
.round(tween
.now
)
48 window
.scrollTo(tween
.options
.bodyScrollLeft
, tween
.options
.bodyScrollTop
)
51 else if ( tween
.elem
.nodeType
&& tween
.elem
.parentNode
) {
52 tween
.elem
[tween
.prop
] = tween
.now
70 configure: function (options
) {
71 // Apply Options to Config
72 $.extend(ScrollTo
.config
, options
|| {})
78 // Perform the Scroll Animation for the Collections
79 // We use $inline here, so we can determine the actual offset start for each overflow:scroll item
80 // Each collection is for each overflow:scroll item
81 scroll: function (collections
, config
) {
83 var collection
, $container
, $target
, $inline
, position
,
84 containerScrollTop
, containerScrollLeft
,
85 containerScrollTopEnd
, containerScrollLeftEnd
,
86 startOffsetTop
, targetOffsetTop
, targetOffsetTopAdjusted
,
87 startOffsetLeft
, targetOffsetLeft
, targetOffsetLeftAdjusted
,
91 // Determine the Scroll
92 collection
= collections
.pop()
93 $container
= collection
.$container
94 $target
= collection
.$target
96 // Prepare the Inline Element of the Container
97 $inline
= $('<span/>').css({
98 'position': 'absolute',
102 position
= $container
.css('position')
104 // Insert the Inline Element of the Container
105 $container
.css({position
: 'relative'})
106 $inline
.appendTo($container
)
108 // Determine the top offset
109 startOffsetTop
= $inline
.offset().top
110 targetOffsetTop
= $target
.offset().top
111 targetOffsetTopAdjusted
= targetOffsetTop
- startOffsetTop
- parseInt(config
.offsetTop
, 10)
113 // Determine the left offset
114 startOffsetLeft
= $inline
.offset().left
115 targetOffsetLeft
= $target
.offset().left
116 targetOffsetLeftAdjusted
= targetOffsetLeft
- startOffsetLeft
- parseInt(config
.offsetLeft
, 10)
118 // Determine current scroll positions
119 containerScrollTop
= $container
.prop('scrollTop')
120 containerScrollLeft
= $container
.prop('scrollLeft')
122 // Reset the Inline Element of the Container
124 $container
.css({position
: position
})
126 // Prepare the scroll options
129 // Prepare the callback
130 callback = function () {
132 if ( collections
.length
=== 0 ) {
134 if ( typeof config
.callback
=== 'function' ) {
140 ScrollTo
.scroll(collections
, config
)
146 // Handle if we only want to scroll if we are outside the viewport
147 if ( config
.onlyIfOutside
) {
148 // Determine current scroll positions
149 containerScrollTopEnd
= containerScrollTop
+ $container
.height()
150 containerScrollLeftEnd
= containerScrollLeft
+ $container
.width()
152 // Check if we are in the range of the visible area of the container
153 if ( containerScrollTop
< targetOffsetTopAdjusted
&& targetOffsetTopAdjusted
< containerScrollTopEnd
) {
154 targetOffsetTopAdjusted
= containerScrollTop
156 if ( containerScrollLeft
< targetOffsetLeftAdjusted
&& targetOffsetLeftAdjusted
< containerScrollLeftEnd
) {
157 targetOffsetLeftAdjusted
= containerScrollLeft
161 // Determine the scroll options
162 if ( targetOffsetTopAdjusted
!== containerScrollTop
) {
163 scrollOptions
.scrollTop
= targetOffsetTopAdjusted
165 if ( targetOffsetLeftAdjusted
!== containerScrollLeft
) {
166 scrollOptions
.scrollLeft
= targetOffsetLeftAdjusted
169 // Check to see if the scroll is necessary
170 if ( $container
.prop('scrollHeight') === $container
.height() ) {
171 delete scrollOptions
.scrollTop
173 if ( $container
.prop('scrollWidth') === $container
.width() ) {
174 delete scrollOptions
.scrollLeft
177 // Perform the scroll
178 if ( scrollOptions
.scrollTop
!= null || scrollOptions
.scrollLeft
!= null ) {
179 $container
.animate(scrollOptions
, {
180 duration
: config
.duration
,
181 easing
: config
.easing
,
193 // ScrollTo the Element using the Options
194 fn: function (options
) {
196 var collections
, config
, $container
, container
200 var $target
= $(this)
201 if ( $target
.length
=== 0 ) {
207 config
= $.extend({}, ScrollTo
.config
, options
)
210 $container
= $target
.parent()
211 container
= $container
.get(0)
213 // Cycle through the containers
214 while ( ($container
.length
=== 1) && (container
!== document
.body
) && (container
!== document
) ) {
215 // Check Container for scroll differences
216 var containerScrollTop
, containerScrollLeft
217 containerScrollTop
= $container
.css('overflow-y') !== 'visible' && container
.scrollHeight
!== container
.clientHeight
218 containerScrollLeft
= $container
.css('overflow-x') !== 'visible' && container
.scrollWidth
!== container
.clientWidth
219 if ( containerScrollTop
|| containerScrollLeft
) {
220 // Push the Collection
222 '$container': $container
,
228 // Update the Container
229 $container
= $container
.parent()
230 container
= $container
.get(0)
233 // Add the final collection
235 '$container': $('html'),
236 // document.body doesn't work in firefox, html works for all
237 // internet explorer starts at the beggining
242 if ( config
.durationMode
=== 'all' ) {
243 config
.duration
/= collections
.length
247 ScrollTo
.scroll(collections
, config
)
254 // Apply our extensions to jQuery
255 $.ScrollTo
= $.ScrollTo
|| ScrollTo
256 $.fn
.ScrollTo
= $.fn
.ScrollTo
|| ScrollTo
.fn