- This topic has 27 replies, 3 voices, and was last updated 3 years, 1 month ago by Elvin.
-
AuthorPosts
-
December 11, 2017 at 9:55 pm #447435Tom
Hi
I’ve searched through the forums but cant find the answer. I was wondering how it’s possible to add a top margin to the smooth scroll. At the moment, the sticky header is hiding the section titles when scrolling down the page (1 page website).
This is the code I’m currently using:<script> jQuery('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) { var target = jQuery(this.hash); target = target.length ? target : jQuery('[name=' + this.hash.slice(1) +']'); if (target.length) { jQuery('html,body').animate({ scrollTop: target.offset().top }, 1000); return false; } } }); </script>
Thanks
December 11, 2017 at 11:23 pm #447470TomLead DeveloperLead DeveloperHi there,
Take this line:
scrollTop: target.offset().top
And minus the height of your navigation:
scrollTop: target.offset().top - 60
December 12, 2017 at 2:18 pm #448131Tomhi Tom,
Didn’t seem to change anything. I even changed the margin to -500 without seeing a difference.
<script> jQuery('a[href*=#]:not([href=#])').click(function() { if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') || location.hostname == this.hostname) { var target = jQuery(this.hash); target = target.length ? target : jQuery('[name=' + this.hash.slice(1) +']'); if (target.length) { jQuery('html,body').animate({ scrollTop: target.offset().top - 500 }, 1000); return false; } } }); </script>
December 12, 2017 at 5:24 pm #448218TomLead DeveloperLead DeveloperHmm I was using that exact code on this site until we disabled the sticky menu on scroll down.
Be sure to clear your browser cache after making the change etc..
December 12, 2017 at 6:43 pm #448241Tomworks now, just had to delete cache…whoops
thanks mate
December 12, 2017 at 7:46 pm #448261TomLead DeveloperLead DeveloperNo problem π
December 13, 2017 at 12:40 am #448343BrettHeya,
I’m also using this code – for smooth scrolling on anchor links.
Works great when logged in as admin but just found out it does not work incognito / when not logged in.
Any of you Tom’s also having this problem? π
Cheers.
December 13, 2017 at 9:00 am #448705LeoStaffCustomer SupportHmm that sounds like some aggressive caching to me.
I’ve been using the exact same code on this site for the Contact button and it has been working: http://www.csmwhistler.com/
December 13, 2017 at 9:51 am #448751BrettAhhh cheers for pointing that out Leo,
I think we can call it Violent Caching! π
Note to self: Render blocking JS is not very friendly!
Like your spinning menu cog by the way!
It’s the small details eh. πThanks…
December 13, 2017 at 9:54 am #448754LeoStaffCustomer SupportNo problem π
January 24, 2021 at 12:23 pm #1631806GeorgeIs there a way to do that with Vanilla JS or maybe through a filter?
January 24, 2021 at 10:47 pm #1632205ElvinStaffCustomer SupportHi George,
It’s pretty tricky to directly convert the provided jQuery to Vanilla JS so I just made one from scratch. (I copied the selector tho. lol)
To be frank, this is out of our scope but coincidentally, I’ve been playing around with this for a while and I found this version of the scripts I’ve done, my most preferred:
<script> var smoothScrolltarget = document.querySelectorAll('a[href*="#"]:not([href="#])'); for(i=0;i<smoothScrolltarget.length;i++){ smoothScrolltarget[i].addEventListener("click", function(event) { const href = this.getAttribute("href"); smoothScroll(href,1000,200); }); } function smoothScroll(target,duration,yOffset){ var target = document.querySelector(target); var targetPosY = target.getBoundingClientRect().top - yOffset; var startPos = window.pageYOffset; var distance = targetPosY - startPos; var startTime = null; function animate(currentTime){ if(startTime === null) startTime = currentTime; var timeElapsed = currentTime - startTime; var run = ease(timeElapsed,startPos,distance,duration); window.scrollTo(0,run); if(timeElapsed < duration) requestAnimationFrame(animate); } function ease(t, b, c, d) { t /= d; t--; return c*(t*t*t*t*t + 1) + b; } requestAnimationFrame(animate); } </script>
Source: https://www.youtube.com/watch?v=oUSvlrDTLi4
I’ve modified his code a bit to automate adding target instead of hardcoding it. I’ve also added the offset but most are similar to the reference/source.
January 25, 2021 at 5:00 am #1632578GeorgeIs the
200
in thesmoothScroll(href,1000,200);
line the offset? Doesn’t seem to be doing anything.January 25, 2021 at 3:53 pm #1633402ElvinStaffCustomer SupportYes 200 is the top offset. You won’t notice the difference if the anchor it at the bottom of the page.
You can try testing it on a page with plenty of content and play around with its value. I’ve already tested this and it works well on my end. π
January 25, 2021 at 4:11 pm #1633417GeorgeYeah, not luck for me regarding the offset. How do you include the JS? I used a
wp_head
hook sincewp_footer
produced an error. -
AuthorPosts
- You must be logged in to reply to this topic.