Posted By

miha on 07/28/14


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Beppoi


Bootstrap ScrollSpy alternative with animation


 / Published in: JavaScript
 

URL: https://jsfiddle.net/vvvvvv/3hfcx3yx/

Bootstrap ScrollSpy alternative with animation

  1. // EXAMPLE:
  2. // https://jsfiddle.net/vvvvvv/3hfcx3yx/
  3.  
  4. // https://raw.githubusercontent.com/jquery/jquery-mousewheel/4.0.x/jquery.mousewheel.min.js
  5. /*! Copyright (c) 2013 Brandon Aaron (http://brandon.aaron.sh)
  6.  * Licensed under the MIT License (LICENSE.txt).
  7.  *
  8.  * Version: 3.1.12
  9.  *
  10.  * Requires: jQuery 1.2.2+
  11.  */
  12. !function(a){"function"==typeof define&&define.amd?define(["jquery"],a):"object"==typeof exports?module.exports=a:a(jQuery)}(function(a){function b(b){var g=b||window.event,h=i.call(arguments,1),j=0,l=0,m=0,n=0,o=0,p=0;if(b=a.event.fix(g),b.type="mousewheel","detail"in g&&(m=-1*g.detail),"wheelDelta"in g&&(m=g.wheelDelta),"wheelDeltaY"in g&&(m=g.wheelDeltaY),"wheelDeltaX"in g&&(l=-1*g.wheelDeltaX),"axis"in g&&g.axis===g.HORIZONTAL_AXIS&&(l=-1*m,m=0),j=0===m?l:m,"deltaY"in g&&(m=-1*g.deltaY,j=m),"deltaX"in g&&(l=g.deltaX,0===m&&(j=-1*l)),0!==m||0!==l){if(1===g.deltaMode){var q=a.data(this,"mousewheel-line-height");j*=q,m*=q,l*=q}else if(2===g.deltaMode){var r=a.data(this,"mousewheel-page-height");j*=r,m*=r,l*=r}if(n=Math.max(Math.abs(m),Math.abs(l)),(!f||f>n)&&(f=n,d(g,n)&&(f/=40)),d(g,n)&&(j/=40,l/=40,m/=40),j=Math[j>=1?"floor":"ceil"](j/f),l=Math[l>=1?"floor":"ceil"](l/f),m=Math[m>=1?"floor":"ceil"](m/f),k.settings.normalizeOffset&&this.getBoundingClientRect){var s=this.getBoundingClientRect();o=b.clientX-s.left,p=b.clientY-s.top}return b.deltaX=l,b.deltaY=m,b.deltaFactor=f,b.offsetX=o,b.offsetY=p,b.deltaMode=0,h.unshift(b,j,l,m),e&&clearTimeout(e),e=setTimeout(c,200),(a.event.dispatch||a.event.handle).apply(this,h)}}function c(){f=null}function d(a,b){return k.settings.adjustOldDeltas&&"mousewheel"===a.type&&b%120===0}var e,f,g=["wheel","mousewheel","DOMMouseScroll","MozMousePixelScroll"],h="onwheel"in document||document.documentMode>=9?["wheel"]:["mousewheel","DomMouseScroll","MozMousePixelScroll"],i=Array.prototype.slice;if(a.event.fixHooks)for(var j=g.length;j;)a.event.fixHooks[g[--j]]=a.event.mouseHooks;var k=a.event.special.mousewheel={version:"3.1.12",setup:function(){if(this.addEventListener)for(var c=h.length;c;)this.addEventListener(h[--c],b,!1);else this.onmousewheel=b;a.data(this,"mousewheel-line-height",k.getLineHeight(this)),a.data(this,"mousewheel-page-height",k.getPageHeight(this))},teardown:function(){if(this.removeEventListener)for(var c=h.length;c;)this.removeEventListener(h[--c],b,!1);else this.onmousewheel=null;a.removeData(this,"mousewheel-line-height"),a.removeData(this,"mousewheel-page-height")},getLineHeight:function(b){var c=a(b),d=c["offsetParent"in a.fn?"offsetParent":"parent"]();return d.length||(d=a("body")),parseInt(d.css("fontSize"),10)||parseInt(c.css("fontSize"),10)||16},getPageHeight:function(b){return a(b).height()},settings:{adjustOldDeltas:!0,normalizeOffset:!0}};a.fn.extend({mousewheel:function(a){return a?this.bind("mousewheel",a):this.trigger("mousewheel")},unmousewheel:function(a){return this.unbind("mousewheel",a)}})});
  13.  
  14. // http://stackoverflow.com/a/14805098/1677077
  15. // http://stackoverflow.com/a/9028213/1677077
  16. $(function(){
  17. var $window = $(window);
  18. var timeStamp = new Date().getTime();
  19. var $header = $('#header');
  20. var $header_li = $header.find("ul li a[href^='#']");
  21. var header_height = ($header.css('position') == 'fixed') ? Math.round($header.outerHeight(true)) : 0;
  22. $header_li.on('click',function(e){
  23. e.preventDefault();
  24. var hash = this.hash;
  25. var $this = $(this);
  26. $('html, body').finish().animate({
  27. //scrollTop: Math.round($(this.hash).offset().top)-header_height
  28. scrollTop: Math.round($(this.hash).offset().top)
  29. },450,function(){
  30. $header_li.removeClass('active');
  31. $this.addClass('active');
  32. //window.location.hash = hash;
  33. });
  34. });
  35. $window.on('scroll',function(){
  36. $this = $(this);
  37. for(var i = 0; i < $header_li.length; i++) {
  38. //if(Math.round($this.scrollTop()) < (Math.round($($($header_li[i]).attr('href')).offset().top)-header_height)) {
  39. if(Math.round($this.scrollTop()) < Math.round($($($header_li[i]).attr('href')).offset().top)) {
  40. $($header_li[i]).removeClass('active');
  41. }
  42. else {
  43. $($header_li[i]).addClass('active');
  44. if(typeof $header_li[i+1] != 'undefined') {
  45. //if(Math.round($this.scrollTop()) >= (Math.round($($($header_li[i+1]).attr('href')).offset().top)-header_height)) {
  46. if(Math.round($this.scrollTop()) >= Math.round($($($header_li[i+1]).attr('href')).offset().top)) {
  47. $($header_li[i]).removeClass('active');
  48. }
  49. }
  50. }
  51. }
  52. });
  53. var mousewheel_sections = [];
  54. for(var i = 0; i < $header_li.length; i++) {
  55. var header_li_href = $($header_li[i]).attr('href');
  56. mousewheel_sections[header_li_href.substr(1)] = i;
  57. $(header_li_href).on('mousewheel',function(e){
  58. $this = $(this);
  59. if(Math.round($window.height()) >= Math.round($this.outerHeight(true)) || (Math.round($(window).scrollTop())-Math.round($this.offset().top)) < header_height) {
  60. var timeNow = new Date().getTime();
  61. var mousewheel_attr = '';
  62. if(typeof e.deltaY != 'undefined' && (timeNow - timeStamp >= 100)) {
  63. timeStamp = timeNow;
  64. if(parseInt(e.deltaY) > 0) {
  65. if(typeof $header_li[mousewheel_sections[$this.attr('id')]-1] != 'undefined' && Math.round($window.height()) >= Math.round($this.outerHeight(true))) {
  66. mousewheel_attr = $($header_li[mousewheel_sections[$this.attr('id')]-1]).attr('href');
  67. }
  68. }
  69. else {
  70. if(parseInt(e.deltaY) < 0) {
  71. if(typeof $header_li[mousewheel_sections[$this.attr('id')]+1] != 'undefined' && Math.round($window.height()) >= Math.round($this.outerHeight(true))) {
  72. mousewheel_attr = $($header_li[mousewheel_sections[$this.attr('id')]+1]).attr('href');
  73. }
  74. }
  75. }
  76. if(mousewheel_attr) {
  77. e.preventDefault();
  78. $('html, body').finish().animate({
  79. //scrollTop: Math.round($(mousewheel_attr).offset().top)-header_height
  80. scrollTop: Math.round($(mousewheel_attr).offset().top)
  81. },450);
  82. }
  83. }
  84. else {
  85. timeStamp = timeNow;
  86. return;
  87. }
  88. }
  89. });
  90. }
  91. });

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: miha on August 5, 2014

<ul><li><a href="#idofdiv1">Scroll to div1</a></li></ul>......

You need to login to post a comment.