在悬停时更改CSS背景图像SVG绘制行为? [英] change css background-image svg paint behavior on hover?

查看:43
本文介绍了在悬停时更改CSS背景图像SVG绘制行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用svg作为按钮的背景图片.

I am using an svg as a background image for a button.

按钮需要不同的悬停状态,这些状态只是对svg填充值的更改.

The button needs different hover states, which are just changes to the svg fill values.

是否有一种巧妙的方法可以对svg进行这些更改,而这些更改实际上只是用作图像,还是我需要创建一个具有不同填充颜色的其他svg文件?

Is there a clever way to make these changes to the svg which is effectively just being used as an image, or do I need to create a different svg file with a different fill color?

推荐答案

另一种具有背景位置的方法.

An another method with background-position.

HTML

<div class="locate">
    <a href="#">lorem ipsum</a>
</div>

CSS

 .locate a {
      padding-left: 20px;
      position: relative;
      text-decoration: underline;
      color: #000;
    }
    .locate a:before {
      width: 16px;
      height: 16px;
      content: "";
      background: transparent url('search.svg') no-repeat scroll 0 0;
      -webkit-background-size: 16px 32px;
      background-size: 16px 32px;
      position: absolute;
      top: 0;
      left: 0;
      /* For screen reader */
      speak: none;
    }
    .locate a:hover,
    .locate a:focus {
      color: #a2005a;
      text-decoration: none;
    }
    .locate a:hover:before,
    .locate a:focus:before {
      background-position: 0 -16px;
    }

SVG

<svg xmlns="http://www.w3.org/2000/svg">
<path d="M15.838,13.693l-2.785-2.786c0.705-1.103,1.121-2.408,1.121-3.813c0-3.911-3.17-7.081-7.081-7.081
    c-3.911,0-7.081,3.17-7.081,7.081c0,3.91,3.17,7.08,7.081,7.08c1.406,0,2.712-0.415,3.813-1.121l2.787,2.786
    c0.195,0.197,0.518,0.197,0.715,0l1.43-1.43C16.037,14.211,16.037,13.891,15.838,13.693z M7.093,12.15
    c-2.789,0-5.058-2.27-5.058-5.058c0-2.789,2.269-5.058,5.058-5.058c2.79,0,5.058,2.269,5.058,5.058
    C12.15,9.882,9.883,12.15,7.093,12.15z"/>
<path fill="#A2005A" d="M15.838,29.678l-2.785-2.786c0.705-1.103,1.121-2.408,1.121-3.813c0-3.91-3.17-7.08-7.081-7.08
    c-3.911,0-7.081,3.17-7.081,7.08s3.17,7.08,7.081,7.08c1.406,0,2.712-0.415,3.813-1.121l2.787,2.786
    c0.195,0.197,0.518,0.197,0.715,0l1.43-1.43C16.037,30.195,16.037,29.875,15.838,29.678z M7.093,28.135
    c-2.789,0-5.058-2.27-5.058-5.058s2.269-5.058,5.058-5.058c2.79,0,5.058,2.27,5.058,5.058C12.15,25.866,9.883,28.135,7.093,28.135z"
    />
</svg>

这篇关于在悬停时更改CSS背景图像SVG绘制行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆