【问题标题】:Clip child to parent element with border-radius使用边框半径将子元素剪辑到父元素
【发布时间】:2015-03-10 02:49:33
【问题描述】:

如何强制将子元素剪辑到具有圆角的父元素。

<div class="item" >
  <div class="top">
    <h1>Tile</h1>
    <h2>Click me</h2>
  </div>
  <div class="behind">
    <h3>Details</h3>
  </div>
</div>

当为子元素设置动画时,它会忽略父元素的边界半径。有没有办法修复顶部的两个角?

.item{
  text-align: center;
  cursor: pointer;
  overflow: hidden; 
  height: 280px;
  width: 280px;
  float: left;
  border-radius: 5px;
  background: white;
  margin: 10px; 
  position: absolute;
}
.top{
  z-index: 1;
  position: absolute;
  height: 280px;
  width: 280px;
  background: #ed844b;
  transition: 0.3s; 
  border-radius: 5px;
}
.behind{
  z-index: 0;
  position: absolute;
  width: 100%;
  top: 136px;
  height: 138px;
  padding: 10px 16px;
  background: #DDDDDD;   
  box-sizing: border-box;
  border-radius: 5px;
}
.slide-up{
  transform: translate3d(0, -136px, 0);   
  border-radius: 0px;
}

这是一个小演示: http://codepen.io/Koopa/pen/xbaMez

谢谢

库帕

【问题讨论】:

    标签: css overflow css-animations rounded-corners clip


    【解决方案1】:

    当您向子级添加 css 3d 变换时,您有点将其移动到单独的 GPU 层。您可以将父元素移动到 GPU 层,而不是将 null-transform hack transform: translateZ(0) 添加到 .item。或者您可以将translate 替换为translateY(在这种情况下,只有在没有动画的情况下才会剪辑孩子)。

    【讨论】:

    • 感谢您的回答。可悲的是 translateZ(0) 没有帮助。 TanslateY 正在工作,但在动画进行时问题仍然存在
    • It works for me。在最新的稳定版 Chrome 和 FF Mac/Win 中测试。
    • 这是一个bug in Safari。可以用-webkit-mask-image: -webkit-radial-gradient(circle, white, black);updated fiddle解决它
    • 哦,太好了。非常感谢!
    猜你喜欢
    • 2018-07-30
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多