scrollIt.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * ScrollIt
  3. * ScrollIt.js(scroll•it•dot•js) makes it easy to make long, vertically scrolling pages.
  4. *
  5. * Latest version: https://github.com/cmpolis/scrollIt.js
  6. *
  7. * License <https://github.com/cmpolis/scrollIt.js/blob/master/LICENSE.txt>
  8. */
  9. (function($) {
  10. 'use strict';
  11. var pluginName = 'ScrollIt',
  12. pluginVersion = '1.0.3';
  13. /*
  14. * OPTIONS
  15. */
  16. var defaults = {
  17. upKey: 38,
  18. downKey: 40,
  19. easing: 'linear',
  20. scrollTime: 600,
  21. activeClass: 'active',
  22. onPageChange: null,
  23. topOffset : 0
  24. };
  25. $.scrollIt = function(options) {
  26. /*
  27. * DECLARATIONS
  28. */
  29. var settings = $.extend(defaults, options),
  30. active = 0,
  31. lastIndex = $('[data-scroll-index]:last').attr('data-scroll-index');
  32. /*
  33. * METHODS
  34. */
  35. /**
  36. * navigate
  37. *
  38. * sets up navigation animation
  39. */
  40. var navigate = function(ndx) {
  41. if(ndx < 0 || ndx > lastIndex) return;
  42. var targetTop = $('[data-scroll-index=' + ndx + ']').offset().top + settings.topOffset + 1;
  43. $('html,body').animate({
  44. scrollTop: targetTop,
  45. easing: settings.easing
  46. }, settings.scrollTime);
  47. };
  48. /**
  49. * doScroll
  50. *
  51. * runs navigation() when criteria are met
  52. */
  53. var doScroll = function (e) {
  54. var target = $(e.target).closest("[data-scroll-nav]").attr('data-scroll-nav') ||
  55. $(e.target).closest("[data-scroll-goto]").attr('data-scroll-goto');
  56. navigate(parseInt(target));
  57. };
  58. /**
  59. * keyNavigation
  60. *
  61. * sets up keyboard navigation behavior
  62. */
  63. var keyNavigation = function (e) {
  64. var key = e.which;
  65. if($('html,body').is(':animated') && (key == settings.upKey || key == settings.downKey)) {
  66. return false;
  67. }
  68. if(key == settings.upKey && active > 0) {
  69. navigate(parseInt(active) - 1);
  70. return false;
  71. } else if(key == settings.downKey && active < lastIndex) {
  72. navigate(parseInt(active) + 1);
  73. return false;
  74. }
  75. return true;
  76. };
  77. /**
  78. * updateActive
  79. *
  80. * sets the currently active item
  81. */
  82. var updateActive = function(ndx) {
  83. if(settings.onPageChange && ndx && (active != ndx)) settings.onPageChange(ndx);
  84. active = ndx;
  85. $('[data-scroll-nav]').removeClass(settings.activeClass);
  86. $('[data-scroll-nav=' + ndx + ']').addClass(settings.activeClass);
  87. };
  88. /**
  89. * watchActive
  90. *
  91. * watches currently active item and updates accordingly
  92. */
  93. var watchActive = function() {
  94. var winTop = $(window).scrollTop();
  95. var visible = $('[data-scroll-index]').filter(function(ndx, div) {
  96. return winTop >= $(div).offset().top + settings.topOffset &&
  97. winTop < $(div).offset().top + (settings.topOffset) + $(div).outerHeight()
  98. });
  99. var newActive = visible.first().attr('data-scroll-index');
  100. updateActive(newActive);
  101. };
  102. /*
  103. * runs methods
  104. */
  105. $(window).on('scroll',watchActive).scroll();
  106. $(window).on('keydown', keyNavigation);
  107. $('body').on('click','[data-scroll-nav], [data-scroll-goto]', function(e){
  108. e.preventDefault();
  109. doScroll(e);
  110. });
  111. };
  112. }(jQuery));