jquery.scrollfollow.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * jquery.scrollFollow.js
  3. * Copyright (c) 2008 Net Perspective (http://kitchen.net-perspective.com/)
  4. * Licensed under the MIT License (http://www.opensource.org/licenses/mit-license.php)
  5. *
  6. * @author R.A. Ray
  7. *
  8. * @projectDescription jQuery plugin for allowing an element to animate down as the user scrolls the page.
  9. *
  10. * @version 0.4.0
  11. *
  12. * @requires jquery.js (tested with 1.2.6)
  13. * @requires ui.core.js (tested with 1.5.2)
  14. *
  15. * @optional jquery.cookie.js (http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/)
  16. * @optional jquery.easing.js (http://gsgd.co.uk/sandbox/jquery/easing/ - tested with 1.3)
  17. *
  18. * @param speed int - Duration of animation (in milliseconds)
  19. * default: 500
  20. * @param offset int - Number of pixels box should remain from top of viewport
  21. * default: 0
  22. * @param easing string - Any one of the easing options from the easing plugin - Requires jQuery Easing Plugin < http://gsgd.co.uk/sandbox/jquery/easing/ >
  23. * default: 'linear'
  24. * @param container string - ID of the containing div
  25. * default: box's immediate parent
  26. * @param killSwitch string - ID of the On/Off toggle element
  27. * default: 'killSwitch'
  28. * @param onText string - killSwitch text to be displayed if sliding is enabled
  29. * default: 'Turn Slide Off'
  30. * @param offText string - killSwitch text to be displayed if sliding is disabled
  31. * default: 'Turn Slide On'
  32. * @param relativeTo string - Scroll animation can be relative to either the 'top' or 'bottom' of the viewport
  33. * default: 'top'
  34. * @param delay int - Time between the end of the scroll and the beginning of the animation in milliseconds
  35. * default: 0
  36. */
  37. ( function( $ ) {
  38. $.scrollFollow = function ( box, options )
  39. {
  40. // Convert box into a jQuery object
  41. box = $( box );
  42. // 'box' is the object to be animated
  43. var position = box.css( 'position' );
  44. box.css('position','relative');
  45. box.css('z-index','10');
  46. box.css('background-color','#CCEEFF');
  47. function ani()
  48. {
  49. // The script runs on every scroll which really means many times during a scroll.
  50. // We don't want multiple slides to queue up.
  51. box.queue( [ ] );
  52. // A bunch of values we need to determine where to animate to
  53. var viewportHeight = parseInt( $( window ).height() );
  54. var pageScroll = parseInt( $( document ).scrollTop() );
  55. var parentTop = parseInt( box.offset().top );
  56. var parentHeight = parseInt( box.attr( 'offsetHeight' ) );
  57. var boxHeight = parseInt( box.attr( 'offsetHeight' ) + ( parseInt( box.css( 'marginTop' ) ) || 0 ) + ( parseInt( box.css( 'marginBottom' ) ) || 0 ) );
  58. var aniTop;
  59. // Make sure the user wants the animation to happen
  60. if ( isActive )
  61. {
  62. // If the box should animate relative to the top of the window
  63. if ( options.relativeTo == 'top' )
  64. {
  65. // Don't animate until the top of the window is close enough to the top of the box
  66. if ( box.initialOffsetTop >= ( pageScroll + options.offset ) )
  67. {
  68. aniTop = box.initialTop;
  69. }
  70. else
  71. {
  72. aniTop = Math.min( ( Math.max( ( -parentTop ), ( pageScroll - box.initialOffsetTop + box.initialTop ) ) + options.offset ), ( parentHeight - boxHeight - box.paddingAdjustment ) );
  73. }
  74. }
  75. // If the box should animate relative to the bottom of the window
  76. else if ( options.relativeTo == 'bottom' )
  77. {
  78. // Don't animate until the bottom of the window is close enough to the bottom of the box
  79. if ( ( box.initialOffsetTop + boxHeight ) >= ( pageScroll + options.offset + viewportHeight ) )
  80. {
  81. aniTop = box.initialTop;
  82. }
  83. else
  84. {
  85. aniTop = Math.min( ( pageScroll + viewportHeight - boxHeight - options.offset ), ( parentHeight - boxHeight ) );
  86. }
  87. }
  88. // Checks to see if the relevant scroll was the last one
  89. // "-20" is to account for inaccuracy in the timeout
  90. if ( ( new Date().getTime() - box.lastScroll ) >= ( options.delay - 20 ) )
  91. {
  92. box.animate(
  93. {
  94. top: pageScroll
  95. }, options.speed, options.easing
  96. );
  97. }
  98. }
  99. };
  100. // For user-initiated stopping of the slide
  101. var isActive = true;
  102. if ( $.cookie != undefined )
  103. {
  104. if( $.cookie( 'scrollFollowSetting' + box.attr( 'id' ) ) == 'false' )
  105. {
  106. var isActive = false;
  107. $( '#' + options.killSwitch ).text( options.offText )
  108. .toggle(
  109. function ()
  110. {
  111. isActive = true;
  112. $( this ).text( options.onText );
  113. $.cookie( 'scrollFollowSetting' + box.attr( 'id' ), true, { expires: 365, path: '/'} );
  114. ani();
  115. },
  116. function ()
  117. {
  118. isActive = false;
  119. $( this ).text( options.offText );
  120. box.animate(
  121. {
  122. top: box.initialTop
  123. }, options.speed, options.easing
  124. );
  125. $.cookie( 'scrollFollowSetting' + box.attr( 'id' ), false, { expires: 365, path: '/'} );
  126. }
  127. );
  128. }
  129. else
  130. {
  131. $( '#' + options.killSwitch ).text( options.onText )
  132. .toggle(
  133. function ()
  134. {
  135. isActive = false;
  136. $( this ).text( options.offText );
  137. box.animate(
  138. {
  139. top: box.initialTop
  140. }, 0
  141. );
  142. $.cookie( 'scrollFollowSetting' + box.attr( 'id' ), false, { expires: 365, path: '/'} );
  143. },
  144. function ()
  145. {
  146. isActive = true;
  147. $( this ).text( options.onText );
  148. $.cookie( 'scrollFollowSetting' + box.attr( 'id' ), true, { expires: 365, path: '/'} );
  149. ani();
  150. }
  151. );
  152. }
  153. }
  154. // If no parent ID was specified, and the immediate parent does not have an ID
  155. // options.container will be undefined. So we need to figure out the parent element.
  156. if ( options.container == '')
  157. {
  158. box.cont = box.parent();
  159. }
  160. else
  161. {
  162. box.cont = $( '#' + options.container );
  163. }
  164. // Finds the default positioning of the box.
  165. box.initialOffsetTop = parseInt( box.offset().top );
  166. box.initialTop = parseInt( box.css( 'top' ) ) || 0;
  167. // Hack to fix different treatment of boxes positioned 'absolute' and 'relative'
  168. if ( box.css( 'position' ) == 'relative' )
  169. {
  170. box.paddingAdjustment = parseInt( box.cont.css( 'paddingTop' ) ) + parseInt( box.cont.css( 'paddingBottom' ) );
  171. }
  172. else
  173. {
  174. box.paddingAdjustment = 0;
  175. }
  176. // Animate the box when the page is scrolled
  177. $( window ).scroll( function ()
  178. {
  179. // Sets up the delay of the animation
  180. $.fn.scrollFollow.interval = setTimeout( function(){ ani();} , options.delay );
  181. // To check against right before setting the animation
  182. box.lastScroll = new Date().getTime();
  183. }
  184. );
  185. // Animate the box when the page is resized
  186. $( window ).resize( function ()
  187. {
  188. // Sets up the delay of the animation
  189. $.fn.scrollFollow.interval = setTimeout( function(){ ani();} , options.delay );
  190. // To check against right before setting the animation
  191. box.lastScroll = new Date().getTime();
  192. }
  193. );
  194. // Run an initial animation on page load
  195. box.lastScroll = 0;
  196. ani();
  197. };
  198. $.fn.scrollFollow = function ( options )
  199. {
  200. options = options || {};
  201. options.relativeTo = options.relativeTo || 'top';
  202. options.speed = options.speed || 500;
  203. options.offset = options.offset || 0;
  204. options.easing = options.easing || 'swing';
  205. options.container = options.container || this.parent().attr( 'id' );
  206. options.killSwitch = options.killSwitch || 'killSwitch';
  207. options.onText = options.onText || 'Turn Slide Off';
  208. options.offText = options.offText || 'Turn Slide On';
  209. options.delay = options.delay || 0;
  210. this.each( function()
  211. {
  212. new $.scrollFollow( this, options );
  213. }
  214. );
  215. return this;
  216. };
  217. })( jQuery );
  218. $(function(){
  219. // $(".panel-top").each(function(){
  220. // var useSclFlw = $(this).attr("useSclFlw");
  221. // if(typeof useSclFlw=='undefined'||useSclFlw=='true'){
  222. // $(this).scrollFollow();
  223. // }
  224. // })
  225. });