blockScroll.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. (function( $ ) {
  2. $.fn.blockScroll = function(options) {
  3. var settings = $.extend({
  4. // These are the defaults.
  5. startDiv : 1,
  6. fadeDuration : "slow",
  7. paddingRatio : 0.05,
  8. triggerRatio : 0.005,
  9. scrollDuration: "fast",
  10. fadeBlocks: true
  11. }, options );
  12. if(settings.triggerRatio > settings.paddingRatio*.95) { settings.triggerRatio = settings.paddingRatio*.95 }
  13. var theDivs = this.children().filter("div");
  14. var activeDiv = settings.startDiv-1; //Active did is 0-index, settings is 1-index
  15. var windowHeight;
  16. var paddingHeight;
  17. var triggerHeight;
  18. var currentDownTrigger;
  19. var currentUpTrigger;
  20. var totalDivs = theDivs.length;
  21. var lastScrollPos;
  22. var activelyScrolling = false;
  23. var activeBackground= 0;
  24. // Ensure that all of the elements are hidden just in case the css is not setup properly
  25. if(settings.fadeBlocks)
  26. {
  27. this.children().each(function() {
  28. $(this).css('opacity','0');
  29. });
  30. }
  31. arrange();
  32. // Fade in the first div
  33. $(theDivs[activeDiv]).animate({opacity: 1},settings.fadeDuration,'linear', function() {
  34. $(window).scrollTop(0);
  35. calcTriggers();
  36. bindEvents();
  37. lastScrollPos = $(window).scrollTop();
  38. });
  39. function bindEvents()
  40. {
  41. $(window).on('scroll', function(e) {
  42. var scrollPosition = $(window).scrollTop();
  43. var scrollDistance = $(window).height();
  44. var indexOfClosest = 0;
  45. theDivs.each(function(index, element) {
  46. var $this = $(this);
  47. var topPosition = $this.offset().top;
  48. var newScrollDistance = Math.abs(scrollPosition - topPosition);
  49. if(newScrollDistance < scrollDistance)
  50. {
  51. indexOfClosest = index;
  52. scrollDistance = newScrollDistance;
  53. }
  54. });
  55. gotoDiv(indexOfClosest);
  56. }, 250);
  57. $(window).resize(function() {
  58. arrange();
  59. });
  60. $("#block-up-arrow").click(function() {
  61. goUp();
  62. });
  63. $("#block-down-arrow").click(function() {
  64. goDown();
  65. });
  66. $(document).keydown(function(e){
  67. if (e.keyCode == 37 || e.keyCode == 38) {
  68. goUp();
  69. return false;
  70. }
  71. if (e.keyCode == 39 || e.keyCode == 40) {
  72. goDown();
  73. return false;
  74. }
  75. });
  76. $(window).bind('mousewheel', function(e){
  77. if(e.originalEvent.wheelDelta > 119) {
  78. goUp();
  79. }
  80. else if (e.originalEvent.wheelDelta < -119) {
  81. goDown();
  82. }
  83. });
  84. $(window).bind('DOMMouseScroll', function(e){
  85. if(e.originalEvent.detail < 0) {
  86. goUp();
  87. }
  88. else if (e.originalEvent.detail > 0) {
  89. goDown();
  90. }
  91. });
  92. }
  93. function goUp()
  94. {
  95. if(activeDiv > 0 && !activelyScrolling)
  96. {
  97. gotoDiv(activeDiv-1);
  98. }
  99. }
  100. function goDown()
  101. {
  102. if(activeDiv < totalDivs - 1 && !activelyScrolling)
  103. {
  104. gotoDiv(activeDiv+1);
  105. }
  106. }
  107. function gotoDiv(number)
  108. {
  109. if(number == 0)
  110. $("#block-up-arrow").hide();
  111. else
  112. $("#block-up-arrow").show();
  113. if(number == totalDivs-1)
  114. $("#block-down-arrow").hide();
  115. else
  116. $("#block-down-arrow").show();
  117. activeDiv = number;
  118. activelyScrolling = true;
  119. $('html, body').animate({scrollTop: $(theDivs[activeDiv]).offset().top}, settings.scrollDuration, 'linear', function() {
  120. $(theDivs[activeDiv]).animate({opacity: 1}, settings.fadeDuration,'linear', function() {
  121. setTimeout(function(){
  122. activelyScrolling = false; lastScrollPos = $(window).scrollTop();
  123. },50);
  124. });
  125. });
  126. calcTriggers();
  127. }
  128. function calcTriggers()
  129. {
  130. if (activeDiv < totalDivs -1)
  131. {
  132. currentDownTrigger = $(theDivs[activeDiv+1]).offset().top;
  133. } else {
  134. currentDownTrigger = -1;
  135. }
  136. if (activeDiv > 0) {
  137. currentUpTrigger = $(theDivs[activeDiv-1]).offset().top;
  138. } else {
  139. currentUpTrigger = -1;
  140. }
  141. }
  142. function calcDims()
  143. {
  144. windowHeight = $(window).height();
  145. paddingHeight = windowHeight * settings.paddingRatio;
  146. triggerHeight = windowHeight * settings.triggerRatio;
  147. }
  148. function arrange()
  149. {
  150. calcDims();
  151. theDivs.each(function(index, element) {
  152. var $this = $(this);
  153. $this.height('auto');
  154. if($this.height() < windowHeight)
  155. {
  156. var margin = windowHeight/2 - $this.height()/2;
  157. $this.height(windowHeight-margin);
  158. $this.css('padding-top', margin + "px");
  159. var $innerDiv = $($this.children().filter('div')[0]);
  160. // $innerDiv.css('padding-top', margin + "px");
  161. }
  162. if(index != totalDivs - 1)
  163. {
  164. //$this.css('padding-bottom',paddingHeight + 'px');
  165. }
  166. });
  167. gotoDiv(activeDiv);
  168. }
  169. var gotoView = function(number)
  170. {
  171. gotoDiv(number-1);
  172. }
  173. return {
  174. goto: gotoView
  175. };
  176. }
  177. }( jQuery ));
  178. ;(function ($) {
  179. var on = $.fn.on, timer;
  180. $.fn.on = function () {
  181. var args = Array.apply(null, arguments);
  182. var last = args[args.length - 1];
  183. if (isNaN(last) || (last === 1 && args.pop())) return on.apply(this, args);
  184. var delay = args.pop();
  185. var fn = args.pop();
  186. args.push(function () {
  187. var self = this, params = arguments;
  188. clearTimeout(timer);
  189. timer = setTimeout(function () {
  190. fn.apply(self, params);
  191. }, delay);
  192. });
  193. return on.apply(this, args);
  194. };
  195. }(this.jQuery || this.Zepto));