Home  >  Article  >  Web Front-end  >  获得html元素自身的宽度_html/css_WEB-ITnose

获得html元素自身的宽度_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 09:13:401341browse

想要控制html元素显示的clip量.
最好是要知道html元素自身能有多么大的宽度.

接上一文章.

父nav截断了子元素的显示.

那么使用鼠标可以获得子元素的实际需要的宽度.
加载完毕之后,可以使用js动态控制nav的宽度.:想要100%,120%,80%的宽度.

用到javascript属性.offsetWitdh
与此类似的还有,scrollWitdh,scrollLeft,clientWidth
参考链接:HTML精准定位

获取宽度

获取鼠标位置.
使用onmouseover来监测.
使用到的javascript关键词:eventX,eventY,offsetX,offsetY
使用相对于容器坐标.则需要offset.

进一步 可以使用设置scrollLeft修改滚动位置.

(本文章功用测试源代码):

javascript 获取dom宽度
ko
<script> var inner=document.getElementById('inner'); var outer=document.getElementById('outer'); inner.innerHTML = inner.offsetWidth+" / "+outer.offsetWidth; outer.style.width = inner.offsetWidth*.8 + "px"; inner.innerHTML = inner.innerHTML+" / "+outer.offsetWidth; inner.onmousemove=function(){ inner.innerHTML=event.offsetX+"/"+event.clientX; } </script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn