css - SVG 背景图像悬停时奇怪的过渡行为

标签 css svg css-transitions

我有一个带有 svg 作为背景的按钮。 :hover 选择器工作正常,但是当我给它一个过渡时,按钮首先变小,然后跳回到原来的大小。我该如何阻止这种行为?

这是我的代码:

a {
  width: 250px;
  display: block;
  color: rgba(0, 0, 0, .9);
  text-decoration: none
}
.prod-ueber .cta-wrapper {
  position: absolute;
  left: 310px;
  width: 100px;
  top: 45px;
  z-index: 5
}
.info-btn,
.korb-btn {
  width: 50px;
  height: 50px;
  background-repeat: no-repeat;
  background-size: 100%;
  float: left;
  transition: background .4s ease;
}
.info-btn {
  background-image: url(http://imgh.us/info-btn.svg);
}
.korb-btn {
  background-image: url(http://imgh.us/korb-btn.svg);
  margin-left: 12px;
}
.info-btn:hover,
.info-btn:active,
.info-btn:focus {
  background-image: url(http://imgh.us/info-btn_h.svg);
}
.korb-btn:hover,
.korb-btn:active,
.korb-btn:focus {
  background-image: url(http://imgh.us/korb-btn_h.svg);
}
<a href="#">
  <div class="prod-ueber">
    <img src="http://dummyimage.com/" height="290" width="426" alt="Minze" class="img-responsive">
    <h3 class="Artikelname">Malve</h3>
    <small>Description</small>
    <hr>
    <h5 class="preis">€ 1,79</h5>
    <div class="cta-wrapper">
      <a href="#" class="info-btn"></a>
      <a href="#" class="korb-btn"></a>
    </div>
  </div>
  <!-- produkt -->
</a>

最佳答案

您可以通过为 pseudo 元素添加 background-image 来使用解决方法

a {
  width: 250px;
  display: block;
  color: rgba(0, 0, 0, .9);
  text-decoration: none
}
.prod-ueber .cta-wrapper {
  position: absolute;
  left: 310px;
  width: 100px;
  top: 45px;
  z-index: 5
}
.info-btn,
.korb-btn {
  width: 50px;
  height: 50px;
  background-repeat: no-repeat;
  background-size: 100%;
  float: left;
  position: relative;
  overflow:hidden;
}
.info-btn {
  background-image: url(http://imgh.us/info-btn.svg);
}
.korb-btn {
  background-image: url(http://imgh.us/korb-btn.svg);
  margin-left: 12px;
}
.info-btn:before,
.korb-btn:before {
  content: '';
  width: 100%;
  height: 100%;
  position: absolute;
  background-size: 100% auto;
  opacity: 0;
  transition: .4s ease;
}
.info-btn:before {
  background-image: url(http://imgh.us/info-btn_h.svg);
}
.korb-btn:before {
  background-image: url(http://imgh.us/korb-btn_h.svg);
}
.info-btn:hover:before,
.info-btn:active:before,
.info-btn:focus:before {
  opacity: 1;
}
.korb-btn:hover:before,
.korb-btn:active:before,
.korb-btn:focus:before {
  opacity: 1;
}
<a href="#">
  <div class="prod-ueber">
    <img src="http://dummyimage.com/" height="290" width="426" alt="Minze" class="img-responsive">
    <h3 class="Artikelname">Malve</h3>
    <small>Description</small>
    <hr>
    <h5 class="preis">€ 1,79</h5>
    <div class="cta-wrapper">
      <a href="#" class="info-btn"></a>
      <a href="#" class="korb-btn"></a>
    </div>
  </div>
</a>

关于css - SVG 背景图像悬停时奇怪的过渡行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29644539/

相关文章:

html - 在 Twitter Bootstrap 中更改 .table-bordered 的线条颜色

javascript - SVG 片段被错误地解析为 HTMLUnknownElement

html - 如何调整 html 中 img 标签中使用的 svg 的大小?

jquery - jQuery .css() 内的 CSS3 转换

javascript - CSS 过渡百分比(用于进度条)

html - Bootstrap 封面模板 : where to place image?

css - 自定义复选框在 Windows 上的 Chrome 中不起作用

html - Firefox 在 div 中向右呈现表格,但在 IE 和 Chrome 中看起来正常

css - 带有渐变叠加和背景图像的 SVG 圆 Angular 三 Angular 形

jquery - 动态 DOM 元素上的 webKit 转换