- This topic has 27 replies, 3 voices, and was last updated 4 years, 7 months ago by
Elvin.
-
AuthorPosts
-
December 11, 2017 at 9:55 pm #447435
Tom
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 #447470Tom
Lead DeveloperLead DeveloperHi there,
Take this line:
scrollTop: target.offset().topAnd minus the height of your navigation:
scrollTop: target.offset().top - 60December 12, 2017 at 2:18 pm #448131Tom
hi 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 #448218Tom
Lead 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 #448241Tom
works now, just had to delete cache…whoops
thanks mate
December 12, 2017 at 7:46 pm #448261Tom
Lead DeveloperLead DeveloperNo problem ๐
December 13, 2017 at 12:40 am #448343Brett
Heya,
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 #448705Leo
StaffCustomer 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 #448751Brett
Ahhh 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 #448754Leo
StaffCustomer SupportNo problem ๐
January 24, 2021 at 12:23 pm #1631806George
Is there a way to do that with Vanilla JS or maybe through a filter?
January 24, 2021 at 10:47 pm #1632205Elvin
StaffCustomer 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 #1632578George
Is the
200in thesmoothScroll(href,1000,200);line the offset? Doesn’t seem to be doing anything.January 25, 2021 at 3:53 pm #1633402Elvin
StaffCustomer 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 #1633417George
Yeah, not luck for me regarding the offset. How do you include the JS? I used a
wp_headhook sincewp_footerproduced an error. -
AuthorPosts
- You must be logged in to reply to this topic.