Site logo

[Resolved] Sticky button – Javascript for ‘make sticky button visible on scroll’

Home Forums Support [Resolved] Sticky button – Javascript for ‘make sticky button visible on scroll’

Home Forums Support Sticky button – Javascript for ‘make sticky button visible on scroll’

Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #2584375
    eastwood501

    Hi, this is a question for David.

    I have created a hook/element for a sticky left hand side Orange button.

    I am using Davids tutorial on the javascript for making the sticky button appear after scroll. (https://generatepress.com/forums/topic/make-element-appear-after-scroll/)

    However, i am unable to get this to work, David are you able to assist on some CSS to make the orange button appear after scroll?

    Thanks in advance.

    #2584700
    David
    Staff
    Customer Support

    Hi there,

    1. Edit the Button and in Advanced give it a HTML Anchor of: sticky-button

    2. in the JS look for this line:

    const watchedEL = document.querySelector('#see-buying-options');

    and change it to:

    const watchedEL = document.querySelector('#sticky-button');

    Let me know once that is done

    #2584714
    eastwood501

    Hi David,

    Thanks for your reply.

    I have dont both steps 1 & 2 now.

    Cheers

    #2584790
    David
    Staff
    Customer Support

    The sticky-button has to be added to the Advanced > HTML Anchor field not in the CSS field

    #2584800
    eastwood501

    Hi David,

    My apologise, i have now added sticky-button under the HTML ANCHOR field and removed it from the CSS field.

    Kind Regards

    #2584956
    eastwood501

    I have added some more information below, so far I have tried the CSS from the tutorial here https://generatepress.com/forums/topic/make-element-appear-after-scroll/ to the new script but its not working still.
    Many thanks in advance.

    #2585385
    David
    Staff
    Customer Support

    Unfortunately you can’t just mix and match codes 🙂
    Can you remove any Javascript and CSS related to making this button sticky. And then let me know.

    #2585395
    eastwood501

    Hi David,
    I have removed any CSS and javascript that was related to the button. As per the homepage, the button is now fixed and in the top middle of the page.
    Regards

    #2585488
    David
    Staff
    Customer Support

    Can you clear any caches as i still see the scripts and CSS on that page.

    #2585505
    eastwood501

    Hi David,
    I have now purged and cleared the host and WordPress and cloudfare caches.
    Hopefully clear on your end.

    #2585797
    David
    Staff
    Customer Support

    OK. Lets get the JS working first.

    1. Create your Hook element, and set the Hook to wp_footer

    2. Add this Code to the Hook:

    <script>
    // debounce scroll eventListener
    function debounceX(func, wait = 10, immediate = true) {
      let timeout;
      return function() {
        let context = this, args = arguments;
        let later = function() {
          timeout = null;
          if (!immediate) func.apply(context, args);
        };
        let callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
      };
    };
    // check position 
    function checkScrollPosition() {
      let windowY = window.scrollY;
      let targetY = 600; // target scroll position
      if (windowY < targetY) {
        watchedEL.classList.remove('is-visible');
      } else {
        watchedEL.classList.add('is-visible');
      }
      scrollPos = windowY;
    }
    //  add listener
    const watchedEL = document.querySelector('#sticky-button');
    window.addEventListener('scroll', debounceX(checkScrollPosition));
    </script> 

    3. Set the Display Rules to the Entire site or wherever you want it displayed.

    4. Publish that,

    Then let me know

    #2585799
    eastwood501

    Hi David,

    Script has been place into wp footer element, rule set to entire site and now published!

    Cheers

    #2585834
    David
    Staff
    Customer Support

    Can you move the #sticky-button ID to the Container Block that the buttons site inside of. It currently has a hide-on-mobile class attached to it for reference.

    Can you purge those caches 🙂 Or maybe disable them whilst you’re developing

    #2585938
    eastwood501

    Hi David,

    I have removed the ‘hide on mobile’ and moved the #stick-button ID to the container block.
    Cache purged as well.

    #2585981
    David
    Staff
    Customer Support

    Now add this CSS:

    #sticky-button {
        position: fixed;
        left: 0;
        transform: rotate(90deg);
        transform-origin: bottom left;
    }
    #sticky-button .gb-button {
        opacity: 0;
        transform: translatey(100%); 
        transition: all 0.4s ease-in-out;
    }
    #sticky-button.is-visible .gb-button {
        transform: translatey(0); 
        opacity: 1;
    }
Viewing 15 posts - 1 through 15 (of 18 total)
  • You must be logged in to reply to this topic.