How do I get the max value of scrollLeft?

前端 未结 8 523
夕颜
夕颜 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条回答
  • 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:

    <div id="container">
        <div id="content">
            Very long text or images or whatever you need
        </div>
    </div>
    

    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/

    0 讨论(0)
  • 2020-12-04 21:56

    Simple solution is

    var maxScrollLeft = $(document).width() - $(window).width();
    
    0 讨论(0)
提交回复
热议问题