6

The scrollLeft property of a div seems to return different values in different browsers when the body direction is rtl.

An example can be seen here - http://jsfiddle.net/auVLZ/2/

body { direction: rtl; }
div.Container { border: 5px solid #F00; width: 500px; height: 400px; overflow: auto; }    
div.Content { background-color: #00F; width: 900px; height: 380px; }​

<div id="divContainer" class="Container">
    <div class="Content"></div>
</div>
<br />
<input id="showScrollLeft" type="button" value="Show ScrollLeft" />​

$(document).ready(function()
{
    $("#showScrollLeft").click(function(e)
    {
        alert($("div.Container").scrollLeft());
    });
});​

Chrome - the initial value is 400, and when moving the scrollbar to the left it is 0.

IE8 - 0 and 400.

Firefox - 0 and -400.

What is causing this difference and how can it be handled?

Edit: note that this is happening with the "regular" scrollLeft as well - document.getElementById("divContainer").scrollLeft returns the same results.

j0k
  • 22,600
  • 28
  • 79
  • 90
Amit
  • 1,174
  • 2
  • 15
  • 22

2 Answers2

4

Although I was hoping to avoid that, I ended up using browser detection:

function GetScrollLeft(elem)
{
    var scrollLeft = elem.scrollLeft();
    if ($("body").css("direction").toLowerCase() == "rtl")
    {
        // Absolute value - gets IE and FF to return the same values
        var scrollLeft = Math.abs(scrollLeft);

        // Get Chrome and Safari to return the same value as well
        if ($.browser.webkit)
        {
            scrollLeft = elem[0].scrollWidth - elem[0].clientWidth - scrollLeft;
        }
    }
    return scrollLeft;
}
Amit
  • 1,174
  • 2
  • 15
  • 22
  • Browser detection is not a bad thing in itself, IE is. – jbkkd Aug 08 '12 at 08:55
  • I'm fighting this issue also.... made this plunk to show that canary fixed scrollLeft, but not scrollIntoView() http://plnkr.co/edit/UZvnlq?p=info – Jason Jan 29 '14 at 19:03
0

Browser incompatibilities. Try this

Adrian Heine
  • 4,051
  • 2
  • 30
  • 43
user1431317
  • 2,674
  • 1
  • 21
  • 18