如何获得 "overflow-x: auto;" 区域内元素的全宽?
How to get full width of an element inside an "overflow-x: auto;" area?
我这样编码:
$(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>
我想计算 #container
元素内的 #text
元素的宽度。目前,它显示 300px
作为结果。但那是容器的宽度,而不是文本元素的宽度。
尝试使用 scrollWidth
功能
$(document).ready(function() {
$("button").click(function() {
alert("Width of #text: " + $("#scroll-area")[0].scrollWidth);
});
});
* {
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>
我这样编码:
$(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>
我想计算 #container
元素内的 #text
元素的宽度。目前,它显示 300px
作为结果。但那是容器的宽度,而不是文本元素的宽度。
尝试使用 scrollWidth
功能
$(document).ready(function() {
$("button").click(function() {
alert("Width of #text: " + $("#scroll-area")[0].scrollWidth);
});
});
* {
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>