Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate reverse scroll #3

Open
kadosh1000 opened this issue Jul 25, 2016 · 1 comment
Open

calculate reverse scroll #3

kadosh1000 opened this issue Jul 25, 2016 · 1 comment

Comments

@kadosh1000
Copy link

Hi, I am using scrollTo plugin which have this exact problem on rtl containers.

I was wondering if any of you have some kind of a function that knows to calculate the reverse scroll from a regular one.

Thanks

@joloco
Copy link

joloco commented Jun 18, 2020

I know this is four years too late, but it may help someone!

jQuery.fn.scrollLeftOriginal = jQuery.fn.scrollLeft;

jQuery.fn.scrollLeft = function(val) {

    if (getComputedStyle(this[0]).direction === 'rtl') {

        val = Math.abs(val);

        if      (jQuery.support.rtlScrollType==='negative') val = 0 - val;
        else if (jQuery.support.rtlScrollType==='positive') val = (this[0].scrollWidth - this[0].clientWidth) - val;

    }

    this.scrollLeftOriginal( val );

};

This will replace jQuery's built-in scrollLeft function with one that takes RTL into account. You call it like so:

$(element).scrollLeft(value);

Zero means the element is scrolled all the way to the right (as at first load). The 'value' parameter can be positive or negative, and will scroll the element to that position. (Note it scrolls it TO the position, not BY the amount specified.)

It checks whether the element is RTL, and just calls the normal jQuery scrollLeft() function if not, so it should be safe to drop in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants