jquery.smint.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. SMINT V1.0 by Robert McCracken
  3. SMINT V2.0 by robert McCracken with some awesome help from Ryan Clarke (@clarkieryan) and mcpacosy ‏(@mcpacosy)
  4. SMINT V3.0 by robert McCracken with some awesome help from Ryan Clarke (@clarkieryan) and mcpacosy ‏(@mcpacosy)
  5. SMINT is my first dabble into jQuery plugins!
  6. http://www.outyear.co.uk/smint/
  7. If you like Smint, or have suggestions on how it could be improved, send me a tweet @rabmyself
  8. */
  9. (function(){
  10. $.fn.smint = function( options ) {
  11. var settings = $.extend({
  12. 'scrollSpeed' : 500,
  13. 'mySelector' : 'div',
  14. 'position' : 'fixed',
  15. 'smintClass':'smint'
  16. }, options);
  17. // adding a class to users div
  18. $(this).addClass(settings.smintClass);
  19. //Set the variables needed
  20. var optionLocs = new Array(),
  21. lastScrollTop = 0,
  22. menuHeight = $(".smint").height(),
  23. smint = $('.'+settings.smintClass),
  24. smintA = $('.'+settings.smintClass+' a'),
  25. myOffset = smint.height();
  26. if ( settings.scrollSpeed ) {
  27. var scrollSpeed = settings.scrollSpeed
  28. }
  29. if ( settings.mySelector ) {
  30. var mySelector = settings.mySelector
  31. };
  32. if(settings.position){
  33. var position = settings.position;
  34. }
  35. return smintA.each( function(index) {
  36. var id = $(this).attr('href').split('#')[1];
  37. if (!$(this).hasClass("extLink")) {
  38. $(this).attr('id', id);
  39. }
  40. if(id==undefined){
  41. return;
  42. }
  43. //Fill the menu
  44. // console.log(mySelector+"."+id)
  45. optionLocs.push(Array(
  46. $(mySelector+"."+id).position().top-menuHeight,
  47. $(mySelector+"."+id).height()+$(mySelector+"."+id).position().top, id)
  48. );
  49. ///////////////////////////////////
  50. // get initial top offset for the menu
  51. var stickyTop = smint.offset().top;
  52. // check position and make sticky if needed
  53. var stickyMenu = function(direction){
  54. // current distance top
  55. var scrollTop = $(window).scrollTop()+myOffset;
  56. // if we scroll more than the navigation, change its position to fixed and add class 'fxd', otherwise change it back to absolute and remove the class
  57. if (scrollTop > stickyTop+myOffset) {
  58. smint.css({ 'position': position, 'top':0,'left':0 }).addClass('fxd');
  59. // add padding to the body to make up for the loss in heigt when the menu goes to a fixed position.
  60. // When an item is fixed, its removed from the flow so its height doesnt impact the other items on the page
  61. if(smint.is(':visible')){
  62. $('body').css('padding-top', menuHeight );
  63. }
  64. } else {
  65. smint.css( 'position', 'relative').removeClass('fxd');
  66. //remove the padding we added.
  67. $('body').css('padding-top', '0' );
  68. }
  69. // Check if the position is inside then change the menu
  70. // Courtesy of Ryan Clarke (@clarkieryan)
  71. if(optionLocs[index][0] <= scrollTop && scrollTop <= optionLocs[index][1]){
  72. if(direction == "up"){
  73. $("#"+id).addClass("active");
  74. if(optionLocs[index+1]) {
  75. $("#" + optionLocs[index + 1][2]).removeClass("active");
  76. }
  77. } else if(index > 0) {
  78. $("#"+id).addClass("active");
  79. $("#"+optionLocs[index-1][2]).removeClass("active");
  80. } else if(direction == undefined){
  81. $("#"+id).addClass("active");
  82. }
  83. $.each(optionLocs, function(i){
  84. if(id != optionLocs[i][2]){
  85. $("#"+optionLocs[i][2]).removeClass("active");
  86. }
  87. });
  88. }
  89. };
  90. // run functions
  91. stickyMenu();
  92. // run function every time you scroll
  93. $(window).scroll(function() {
  94. //Get the direction of scroll
  95. var st = $(this).scrollTop()+myOffset;
  96. if (st > lastScrollTop) {
  97. direction = "down";
  98. } else if (st < lastScrollTop ){
  99. direction = "up";
  100. }
  101. lastScrollTop = st;
  102. stickyMenu(direction);
  103. // Check if at bottom of page, if so, add class to last <a> as sometimes the last div
  104. // isnt long enough to scroll to the top of the page and trigger the active state.
  105. if($(window).scrollTop() + $(window).height() == $(document).height()) {
  106. smintA.removeClass('active')
  107. $(".smint a:not('.extLink'):last").addClass('active')
  108. } else {
  109. smintA.last().removeClass('active')
  110. }
  111. });
  112. ///////////////////////////////////////
  113. $(this).on('click', function(e){
  114. // gets the height of the users div. This is used for off-setting the scroll so the menu doesnt overlap any content in the div they jst scrolled to
  115. var myOffset = smint.height();
  116. // stops hrefs making the page jump when clicked
  117. e.preventDefault();
  118. // get the hash of the button you just clicked
  119. var hash = $(this).attr('href').split('#')[1];
  120. var goTo = $(mySelector+'.'+ hash).offset().top-myOffset;
  121. // Scroll the page to the desired position!
  122. $("html, body").stop().animate({ scrollTop: goTo }, scrollSpeed);
  123. // if the link has the '.extLink' class it will be ignored
  124. // Courtesy of mcpacosy ‏(@mcpacosy)
  125. if ($(this).hasClass("extLink"))
  126. {
  127. return false;
  128. }
  129. });
  130. //This lets yo use links in body text to scroll. Just add the class 'intLink' to your button and it will scroll
  131. $('.intLink').on('click', function(e){
  132. var myOffset = smint.height();
  133. e.preventDefault();
  134. var hash = $(this).attr('href').split('#')[1];
  135. if (smint.hasClass('fxd')) {
  136. var goTo = $(mySelector+'.'+ hash).position().top-myOffset;
  137. } else {
  138. var goTo = $(mySelector+'.'+ hash).position().top-myOffset*2;
  139. }
  140. $("html, body").stop().animate({ scrollTop: goTo }, scrollSpeed);
  141. if ($(this).hasClass("extLink"))
  142. {
  143. return false;
  144. }
  145. });
  146. });
  147. };
  148. $.fn.smint.defaults = { 'scrollSpeed': 500, 'mySelector': 'div','position':'fixed'};
  149. })(jQuery);