Anchor Link Scroll Effect

I have to create a one page website. At the top of the page, I place a horizontal menu with some anchor links. When a user clicks on one of these, will arrive on a specific section of the page with a scroll effect. Can I create a similar effect in a Website Page in Hubspot?

I have added anchor tag to my http://seerion-2310098.hs-sites.com/seerionlanding on section about the event. I have named the anchor as #about. Dont know where I am going wrong. But then I went in my advanced menu option and in the linking to page option i just entered #about . But when I actually click on about on my menu , it just displays blank.

Sorry you're having trouble with this, I took a look at your page and I noticed your link at the top "About The Event" is linking to "//seerion-2310098.hs-sites.com/seerionlanding/#about". What is happening here is that it is reloading the page and then taking you to the section with the id of "about".

For starters, change your link to just say '#about'. What will happen now is your browser will register that click and append '#about' into the url. There won't be a page refresh so it should just take you directly to the section.

As far as the scroll animation, I couldn't find where you were including the script. Could you clarify for me what file it was placed in? Your console isn't throwing any errors, but it may not be running correctly.

Trying to do the same in a CTA on a HubSpot landing page: there is a button at the top which I'd like to link to the 'fill-out-this-form' section a little further down the page. For now, I have included the complete landing page URL + [#anchorlink] (in this case, '#facebookform'). Unfortunately, the CTA button now reloads the page entirely when clicked upon. And it does link to the right section on desktop, but not on mobile (then it just reloads the same page from the top).

So I found your tip about linking directly to the anchor with a link starting in '#-'. For some reason, however, the CTA designer module in HS won't let me link to the anchor link '#facebookform' that I created for this landing page.

Does directly linking to a 'bare' anchor only work for standard links, or also in CTA buttons? And if so, how?

Do you have a sample page that you are working on that I could take a look at?

Also, if you link to the page + your ID, your browser will reload on click because it is fetching that beginning URL. As for being able to linkto an anchor text in the CTA tool, I don't believe that field will accept just a hash-link. You will probably need to hardcode this into your page.

So i tried coming up with a few solutions for you, but first here is the Knowledge article that confirms what can and can't be put in the CTA fields. I think i've come up with a 3 possible solutions for you, each having their own pros/cons depending on your skillset.

First Solution (easiest)

You leave the button as is, with the [url]#anchor_id, the reload isn't going to be the end-all-be-all of ux issues, but I do understand that it is a little annoying.

Pros: It's built in and the cta works as designed

Cons: Will create a slightly annoying UX

Second Solution (easy):You can just hardcode the demo button into the section using plain HTML

Cons: You most likely won't get the tracking info, if you don't know JS, you may have a hard time implementing/updating it. Creating 1-off solutions just overcome something like this (which is working as designed) isn't usualy the best idea.

Personally, what I would try first is to get the exact html code that makes your button and try hardcoding it into the templat with just the #anchor-id as your 'href'. Then do a couple tests to see if you can get it to track the click. – If it does, then I'd say go that route.

Hopefully one of these ideas helps you out and gets you on the right track, let me know if you have any other questions!

What this snippet does is that it looks for any of your href's that include the hash(#) and uses `e.preventDefault()` to prevent the typical linking function. Which then allows you to add an animate to use as the new linking function. As long as you have a link with a hash (ie: <a href="#link1">Link 1</a>) and a div with a coresponding name, (ie: <div id="link1">Link 1's div</div>), this function will fire. You can also see a quick example I mocked up for you here on JSFiddle.

An important thing to note is that you can change the speed of the animation by changing the `900` in the function, this number works in "ms", so 900 is actually 900ms. This speed defines how long the whole animation will last.

I updated your header, but it was because you forgot to wrap the script in script tags. Anytime you place javascript into what is considered an HTML file, such as the head, or body of a page, you will need to tell the browser that the following text is to be read as a javascript command.

I checked your page and it seems to be working now, let me know how it looks on your end!

Thank you for sharing this code. I have added it to the header of a landing page template, but now any other links on the page (logo in nav and social buttons in footer) don't go anywhere when you try and click on them. If you hover over the logo or icons, it shows the web address that it should be doing to. The jss code is blocking the links for some reason. Any way to fix this? Is there a way to apply that code only to that specific button?

Just to explain this code a little better, this was for a specific use case where someone didn't have other anchor tags on their page, so we were able to target their link by just using the `a` tag. When you introduce more links to a page, such as social, it needs a little more scoping.

So let's take a look at what's happening, essentially this function is read as

"After load, when the target is clicked, do the code in the function. Now let's look at our params we set. We're catching the 'event'. A default event for an <a> is to take you to their href="" value. That's what this line is in there for.

event.preventDefault();

With this line we are basically telling the page on clicking an 'a' tag, to not let it take you to it's proposed href value. (even if you're using a dummy link, such as '#').

Your Solution

For this use case, I would suggest writing a class name on the button you need clicked, such as '.scroll-cta' and replacing the 'a' with that. That way, you're code is only scoped to that specific class, and you don't have to worry about having multiple a-tags or links on a page.

Thanks @Ty! I can't seem to make it work using a class name instead of "a". Does a new class have to be added and called out on the css document that is linked to the template or can I just use a class that was already in the rich text module, like below? I tried the below code and also tried creating a new class called "scroll-btn" that isn't called out in the css doc, but neither worked.

This code is in a rich text module and the class "btn" was previously defined to style the button. I didn't make a new class name. I am not using it as a CTA.

Any idea why the following javascript that makes my mobile navigation work is preventing my anchor scroll buttons to work on mobile? If I remove any navigation and the below javascript that is embeded in a custom HTML module at the bottom of the template, the buttons work in mobile.

Here is an example of a page below. I had to create a mobile button (hide on desktop) and a desktop button (hide on mobile) for each page if I want to use the scroll javascript on desktop. http://www.alignex.com/mechfuse-2017

Hi @leckerman, took me a minute, but I think i have a solution for you.

Alright, so first thing is, I noticed you weren't using the same button from desktop to mobile. You have two parent divs .hide-on-mobile and .hide-on-desktop. Since you have 2 buttons, only one of them had the class '.link-scroll' on it.

I hopped into your portal and added the class on the mobile version, that's step 1. Step 2 is, when you go into a mobile screensize, you are adding this css code

@media (max-width: 1024px)
body, html {
height: 100%;
}

For this Javascript to work, you will need to remove the 'height: 100%;' property.

Hi @Ty - That does fix the issue, however my navigation on mobile and destktop BOTH disapear when I comment out this code on the css doc...

@media (max-width: 1024px) { body, html {height: 100%;}

I removed the "link-scroll" class on the mobile button, because that was my work-around to try and get the button to at least work as an anchor link. When that class is on the "hide-on-desktop" module (mobile only) the button doesn't work AT ALL. I am leaving it as is for now, but will need to take it off sometime today if I can't get the issue fixed.

Hi, I am not looking for scroll but I am trying to figure out why my menu link takes me to the middle of a DIV where the Anchor is rather than taking me to the actual location of the anchor. Any help is appreciated.

Been trying to get something like this working on my site with no luck thus far. Have worked through the thread trying the proposed solutions and am sure I'm missing something critical but not obvious to me.

I'm trying to get the 'FIND OUT MORE - GET IN TOUCH' link on this page to scroll smoothly to an anchor I defined near the top of the page.

Don't worry Ty, I managed to make it work using your original method suggested right at the start of the thread. Thanks for your willing help!

Hi Ty

Thanks for your prompt response, really appreciate it. Sadly, however, I'm still not able to achieve what I'm looking for. I made the change you suggested and the result was that the smooth scroll animation happened, but it didn't scroll to the anchor I had defined - it simply scrolled the button/link to the top of the page.

I'm looking to smoothly scroll back to a specific anchor point on the page on click. Perhaps we can go back to basics and you can just describe the best way to do that to me?