画面上部ナビバーのタイトルを押すと、該当箇所に自動的にスクロールされる機能を作ったのですが、
1度目は反応するものの、一度その機能を使ってしまうと、サイトを更新/リフレッシュしない限り動かなくなってしまいます。
誤りがわからず、御指南いただけますと幸甚です。
javascript
1$(document).on('turbolinks:load', function(){ 2 "use strict"; // Start of use strict 3 4 // Smooth scrolling using jQuery easing 5 $('a.js-scroll-trigger[href*="#"]:not([href="#"])').click(function() { 6 if (location.pathname.replace(/^//, '') == this.pathname.replace(/^//, '') && location.hostname == this.hostname) { 7 var target = $(this.hash); 8 target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); 9 if (target.length) { 10 $('html, body').animate({ 11 scrollTop: (target.offset().top - 57) 12 }, 1000, "easeInOutExpo"); 13 return false; 14 } 15 } 16 }); 17 18 // Closes responsive menu when a scroll trigger link is clicked 19 $('.js-scroll-trigger').click(function() { 20 $('.navbar-collapse').collapse('hide'); 21 }); 22 23 // Activate scrollspy to add active class to navbar items on scroll 24 $('body').scrollspy({ 25 target: '#mainNav', 26 offset: 57 27 }); 28 29 // Collapse Navbar 30 var navbarCollapse = function() { 31 if ($("#mainNav").offset().top > 100) { 32 $("#mainNav").addClass("navbar-shrink"); 33 } else { 34 $("#mainNav").removeClass("navbar-shrink"); 35 } 36 }; 37 // Collapse now if page is not at top 38 navbarCollapse(); 39 // Collapse the navbar when page is scrolled 40 $(window).scroll(navbarCollapse); 41 42 // Scroll reveal calls 43 window.sr = ScrollReveal(); 44 sr.reveal('.sr-icons', { 45 duration: 600, 46 scale: 0.3, 47 distance: '0px' 48 }, 200); 49 sr.reveal('.sr-button', { 50 duration: 1000, 51 delay: 200 52 }); 53 sr.reveal('.sr-contact', { 54 duration: 600, 55 scale: 0.3, 56 distance: '0px' 57 }, 300); 58 59 // Magnific popup calls 60 $('.popup-gallery').magnificPopup({ 61 delegate: 'a', 62 type: 'image', 63 tLoading: 'Loading image #%curr%...', 64 mainClass: 'mfp-img-mobile', 65 gallery: { 66 enabled: true, 67 navigateByImgClick: true, 68 preload: [0, 1] 69 }, 70 image: { 71 tError: '<a href="%url%">The image #%curr%</a> could not be loaded.' 72 } 73 }); 74 75})(jQuery); // End of use strict 76
画面ナビバーの、例えば、Product & Services
を押すとその部分に自動スクロールされます。
1度目は動きますが、その後、サイトをリフレッシュしない限り動かなくなります。
あなたの回答
tips
プレビュー