Anchor Link Scroll Effect

New Contributor

Hello everyone, 

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?

Reply
0 Upvotes
29 Replies 29
Advisor

yep. In a Rich Text module there is an option under the insert menu titled "anchor" that will place an ID on an empty "a" tag. Then you just need to link to that name the usual way - #linktoname.

You may also want to add smooth scrolling with this javascript: https://css-tricks.com/snippets/jquery/smooth-scrolling/

--
Stefen Phelps
Web Developer / Co-founder
Kelp Creative Agency
Twitter — @stefen
New Contributor

Hi,

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.

Can you please tell me where m i going wrong?

 

TIA

Reply
0 Upvotes
Community Manager
Community Manager

Hi @poojanadkarni1,

 

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.

 

Let me know if this fixes your problem!

-- Ty

Reply
0 Upvotes
Visitor

Hi Ty,

 

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? Smiley Happy

 

Thanks in advance for your help, cheers from Amsterdam,

Lisanne

Reply
0 Upvotes
Community Manager
Community Manager

Hi @lisannebuisman,

 

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.

 

Let me know if you have any other questions!

-- Ty

Reply
0 Upvotes
Visitor

Hi @Ty

 

Thanks for the quick reply! (See how my rhiming is on point? Smiley Wink )

 

This is the landing page: https://www.clevergig.nl/nl/facebook/planning-software-voor-jouw-flexibele-personeel .

 

But if the CTA link field doesn't except a '#anchor' link then I'm afraid it won't work for us Smiley Sad

Reply
0 Upvotes
Community Manager
Community Manager

Hey again @lisannebuisman,

 

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

<span class="hs-cta-node hs-cta-11420eeb-d4ce-4169-9ff3-84be0e6db6a8" id="hs-cta-11420eeb-d4ce-4169-9ff3-84be0e6db6a8" style="visibility: visible;" data-hs-drop="true">
<a id="cta_button_1794004_0d52d041-2aa9-45a5-810b-a198a4a3a07e" class="cta_button " href="#your_link_id" style="" cta_dest_link="https://app.hubspot.com/meetings/info11760" title="Demo wanneer het jou uitkomt">
<strong>
<span style="font-size: 15px;">Demo wanneer het jou uitkomt</span></strong>
</a>
</span>

Some pros an cons of this soution:

Pros: No extra scripting needed, just a simple hardcoded button. 

Cons: I don't think you'll be able to see the tracking – You may be able to hack it together where the ids match and HubSpot sees it by clicks on said ID, but I don't know that for sure.

 

Third Solution (Probably the hardest of these solutions):

You could write some custom javascript to replace the href of that specific button on page load. For example:

<script>
$( document ).ready(function() {
   $('#your_cta').attr('href', '#your_anchor_id');

});
</script>

Pros: If you know JS, it's easy to edit/implement

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!

Reply
0 Upvotes
Community Manager
Community Manager

Hi Vanessa,

 

From the sound of it, it sounds like you're looking for a smooth scrolling effect. This can be down easily with just a little bit of jQuery. 

 

You can insert this snippet into your JS file, or the head-tag of your template:

$(document).ready(function(){
	$('a[href^="#"]').on('click',function (e) {
	    e.preventDefault();

	    var target = this.hash,
	    $target = $(target);

	    $('html, body').stop().animate({
	        'scrollTop': $target.offset().top
	    }, 900, function () {
	        window.location.hash = target;
	    });
	});
});

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. 

 

Hope this helps!

-- Ty

Regular Contributor

Hi Ty

 

Looking to do the same thing here. I can't seem to implement this and not quite sure what I'm missing... I've added it to the head but no change.

 

I'm trying to put the effect on the 'Let's work together' link here: https://www.zoodigital.com/contact

 

Any advice?

 

Cheers, Jonny

 

Reply
0 Upvotes
Community Manager
Community Manager

Hi @JonnyZOO,

 

I ended up rewriting this over the weekend to make it a little more compact, you could try replacing the original code with this instead.

$(document).on('click', 'a', function(event){
    event.preventDefault();

    $('html, body').animate({
        scrollTop: $( $.attr(this, 'href') ).offset().top
    }, 500);
});

Then change the 500 to adjust speed. Remember when using animate in jQuery the numbers are read in milliseconds. So 1000 would be a 1-second scroll effect.

 

Let me know if this works for you!

-- Ty 

Regular Contributor

Hi Ty

 

Thanks for the quick reply. I've added this code to the custom head but I'm getting a message of 'There is invalid markup in your custom head'.

Screen Shot 2017-07-05 at 14.27.43.png

 

Any ideas?

 

 

Cheers, Jonny

Community Manager
Community Manager

Hi @JonnyZOO,

 

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.

 

Screen Shot 2017-07-05 at 9.34.50 AM.png

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

-- Ty

Regular Contributor

Awesome, good stuff Ty - thank you!

Reply
0 Upvotes
Top Contributor

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?

 

This is the page where I am having the issue. The "Save my seat" button in the banner image works, but no other links are clickable - http://www.alignex.com/mechfuse-2017

Reply
0 Upvotes
Community Manager
Community Manager

Hey @leckerman,

 

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.

 

I'll break this code down user colors:

 

$(document).on('click', 'a', function(event){
    event.preventDefault();
});

So here is the basic declaration we are calling.

Blue is your action, how you will trigger the event

Red is your target, what will be triggering this event

Green is your functions arguments/params

 

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.

 

Your new code would look something like this:

 

 

$(document).on('click', '.your-new-btn-class', function(event){
    event.preventDefault();
    $('html, body').animate({
         scrollTop: $( $.attr(this, 'href') ).offset().top
     }, 500);
});

Try this and let me know how it goes, if it's still not working, it will help narrow down what could be going wrong.

 

Hope it helps!

-- Ty

Reply
0 Upvotes
Top Contributor

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. 

<p><span class="btn"><a href="#savemyseat" style="text-decoration: none;">SAVE MY SEAT</a></span></p>

 

This code is the the head of the template under "Additional HTML Head Markup for this Layout"

<script>
$(document).on('click', '.btn', function(event){
event.preventDefault();

$('html, body').animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 500);
});
</script>

Reply
0 Upvotes
Highlighted
Community Manager
Community Manager

@leckerman

 

Alright, I see you issue.

 

So you have it set up so when you click the class of 'btn', it prevents the default event (which should be linking it to your #id). 

 

However, you're html doesn't have an <a> tag with the class of 'btn'.

 

<span class="btn"><a href="#savemyseat" style="text-decoration: none;">SAVE MY SEAT</a></span>

So what you need to do is scope your actions down so that your trigger is clicking the href.

 

 

This can be done in 2 different ways, editing the script, or editing the html.

 

 

Editing your script

 

$(document).on('click', '.btn a', function(event){ });

By adding '.btn a' you are telling your script to only execute your function when you click the <a> child within the '.btn' parent.

 

 

Editing your HTML

<span class="btn"><a href="#savemyseat" class="link-btn" style="text-decoration: none;">SAVE MY SEAT</a></span>

You can add a new class in your html on your <a> tag to handle this function as well, I used 'link-btn' for a naming convention, but feel free to use whatever you want.

 

Then remember to adjust your function command accordingly

$(document).on('click', '.link-btn', function(event){ });

Either of these solutions should fix your issues, let me know if you're still having some trouble!

 

-- Ty

Top Contributor

You did it @Ty! All fixed. Thank you so much for your help and for taking the time to explain why too. That was VERY helpful! 

Reply
0 Upvotes
Top Contributor

Hi @Ty,

 

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

 

<script>
/*-------------------------------------------------------------------------------*/
/* MOBILE NAV - TOGGLE SIDE MENU
/*-------------------------------------------------------------------------------*/
$(function() {

// Prepend a mobile icon to the header
$(".custom-menu-primary").after('<a class="mobile-icon"><span></span></a>');

// Prepend a close icon to the menu
$(".custom-menu-primary .hs-menu-flow-horizontal>ul").before('<a class="close-icon"><span></span></a>');

// Add body class on mobile icon click
$(".mobile-icon, .close-icon").click(function(){
$("body").toggleClass("show-mobile-nav ");
});

// Dropdown sub menu
$(
".hs-menu-depth-1.hs-item-has-children"
).not(".active-branch:nth-child(2)").click(function() {
$(this).toggleClass("drop");
$(this).find("> ul").toggle();
});

// Set the menu height to 100% of the document
function setMenuHeight(){
var height = $(document).outerHeight(true);
$(".custom-menu-primary").height(height);
}
setMenuHeight();

// Wrap body contents with a div so the transforms will work
$('body> div').find('script:not(script[type="IN/Share"])').remove().end().wrapAll('<div id="site-wrapper"></div>');

});

</script>

Reply
0 Upvotes
Community Manager
Community Manager

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.

 

Hope this helps you out!

-- Ty

Reply
0 Upvotes
Top Contributor

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. 

 

Any suggestions? Thank you!!

Reply
0 Upvotes
Regular Contributor

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.

 

Reply
0 Upvotes
Occasional Contributor

This is exactly what I was looking for, thanks!

Reply
0 Upvotes
New Contributor

Hi there,
I'm looking to do something similar, but I think it requires a little bit of different coding (if it's possible to do).

I am wanting to create a button within a custom html module that will be a scrolling anchor link to an image contained within a rich text module farther down on the same page.

Reply
0 Upvotes
Community Manager
Community Manager

Hey there @avherzberg,

 

This should work just the same, in your custom HTML module, you should create your link with a hash, ie:

<a href="#your-id">Link Text</a>

Then in the rich-text module you will want to make sure either the image OR the module is tagged with your id, for example:

<img id="your-id" src="your-img-src" alt="your image alt text" />

Keep in mind that in your "Image ID", you will not be including the '#', you only use that in your a tag's href to tell the page to look for the the element with an ID that matches your 'your-id'.

 

Let me know if you have any other questions!

-- Ty

Reply
0 Upvotes
Occasional Contributor

Hey guys

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. 

 

http://www.adglow.com/web-summit-follow-up?hs_preview=WNNSdvRh-5436505723

 

I have included this script in the head:

<script>$(document).on('click', '#top', function(event){
event.preventDefault();

$('html, body').animate({
scrollTop: $( $.attr(this, 'href') ).offset().top
}, 500);
});</script>

 

And have set up the link like this:

<p><strong><a href="#top">FIND OUT MORE - GET IN TOUCH NOW</a></strong></p>

 

Help appreciated. 

Reply
0 Upvotes
Community Manager
Community Manager

Hi @phaslehurst,

Looking at your code, it seems you're calling your href as if it was an id.

In the line 

$(document).on('click', '#top', function(event){ }

 

This is saying "On click of element with the id of "top", perform this function". But your button is just an <a> with the href of #top.

<p><strong><a href="#top">FIND OUT MORE - GET IN TOUCH NOW</a></strong></p>

To fix this, you can either add a unique class or id to your <a> then call your function with that element's id or class. So the easiest fix would be changing your text link to this:

<a id="top" href="#top">FIND OUT MORE - GET IN TOUCH NOW</a>

Let me know how this works out for you!

-- Ty

Reply
0 Upvotes
Occasional Contributor

***UPDATE***

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?

Thanks again in advance. Smiley Happy

Phil

Reply
0 Upvotes
New Contributor

Hi Tyler,

 

I'm also trying to add the scroll effect to this page here: 

http://updox-1871178.hs-sites.com/updox-pharmacy-connect-stage

 

Unfortunately, I'm not having any luck with getting this implemented. Any advice?

 

Thanks in advance! 

-Jesse

 

 

Reply
0 Upvotes