javascript - 剪辑 div 也剪辑滚动

标签 javascript html css

我有两个 div,子 div 在父 div 中。 div child 比他的 parent 大。所以我决定在 div 父级中放置一个滚动条,因为我可以更好地看到 div 子级的内容。

问题是现在我需要在父div中使用属性clip,但是clip也会影响滚动。

我想问的是有没有什么方法可以将父 div 和滚动大小自动调整到滚动上。

遵循我的代码:

.outter{
    width:100px;
    height:100px;
    background-color:blue;
    overflow: scroll;
    position: absolute;
    clip: rect(23.75px 120px 120px 23.75px);
}

.inner{
    width:200px;
    height:200px;
    background-color:red;
    
}
<div class="outter">
  <div class="inner"></div>
</div>

[编辑]:

跟随你我假装的结果。

enter image description here

如果你比较上面的图片,我在上面放的片段的结果是卷轴被剪掉了,而图像没有

最佳答案

这就是 clip 属性应该做的,限制元素的可见区域。如果你试图剪辑内部元素的内容,你完全可以定位它来这样做。

希望这对您有所帮助!

.outter {
    position: relative; 
    width:160px;
    height:160px;
    background-color:red;
    overflow: scroll;
}

.inner{
    position: absolute;
    left: -40px;
    top: -40px;
    background:url('http://placekitten.com/g/400/400');
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center;
    width:400px;
    height:400px;
}
<div class="outter">
  <div class="inner">
  </div>
</div>

关于javascript - 剪辑 div 也剪辑滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40892960/

相关文章:

javascript - 返回按钮在同一页面上创建多个组件

java - Spring Boot + Angular 5 - http.post 空值

css - 在 Prop 上更新样式组件的样式变化缓慢?

javascript - Angular then() 内的多个函数

javascript - 根据持久设置修改 Chrome 扩展中的内容 css

javascript - Angular JS 错误 : [$injector:unpr] Unknown provider

javascript - 通过 JavaScript 将元素固定到另一个元素的底部

Jquery:我如何找到这样的元素 <p>MyHeader</p>

javascript - 如何切换多个隐藏字段以通过 CSS 和 Javascript 显示?

html - CSS 动画加载器在 IE 11 中只能正常工作一次