1

This is a peculiar issue faced when creating internal links on a WordPress page. There are 3 CTA buttons on the page that when clicked do jump to the respective sections on the same page. However, it is jumping past the section exactly by the height of the header of the page. As a solution, I added a function to each of these buttons so that it sets the scrollbar to pull the bar backward. Below is the code that isn't working. Could you please suggest some changes to make it work?

<h4>Connect with our Team</h4>
<p>Does your order need extra care? You’re in the right place. For special requests, help collecting recipient addresses, or to add corporate branding to your gifts, our Gift Concierge experts can help.</p>
<p><a class="btn" role="button" href="#faqs">FAQs</a> <a class="btn" role="button" href="#let-us-chat">Schedule a Call</a> <a class="btn" role="button" href="#submit-questions">Submit a Question</a></p>
<p>&nbsp;</p>
<h5 id="faqs">FAQs</h5>
<p>Some questions have easy answers. Whether you want to know <a href="https://help.packedwithpurpose.gifts/how-much-does-shipping-cost" target="_blank" rel="noopener">shipping prices</a>, <a href="https://help.packedwithpurpose.gifts/how-long-do-gifts-take-to-be-delivered" target="_blank" rel="noopener">how soon your gifts will ship</a>, or <a href="https://help.packedwithpurpose.gifts/how-do-i-add-my-logo-to-my-gift-boxes">how to add a company logo</a>, browse our <a href="https://packedwithpurpose.gifts/faq/">FAQ</a>s.</p>
<p>&nbsp;</p>
<h5 id="let-us-chat">Let's Chat</h5>
<p>Prefer to schedule a call? Meet with a member of our Gift Concierge team at a time most convenient for you:</p>
<ul>
    <li>Orders under 200 gifts, <a href="https://meetings.hubspot.com/kim67/gift-concierge">schedule here </a></li>
    <li>Orders of 200 or more gifts, <a href="https://meetings.hubspot.com/kim67/high-volume-gift-concierge">schedule here</a></li>
</ul>
<p>&nbsp;</p>
<h5 id="submit-questions">Submit questions</h5>
<p>Ask us anything below. A member of our team will get back to you within one business day.</p>
<p>
<style>
@media only screen and (max-width: 576px)  {
.page-id-2765 .btn {
    padding-left: 1rem;
    padding-right: 1rem;
    font-size: 1.1rem;
}
}

@media only screen and (min-width: 750px) and (max-width: 910px) {
.page-id-2765 .btn {
    padding-left: 1rem;
    padding-right: 1rem;
    font-size: 1.1rem;
}
}
</style>
</p>
<p><script>
function resetScrollpoint() {
 console.log("Hello Pramod");
 window.scroll(0,$(".col-nav").css('height'));
}
</script></p>
dee
  • 2,244
  • 3
  • 13
  • 33
CodeForGood
  • 767
  • 7
  • 30

1 Answers1

1

In your code it is missing where you are calling the resetScrollpoint() function but but at first sight you can do this ...

<a class="btn" role="button" href="javascript:void(0)" onclick="resetScrollpoint('#faqs')">FAQs</a>
<a class="btn" role="button" href="javascript:void(0)" onclick="resetScrollpoint('#let-us-chat')">Schedule a Call</a>
<a class="btn" role="button" href="javascript:void(0)" onclick="resetScrollpoint('#submit-questions')">Submit a Question</a>

And your function change to this:

resetScrollpoint(selector){
    if(!selector) return;
    window.scroll(0,jQuery(selector).offset().top - jQuery('#header').height());
}

Explanations Sorry for my English!

Your first aproach are right

<a class="btn" role="button" href="#faqs">FAQs</a>

That get the #faqs element scrolling to top, but the top is 0 and you have a fixed #header element so you need to scroll to top + #header height

Your second aproach with onclick="resetScrollpoint()" are also right but

  1. Is ambiguos because on the one hand you tell the interpreter to execute href="#faqs" but on the other hand you tell to interpreter to execute resetScrollpoint(). Solution: with href="javascript:void(0)" you tell to interpreter to ignore the default href action

  2. The logical inside your function resetScrollpoint() was wrong because you need to know how many pixels you need to scroll.

Solution: you tell to the function what is the element that you need (passing it as a parameter) resetScrollpoint('#faqs'),

with jQuery(selector).offset().top you find the position of your element received by the parameter selector (how many pixels from the top) and substract the height of #header element because otherwise the scrolling it would be too upstair

MTK
  • 3,300
  • 2
  • 33
  • 49
  • Thanks, but some error is showing up on this page. https://packedwithpurpose.gifts/gift-concierge/ Please click any of the three buttons and see the error message in the console. Please advise any solution. – CodeForGood Nov 13 '21 at 11:11
  • okay. i have looked at it. First your jquery is loaded but not properly loaded `Uncaught TypeError: $ is not a function` but if you try to replace `$` with `jQuery` that is only a little workarround `parseInt(jQuery(".col-nav").css('height'))` – MTK Nov 13 '21 at 11:25
  • Or you can try to redefine `$` after jquery load `$=jQuery` Be careful `jQuery` is case sensitive so only `Q` is capital case – MTK Nov 13 '21 at 11:29
  • Thanks, there is no more error now. But the internal link isn't working accurately. Can you please have a look. Do I need to use something like settimeout()? – CodeForGood Nov 13 '21 at 12:53
  • Show my last edit in the answer – MTK Nov 13 '21 at 15:17
  • It is now working perfect, MTK. Grateful to you. However, I feel a bit ashamed to just adopt this solution without understanding it 100%. – CodeForGood Nov 13 '21 at 15:53
  • @PKTG See explanations in my updated answer – MTK Nov 13 '21 at 22:47
  • MTK, I know I'm asking too much from you. Since I'm a developer without enough resources to equip myself, I largely rely on online resources for solutions and I'm of course making progress. Here is another minor issue with the button not appearing linear across all resolutions. If you find time, kindly look at this page for those three buttons that I am talking about and suggest possible solutions that I can implement. Thank you for everything. https://packedwithpurpose.gifts/gift-concierge/ – CodeForGood Nov 14 '21 at 08:26
  • A little workarround for your last question: if you put `

    ` on the container (parent) of buttons I think it work

    – MTK Nov 14 '21 at 11:49
  • Thanks for the additional help. Is there anything that you can do about my question on making the buttons on the page above responsive. – CodeForGood Nov 14 '21 at 14:31
  • Your jQuery solution has been implemented since you offered it. I didn't add the display:flex part yet. What is happening with the editor is strange. This `href="javascript:void(0)" onclick="resetScrollpoint('#..')"`at all three places don't stick around in WordPress editor. Your solution is perfect as my manager also said. The editor issue baffles us. I already posted about it here https://stackoverflow.com/questions/69961209/wordpress-attributes-added-in-anchor-tags-in-the-editor-disappear-after-a-while – CodeForGood Nov 14 '21 at 16:03
  • Sure, I will remove mine. – CodeForGood Nov 14 '21 at 16:31
  • Hi, I just tried to implement this solution in a new page, but it doesn't seem to work the way it does with the other one. https://dev-packed-with-purpose.pantheonsite.io/gift-concierge-new/ Please open this page and click Submit Form button to see that it is not working. JS code is present in the page itself so you can have a look. – CodeForGood Aug 18 '22 at 10:33
  • I posted this here. https://stackoverflow.com/questions/73397486/resetting-scroll-isnt-working-in-a-page-with-a-fixed-header – CodeForGood Aug 18 '22 at 10:47
  • @CodeForGood I can't answer because someone mark as duplicate and closed ... But this may work for you: `function resetScrollpoint(selector){if(!selector) return;console.log("Offset top before = " +jQuery(selector).offset().top);jQuery(selector).show(0,function(){console.log("Offset top after = " +jQuery(selector).offset().top);window.scroll(0,jQuery(selector).offset().top - jQuery('#header').height());});}` That happend because your form is not yet visible and `offset().top` return `0` ... but if you use `show()` before ... this may work – MTK Aug 20 '22 at 18:52
  • After you copy the modified function use https://beautifier.io/ for rearrange the code because is in one line – MTK Aug 20 '22 at 18:55