html - 如何让 child 向左过渡而不缩放?

标签 html css

我试图让子图像向左滑动并像父图像一样消失。如果您现在运行截图,图像会缩放而不是向左滑动。

如何防止图像缩放?并让图像向左滑动并像父级一样消失?

转换将由媒体查询触发。

这是我的代码。

.parent {
  height: 130px;
  width: 180px;
  background-color: #fff;
  border-bottom-right-radius: 10px;
  border: 1px solid #000000;
  text-align: center;
  float: left;
  position: fixed;
  z-index: 1;
  transition: ease all 0.3s;
}

.child {
  width: 100%;
  height: 100%;
  background-size: contain;
  text-indent: -9999px;
  display: block;
  background-image: url(https://static.pexels.com/photos/34490/keyboard-computer-keys-white.jpg);
  background-repeat: no-repeat;
  background-position: center center;
}

@media screen and (max-width: 655px){
  .parent {
    visibility: hidden;
    width: 1px;
  }
}
<div class="parent">
  <span class="child">
    </span>
</div>

最佳答案

我建议您的最简单的解决方案是为 .child 元素设置 visibility:visible;,这可行,但某些浏览器不支持。这是唯一使 child elementhiding parent 元素上 visible 的元素。

The visibility property can be used to hide an element while leaving the space where it would have been. It can also hide rows or columns of a table.

解决方案 - 1 检查这个jsfiddle

.parent {
  height: 130px;
  width: 180px;
  background-color: #fff;
  border-bottom-right-radius: 10px;
  border: 1px solid #000000;
  text-align: center;
  float: left;
  position: fixed;
  z-index: 1;
  transition: ease all 0.3s;
}

.child {
  width: 100%;
  height: 100%;
  background-size: contain;
  text-indent: -9999px;
  display: block;
  background-image: url(https://static.pexels.com/photos/34490/keyboard-computer-keys-white.jpg);
  background-repeat: no-repeat;
  background-position: center center;
}

@media screen and (max-width: 655px){
  .parent {
    visibility: hidden;
    width: 1px;
  }
  .child{
    width:180px;
    height:130px;
    visibility:visible;
    background-size:100% 100%;
  }
}
<div class="parent">
<span class="child">
</span>
</div>

解决方案 - 2
检查这个jsFiddle

创建两个不同的元素并使用opacity隐藏到div下面。

#bx{
  width:200px;
  height:130px;
  background:#111;
  transition: ease all 0.3s;
}
#b{
  width:200px;
  height:120px;
  top:13px;
  left:8px;
  position:absolute;
  background-image: url(https://static.pexels.com/photos/34490/keyboard-computer-keys-white.jpg);
  background-repeat: no-repeat;
  background-position: center center;
  background-size:100%;
  margin-left:0;
  transition: ease all 0.3s;
}
@media screen and (max-width: 655px){
  #bx{
    width:1px;
    opacity:0;
  }
  #b{
        margin-left:-220px;
  }
}
<div id="bx"></div>
<div id="b"></div>

关于html - 如何让 child 向左过渡而不缩放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42621221/

相关文章:

jquery - 响应式导航栏切换无法正常工作

html - CSS:如何使图像适合底部被剪裁但顶部弹出的圆圈?

html - 调整下拉菜单大小以匹配链接宽度

html - 旋转中间 flex 列效果

javascript - 如何通过不同的点击同时更改多个 CSS?

javascript - <option> 元素没有 append 在他的 <select> parent 中,而是在下面

javascript - Mpeg Dash html5 播放器或直播视频

html - 在提供 base64 编码的 PDF 时自定义 iframe 的标题

css - Windows 高对比度模式 : How are colours computed? 如何为焦点元素指定颜色?

css - Z 索引不起作用