html - CSS 溢出滚动同时可见

标签 html css overflow

我有带子 div 的可滚动 div。有没有办法显示整个 child (也在可滚动的 div 之外)?现在,overflow-x: scroll 就像带有滚动条的overflow-x: hidden。我想要带有滚动条的overflow-x: visible。这是 fiddle .

.container {
  margin: 0 auto;
  overflow-x: scroll;
  width: 300px;
}

.child {
  background-image: linear-gradient(to left, blue, green);
  height: 100px;
  width: 600px;
}

.containerExample {
  background-image: linear-gradient(to left, blue, green);
  margin: 0 auto;
  overflow-x: visible;
  width: 600px;
}

.childExample {
  border: 1px solid red;
  height: 100px;
  margin: 0 auto;
  width: 300px;
}
Now:

<div class="container">
<div class="child">

</div>
</div>

What I want (red border should be scrollable div):

<div class="containerExample">
<div class="childExample">

</div>
</div>

最佳答案

你可以在这里使用一些js来克隆子元素,然后使用container上的scroll事件来计算水平滚动并使用相同的值来偏移位置克隆元素的。

const container = $(".container")
const child = container.find('.child');
const clone = child.clone();
const border = 4;

clone.addClass('clone');
child.addClass('transparent');
container.css('border-width', border);
container.before(clone)

$(".container").on('scroll', function() {
  const offset = $(this).scrollLeft() - border;
  clone.css({
    left: -offset
  })
})
body {
  overflow-x: hidden;
}

.container {
  margin: 0 auto;
  overflow-x: scroll;
  width: 300px;
  border: solid red;
  z-index: 10;
}

.wrapper {
  margin: 0 auto;
  width: 300px;
  position: relative;
}

.child {
  background-image: linear-gradient(to left, blue, green);
  height: 100px;
  width: 600px;
  position: relative;
}

.clone {
  position: absolute;
  z-index: -1;
  left: 0;
  top: 0;
}

.transparent {
  background: transparent;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <div class="container">
    <div class="child"></div>
  </div>
</div>

关于html - CSS 溢出滚动同时可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58225111/

相关文章:

html - 当父级没有固定宽度时,子级的高度为 100%

javascript - 在窗口调整大小时按比例调整 div 的宽度和高度

html - 将图像缩放到窗口大小,同时保持彼此的相对位置

html - 将标题粘到相对父级并溢出

HTML/CSS 按钮无法在 IE 中正确显示

javascript - 如何使用 javascript 验证月、日和年范围?

html - -webkit-transform rotate - Chrome 中的像素化图像

javascript - jquery 文件树高亮选中

javascript - jQuery:当特定div溢出滚动100px时显示元素?

html - 将父元素居中,直到到达左上角,然后停止居中并显示滚动条?