如何获取“overflow-x: auto;”内元素的全宽区域?
我对此进行了编码:
$(document).ready(function() {
$("button").click(function() {
alert("Width of #text: " + $("#text").width());
});
});
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
#container {
width: 300px;
height: 150px;
background-color: grey;
position: relative;
}
#scroll-area {
overflow-x: auto;
height: 100%;
}
#text {
white-space: nowrap;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="container">
<div id="scroll-area">
<div id="text">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</div>
</div>
</div>
<button>Get width of #text</button>
我想计算#Text
元素的宽度,#Container
元素。目前,它显示了我300px
结果。但这是容器的宽度,而不是文本元素的宽度。
I coded this:
$(document).ready(function() {
$("button").click(function() {
alert("Width of #text: " + $("#text").width());
});
});
* {
margin: 0;
padding: 0;
font-family: sans-serif;
}
#container {
width: 300px;
height: 150px;
background-color: grey;
position: relative;
}
#scroll-area {
overflow-x: auto;
height: 100%;
}
#text {
white-space: nowrap;
}
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<div id="container">
<div id="scroll-area">
<div id="text">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.</div>
</div>
</div>
<button>Get width of #text</button>
I would like to calculate the width of the #text
element that is inside the #container
element. Currently, it shows me 300px
as result. But that is the width of the container, and not of the text element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
scrollwidth
功能Try with
scrollWidth
featurestart
start