Difference between $(window).width() vs $(document).width()

后端 未结 6 1021
灰色年华
灰色年华 2020-12-08 03:52

What is the major difference between $(window).width() vs $(document).width() in jQuery? Whether window denotes the browser and document represents

6条回答
  •  旧时难觅i
    2020-12-08 04:40

    From the documentation of width():

    This method is also able to find the width of the window and document.

    $(window).width();   // returns width of browser viewport
    $(document).width(); // returns width of HTML document
    

    Simple jsFiddle Demo

    In the demo, I have set html { width: 1000px; }, which is bigger than the viewport.

    The width of the body of your HTML page is a third value. $('body').width() can also differ from the other two (try body { margin: 100px; } for example).

提交回复
热议问题