mediumish.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. jQuery(document).ready(function($){
  2. var offset = 1250;
  3. var duration = 800;
  4. jQuery(window).scroll(function() {
  5. if (jQuery(this).scrollTop() > offset) {
  6. jQuery('.back-to-top').fadeIn(duration);
  7. } else {
  8. jQuery('.back-to-top').fadeOut(duration);
  9. }
  10. });
  11. jQuery('.back-to-top').click(function(event) {
  12. event.preventDefault();
  13. jQuery('html, body').animate({scrollTop: 0}, duration);
  14. return false;
  15. })
  16. // alertbar later
  17. $(document).scroll(function () {
  18. var maxScroll = $(document).height() - $(window).height();
  19. var y = $(this).scrollTop();
  20. if (y > 350 || y + 100 > maxScroll) {
  21. $('.alertbar').fadeIn();
  22. } else {
  23. $('.alertbar').fadeOut();
  24. }
  25. });
  26. // Smooth on external page
  27. $(function() {
  28. setTimeout(function() {
  29. if (location.hash) {
  30. /* we need to scroll to the top of the window first, because the browser will always jump to the anchor first before JavaScript is ready, thanks Stack Overflow: http://stackoverflow.com/a/3659116 */
  31. window.scrollTo(0, 0);
  32. target = location.hash.split('#');
  33. smoothScrollTo($('#'+target[1]));
  34. }
  35. }, 1);
  36. // taken from: https://css-tricks.com/snippets/jquery/smooth-scrolling/
  37. $('a[href*=\\#]:not([href=\\#])').click(function() {
  38. if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
  39. smoothScrollTo($(this.hash));
  40. return false;
  41. }
  42. });
  43. function smoothScrollTo(target) {
  44. target = target.length ? target : $('[name=' + this.hash.slice(1) +']');
  45. if (target.length) {
  46. $('html,body').animate({
  47. scrollTop: target.offset().top
  48. }, 1000);
  49. }
  50. }
  51. });
  52. // Hide Header on on scroll down
  53. var didScroll;
  54. var lastScrollTop = 0;
  55. var delta = 5;
  56. var navbarHeight = $('nav').outerHeight();
  57. $(window).scroll(function(event){
  58. didScroll = true;
  59. });
  60. setInterval(function() {
  61. if (didScroll) {
  62. hasScrolled();
  63. didScroll = false;
  64. }
  65. }, 250);
  66. function hasScrolled() {
  67. var st = $(this).scrollTop();
  68. // Make sure they scroll more than delta
  69. if(Math.abs(lastScrollTop - st) <= delta)
  70. return;
  71. // If they scrolled down and are past the navbar, add class .nav-up.
  72. // This is necessary so you never see what is "behind" the navbar.
  73. if (st > lastScrollTop && st > navbarHeight){
  74. // Scroll Down
  75. $('nav').removeClass('nav-down').addClass('nav-up');
  76. $('.nav-up').css('top', - $('nav').outerHeight() + 'px');
  77. } else {
  78. // Scroll Up
  79. if(st + $(window).height() < $(document).height()) {
  80. $('nav').removeClass('nav-up').addClass('nav-down');
  81. $('.nav-up, .nav-down').css('top', '0px');
  82. }
  83. }
  84. lastScrollTop = st;
  85. }
  86. //$('.site-content').css('margin-top', $('header').outerHeight() + 'px');
  87. });