js動態獲得捲軸寬度 JavaScript,js
來源:互聯網
上載者:User
javascript|js|動態 我現在有一個層,層中有一個div,當內容比較多的時候就出現捲軸,我現在想,當捲軸出現的時候,div中表格的寬度等於div的寬度減去捲軸的寬度,如何才能去動態獲得捲軸的寬度,或者判斷捲軸的狀態既是否出現.
------------------------------------------------------------------------------------
是不是想這樣?
<div id="demo" style="width:200px;height:100px;background-color:buttonface;overflow:auto;">
never-online<br/>never-online<br/>never-online<br/>never-online<br/>never-online<br/>never-online<br/>never-online<br/>never-online<br/>
</div>
<div id="d2" style="position:absolute;background-color:#eee;"></div>
<script>
alert(demo.offsetWidth);
alert(demo.scrollWidth);
with(d2.style) {
top=demo.offsetTop;
left=demo.offsetLeft;
width=demo.scrollWidth;
height=demo.offsetHeight;
}
</script>
-------------------------------------------------------------------------------------- 如果我沒有理解錯的話,直接減不就可以了嗎?
<div id="demo" style="width:200px;height:100px;background-color:buttonface;overflow:auto;">
never-online
</div>
<div id="d2" style="position:absolute;background-color:#eee;"></div>
<script>
alert("div寬度: "+demo.offsetWidth);
alert("div滾動寬度: "+demo.scrollWidth);
alert("scrollbar寬度: "+(parseInt(demo.offsetWidth)-parseInt(demo.scrollWidth)));
demo.innerhtml = "never-online<br/>never-online<br/>never-online<br/>never-online<br/>never-online<br/>never-online<br/>never-online<br/>never-online<br/>"
alert("div寬度: "+demo.offsetWidth);
alert("div滾動寬度: "+demo.scrollWidth);
alert("scrollbar寬度: "+(parseInt(demo.offsetWidth)-parseInt(demo.scrollWidth)));
with(d2.style) {
top=demo.offsetTop;
left=demo.offsetLeft;
width=demo.scrollWidth;
height=demo.offsetHeight;
}
</script>
--------------------------------------------------------------------------------------