scrollHeight 属性是一个只读属性,它返回该元素的像素高度,高度包含内边距(padding),不包含外边距(margin)、边框(border),是一个整数,单位是像素 px。
scrollHeight 的值等于该元素在不使用滚动条的情况下为了适应视口中所用内容所需的最小高度。 没有垂直滚动条的情况下,scrollHeight 值与元素视图填充所有内容所需要的最小值 clientHeight 相同。包括元素的padding,但不包括元素的 border 和 margin。
这是一个只读属性。
兼容:
主要浏览器都兼容。
element.scrollHeight
返回值: | 返回一个整数,表示该元素的像素高度。 |
---|
获取 div 元素的高度和宽度,包含内边距(padding):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Web176教程网(web176.com)</title> <style> #myDIV { margin-top: 10px; height: 250px; width: 250px; overflow: auto; } #content { height: 800px; width: 2000px; padding: 10px; background-color: coral; } </style> </head> <body> <p>点击按钮获取 id="content" 元素的高度(垂直)和宽度(水平)。</p> <button >
使用 padding(内边距), border(边框), scrollbar(滚动条) 以及 margin(外边距)来演示 scrollWidth 和 scrollHeight 属性的变化:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Web176教程网(web176.com)</title> <style> #content { height: 100px; width: 100px; background-color: coral; padding: 15px; border: 10px solid black; margin: 10px; overflow: scroll; } </style> </head> <body> <p>点击按钮获取 div 的整个高度(垂直)和宽度(水平)。</p> <p>高度包含内边距(padding),不包含外边距(margin)、边框(border),是一个整数,单位是像素 px。</p> <p><strong>提示:</strong> 尝试删除或更改不同的 CSS 属性值,然后再次点击该按钮以了解其工作原理。</p> <button >
返回元素 scrollHeight 和 scrollWidth 属性的值,然后将获取的高度和宽度来设置另外一个元素的 scrollHeight 和 scrollWidth 属性值:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Web176教程网(web176.com)</title> <style> #content { height: 100px; width: 100px; background-color: coral; padding: 15px; border: 10px solid black; margin: 10px; overflow: scroll; } </style> </head> <body> <p>点击第一个按钮返回 div 的 scrollHeight 和 scrollWidth 属性值。 点击第二个按钮,将另外一个 div 的宽度和高度设置为 scrollHeight 和 scrollWidth 返回的值。</p> <button >