.scrollLeft() method not working

2019-08-29 11:42发布

问题:

I have page with some jQuery scripts. Everything is fine except IE. I can't resolve where's the problem with scrolling.

Whole page is one long horizontal page. Everytime when I change the PAGE (identifier number of page), web should scroll left or right. Here's some code of how I handle previous button action:

$('#previous').click(function() {
        var width = $(window).width();
        var leftOffset = $(document, window).scrollLeft();
        var browser = get_browser();
        hidePaging();

        $((browser == "Firefox" || browser == "msie") ? "html" : "body").animate({
            scrollLeft: page > 2 ? width * 2 : leftOffset - width
            },
            1000, 
            function () {
                page = page > 2 ? 2 : page - 1;
                console.log(page);
                fixPagingAppearance();
            }
        );
    });

My question is: "Why scroll in IE not working?!"

Thanks for reply.

回答1:

I think you should use $("html,body").animate({--your code--}) instead of using either $("html") or $("body") .

Replace this line:

var leftOffset = $(document, window).scrollLeft();

with

 var leftOffset = $(document).scrollLeft();

for IE.

Reference: http://forum.jquery.com/topic/animate-scrollleft-get-problems-in-ie-and-ff