CSS: offsetWidth offsetHeight clientWidth clientHeight scrollWidth scrollHeight
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { margin: 20px; width: 200px; height: 300px; background-color: peru; border: 10px dotted magenta; /* border-bottom-width: 50px; */ padding: 20px; } section { background-color: plum; width: 100px; height: 200px; /* overflow: scroll; */ /* overflow-y: hidden; */ overflow: auto; border: 10px dashed teal; /* border-top-width: 50px; */ padding: 20px; } body { background-color: aqua; height: 1500px; width: 1500px; } html { background-color: tan; border: 10px dashed maroon; padding: 20px; } </style> </head> <body> <section> <div></div> </section> <script> const section = document.querySelector('section') const div = document.querySelector('div') </script> </body> </html>
document.documentElement水平和垂直表现不一样
scrollWidth = 10 + 20 + 8 + 1500
scrollHeight = offsetHeight = (10 + 20) * 2 + 8 * 2 + 1500 = 1576
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> * { margin: 0; padding: 0; } html, body { height: 100%; } div { width: 300px; height: 500px; margin: 30px; border: 20px dashed violet; padding: 40px; background-color: plum; position: relative; overflow-x: scroll; overflow-y: scroll; display: flow-root; } aside { width: 100px; height: 800px; background-color: peru; position: absolute; margin: 10px; padding: 20px; border: 30px dotted coral; top: 50px; } </style> </head> <body> <div> <aside></aside> </div> <script> const div = document.querySelector('div') const aside = document.querySelector('aside') </script> </body> </html>
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> div { margin: 20px; width: 200px; height: 300px; background-color: peru; border: 10px dotted magenta; /* border-bottom-width: 50px; */ padding: 20px; } section { background-color: plum; width: 100px; height: 200px; /* overflow: scroll; */ /* overflow-y: hidden; */ overflow: auto; border: 10px dashed teal; /* border-top-width: 50px; */ padding: 20px; } body { background-color: aqua; height: 500px; width: 500px; border: 10px dotted crimson; padding: 20px; } html { background-color: tan; border: 10px dashed maroon; padding: 20px; width: 400px; height: 400px; } </style> </head> <body> <section> <div></div> </section> <script> const section = document.querySelector('section') const div = document.querySelector('div') </script> </body> </html>