How do I get the max value of scrollLeft?

前端 未结 8 550
夕颜
夕颜 2020-12-04 21:28

Okay I\'m using jQuery and currently getting max value of scrollbar doing this:

var $body = $(\'body\');
$body.scrollLeft(99999); // will give me the max val         


        
8条回答
  •  -上瘾入骨i
    2020-12-04 21:53

    AFAIK you have to calculate the maximum scroll value yourself, it is the difference between the parent/container's width and the child/content width.

    An example on how to do that with jQuery:

    HTML:

    Very long text or images or whatever you need

    CSS:

    #container {
        overflow:   scroll;
        width:      200px;
    }
    #content {
        width:      400px;
    }
    

    JS:

    var maxScroll = $("#content").width() - $("#container").width();
    // maxScroll would be 200 in this example
    

    In your case window is the parent/container and document the child/content.

    Here is a JSFiddle with this example: http://jsfiddle.net/yP3wW/

提交回复
热议问题