如何获取“overflow-x: auto;”内元素的全宽区域?

发布于 2025-01-17 19:46:00 字数 1169 浏览 5 评论 0原文

我对此进行了编码:

$(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过期情话 2025-01-24 19:46:00

尝试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>

Try with scrollWidth feature

$(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>

一指流沙 2025-01-24 19:46:00
$(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>

start

$(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>

start

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文