javascript - 使用窗口滚动条来控制溢出的 div 的滚动 - Javascript

标签 javascript jquery css

我有一个带有 overflow-x:auto 的 div,其中包含一个对于 div 而言太宽的元素,因此它会滚动。问题是元素太高,使用元素的滚动条查看其内容相当麻烦。

更好的方法是使用窗口滚动条来控制元素的滚动,因为它们的位置固定在窗口的边缘。

这可能吗?我似乎想不出这样做的好方法。

下面的问题示例。请注意,此包装器元素必须能够在现有页面布局的流程中工作。

https://jsfiddle.net/3y1549jk/2/

.wrapper {
  position: absolute;
  width: 75%;
  left: 12.5%;
  right: 12.5%;
  height: 200%;
  top: 12.5%;
  bottom: 12.5%;
  background-color: red;
  overflow-x: auto;
}

.content {
  position: absolute;
  width: 150%;
  height: 75%;
  margin: 7.5%;
  background-color: blue;
  color: white;
  padding: 7.5%;
}
<!-- I would like the window to control the horizontal scrolling of the .wrapper div -->
<div class="wrapper">
  <div class="content">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at libero imperdiet justo finibus vehicula. Integer ac risus quis lectus pretium condimentum in sit amet mauris. Maecenas et sagittis justo. Cras diam urna, placerat aliquet metus non, interdum cursus nisl. Quisque cursus elit feugiat, tempus diam in, faucibus felis.
  </div>
</div>

最佳答案

使用位置 fixed 而不是 absolute..

.wrapper {
  position: absolute;
  width: 75%;
  left: 12.5%;
  right: 12.5%;
  height: 200%;
  top: 12.5%;
  bottom: 12.5%;
  background-color: red;
  overflow-x: auto;
}

.content {
  position: fixed;
  width: 150%;
  height: 75%;
  margin: 7.5%;
  background-color: blue;
  color: white;
  padding: 7.5%;
}
<!-- I would like the window to control the horizontal scrolling of the .wrapper div -->
<div class="wrapper">
  <div class="content">
  Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse at libero imperdiet justo finibus vehicula. Integer ac risus quis lectus pretium condimentum in sit amet mauris. Maecenas et sagittis justo. Cras diam urna, placerat aliquet metus non, interdum cursus nisl. Quisque cursus elit feugiat, tempus diam in, faucibus felis.
  </div>
</div>

关于javascript - 使用窗口滚动条来控制溢出的 div 的滚动 - Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38494660/

相关文章:

javascript - 同时完成子进程和 Promise 决议

php - 使用 JQuery Serialize 时如何为空白字段插入 NULL 值?

javascript - 在 Chrome 扩展中运行 JQuery

javascript - 这个jquery如何在meteor助手中实现?

javascript - WordPress 主题中 slider 的自定义帖子在浏览器中首次加载后不起作用

javascript - 如何在不点击popup的情况下将数据从popup.html发送到content_script

javascript - 使用纯 JavaScript 交换图像 src

CSS位置全屏

html - 带有旋转文本的垂直对齐 div(无 CSS3)

html - 填充给定 div 的高度