sticky-kit icon indicating copy to clipboard operation
sticky-kit copied to clipboard

Horizontal scroll issue

Open dmitry opened this issue 10 years ago • 7 comments

It doesn't work well, when I'm scrolling horizontaly.

screenshot-1

It works when it's on the top (because no absolute positioning used in this case):

screenshot-2

dmitry avatar Jul 03 '14 17:07 dmitry

I have got the same problem it stay fixed during an horizontal scroll

hlehmann avatar Aug 05 '14 15:08 hlehmann

We use this workaround, to recalculate horizontal scrolling.


        /* Workaround to fix horizontal scrolling for sticky contents.
         *
         * When do horizontal scroll or resize window it does not handle it
         * properly.
         */
        update_horizontal_scroll = function () {
            $content
                .each(function () {
                    var calculated_left, self;

                    self = $(this);

                    if (self.css('position') === 'fixed') {
                        calculated_left = self.parent().offset().left - $(window).scrollLeft();
                    } else {
                        calculated_left = 0;
                    }
                    self.css('left', calculated_left + 'px');
                });
        };
        $content
            .on('sticky_kit:stick', update_horizontal_scroll);
        $(window)
            .on('resize', update_horizontal_scroll)
            .on('scroll', update_horizontal_scroll);

bogdangi avatar Jan 02 '15 14:01 bogdangi

@bogdangi thanks for the snippet. I also hope that the maintainer of this plugin will include the fix right in it's core.

dmitry avatar Jan 02 '15 18:01 dmitry

I also prepared example for it here http://jsfiddle.net/zog1jh29/1/

When you scroll e little bit to start stuck elements and after scroll horizontal the sticky element float on it.

bogdangi avatar Jan 05 '15 08:01 bogdangi

Hey, can someone tell me where to put the workaround code in? I tried and tried but i can not find out...

cuthulino avatar Dec 01 '15 21:12 cuthulino

Here's a clean solution: http://jsfiddle.net/wyne/gyp3kfbu/

wyne avatar Apr 01 '16 07:04 wyne

Solution at http://jsfiddle.net/wyne/gyp3kfbu/ works good, except initial position (when the page content horizontally scrolled to right and the page being reloaded).

So you should force trigger the "scroll" event after the content loaded.

$(window).on("load", function(){
    if ($(window).scrollLeft() > 0) // only if the viewport scrolled horizontally
        $(window).scroll(); // trigger again for sure
});

$(function() {
    if ($(window).scrollLeft() > 0) {// only if the viewport scrolled horizontally
        $(window).scroll(); // trigger again for sure
        setTimeout(function(){
            $(window).scroll(); // trigger again for sure
        }, 300);
    }
});

utilmind avatar Oct 25 '16 01:10 utilmind