• Resolved hshah

    (@hshah)


    I’ve added JS to my footer after reading posts on here, and that works perfectly (scrolls a CPT down to a CSS # with an additional offset of 55px).

    However, that is quite annoying when the user refreshes the page because Chrome usually returns to the same view, but with the jQuery in place, it moves away from that.

    Is there a way to only execute the jQuery if the page is right at the top?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author malihu

    (@malihu)

    Hi,

    I’m not sure which js you added in your footer. Can you post you page/site URL so I can check what happens?

    Thread Starter hshah

    (@hshah)

    Hi,

    I can’t provide a link because I’m playing around in a locked down staging environment, but here is the script I added:

    <script>
        (function($){
            $(window).on("load",function(){
                $.mPageScroll2id("scrollTo","#content-title",{
                    offset:55
                });
            });
        })(jQuery);
    </script>

    This works fine in the sense that every time the page loads, it scrolls to the specified ID + the offset.

    However, I’m wondering if there is a way to make it run only when the page is at the top? In Chrome, when refreshing a page, it reloads to the same position it was in when refreshed.

    If I’m towards the bottom, the script causes it to scroll up to the ID, and if I’m at the top, it scrolls down to the ID.

    I don’t want it to scroll up if I’m more than 50% down the page. If it’s reloaded anywhere between 0% and 50%, I want the script to run and do its thing.

    I hope that makes sense 🙂

    Plugin Author malihu

    (@malihu)

    I see. To trigger the scrollTo method only when the page is at the very top, try this:

    <script>
        (function($){
            $(window).on("load",function(){
                if($(window).scrollTop() == 0){
                    $.mPageScroll2id("scrollTo","#content-title",{
                        offset:55
                    });
                }
            });
        })(jQuery);
    </script>

    To trigger the method only when the page is scrolling is less than 50%, try this:

    <script>
        (function($){
            $(window).on("load",function(){
                var scrollPCT=Math.round(100 * $(window).scrollTop() / ($(document).height() - $(window).height()));
                if(scrollPCT <= 50){
                    $.mPageScroll2id("scrollTo","#content-title",{
                        offset:55
                    });
                }
            });
        })(jQuery);
    </script>

    Hope this helps 🙂

    Thread Starter hshah

    (@hshah)

    @malihu

    Thank you very much! That was much easier than I thought it would be lol 🙂

    Plugin Author malihu

    (@malihu)

    You’re welcome 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Scroll Condition – Current Position’ is closed to new replies.