【问题标题】:background-image (SVG) change on :hover acting funny背景图像(SVG)更改:悬停表现有趣
【发布时间】:2015-11-30 18:49:00
【问题描述】:

当我将鼠标悬停在 div 上时,background-image 应该会更改为另一个。然而,它似乎从一个小图像开始,然后放大到请求的实际大小......

这可能是什么原因造成的?

JSFiddle

.animate { -webkit-transition: all 0.2s ease-in-out; -moz-transition: all 0.2s ease-in-out; -o-transition: all 0.2s ease-in-out; transition: all 0.2s ease-in-out; }

div.rating_system {
	position: relative; float: left; clear: none; display: block;
	width: 260px; height: 50px;
}

div.rating_system input[type=radio] { display: none; visibility: collapse; }

div.rating_system input[type=radio] + label {
	position: relative; float: right; clear: none; display: block;
	width: 18.4%; height: 100%; margin: 0; padding: 0 2% 0 0;
	outline: 0; cursor: pointer;
	
	background-image: url('data:image/svg+xml;utf8,<svg version="1.1" fill="#2C3E50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 110 105" style="enable-background:new 0 0 110 105;" xml:space="preserve"><ellipse opacity="0.3" cx="55" cy="52.5" rx="55" ry="52.5"/><path d="M55,8c25.9,0,47,20,47,44.5S80.9,97,55,97S8,77,8,52.5S29.1,8,55,8 M55,0C24.6,0,0,23.5,0,52.5c0,29,24.6,52.5,55,52.5s55-23.5,55-52.5C110,23.5,85.4,0,55,0L55,0z"/></svg>');
	background-repeat: no-repeat;
	background-position: center center;
	background-size: 40px;
}
div.rating_system input[type=radio]#star-5 + label { padding: 0 0 0 0; }

div.rating_system:hover > input[type=radio] + label:hover,
div.rating_system:hover > input[type=radio] + label:hover ~ label,
div.rating_system > input[type=radio]:checked ~ label {
    background-image: url('data:image/svg+xml;utf8,<svg version="1.1" fill="#2C3E50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 110 105" style="enable-background:new 0 0 110 105;" xml:space="preserve"><ellipse cx="55" cy="52.5" rx="55" ry="52.5"/></svg>');
}
<div class="rating_system">
			<input type="radio" id="star-5" name="rating-01" value="5" class="animate"><label for="star-5" class="animate"></label>
			<input type="radio" id="star-4" name="rating-01" value="4" class="animate"><label for="star-4" class="animate"></label>
			<input type="radio" id="star-3" name="rating-01" value="3" class="animate"><label for="star-3" class="animate"></label>
			<input type="radio" id="star-2" name="rating-01" value="2" class="animate"><label for="star-2" class="animate"></label>
			<input type="radio" id="star-1" name="rating-01" value="1" class="animate"><label for="star-1" class="animate"></label>
</div>

【问题讨论】:

  • @MrLister 尝试将鼠标悬停在圆圈上,单击一些然后再次悬停...
  • @Borsn 它在 FF 中完全失败。在 chrome 中你可以看到一些东西。
  • @timo 可能是因为 svg 是内联使用的……如果我链接文件,它会修复它。但是我尝试通过链接文件,仍然与悬停相同。
  • 啊,Mozilla 对 URI 中的 # 感到窒息。更改为 %23 即可运行。 Updated Fiddle。虽然它没有问题中描述的问题,所以我认为您应该将 "using Chrome" 添加到问题中。
  • @MrLister 做到了,它现在可以在 Firefox 中使用...这与我认为的高度有关

标签: html css svg css-transitions


【解决方案1】:

它看起来像一个浏览器错误。我可以通过opacity 为您提供基于:after pseudo-elementtransition 的跨浏览器解决方案:

JSFiddle(简化了您的 sn-p 代码)

.rating-system {
    float: left;
    display: block;
    width: 260px;
    height: 50px;
}

input {
    display: none;
}

label {
    position: relative;
    float: right;
    display: block;
    width: 18.4%;
    height: 100%;
    margin: 0;
    padding: 0 2% 0 0;
    outline: 0;
    cursor: pointer;

    background-image: url('data:image/svg+xml;utf8,<svg version="1.1" fill="#2C3E50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="110" height="105"><ellipse opacity="0.3" cx="55" cy="52.5" rx="55" ry="52.5"/><path d="M55,8c25.9,0,47,20,47,44.5S80.9,97,55,97S8,77,8,52.5S29.1,8,55,8 M55,0C24.6,0,0,23.5,0,52.5c0,29,24.6,52.5,55,52.5s55-23.5,55-52.5C110,23.5,85.4,0,55,0L55,0z"/></svg>');
    background-repeat: no-repeat;
    background-position: 50% 50%;
    background-size: 40px;
}

label:first-of-type {
    padding: 0;
}

label:after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    
    background-image: url('data:image/svg+xml;utf8,<svg version="1.1" fill="#2C3E50" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="110" height="105"><ellipse cx="55" cy="52.5" rx="55" ry="52.5"/></svg>');
    background-repeat: no-repeat;
    background-position: 50% 50%;
    background-size: 40px;
    
    
    opacity: 0;
    -webkit-transition: opacity 0.2s ease-in-out;
    -moz-transition: opacity 0.2s ease-in-out;
    -o-transition: opacity 0.2s ease-in-out;
    transition: opacity 0.2s ease-in-out;
}

label:hover:after,
label:hover ~ label:after,
input:checked ~ label:after {
    opacity: 1;
}
<div class="rating-system">
    <input type="radio" id="star-5" name="rating-01" value="5"/>
    <label for="star-5"></label>
    <input type="radio" id="star-4" name="rating-01" value="4"/>
    <label for="star-4"></label>
    <input type="radio" id="star-3" name="rating-01" value="3"/>
    <label for="star-3"></label>
    <input type="radio" id="star-2" name="rating-01" value="2"/>
    <label for="star-2"></label>
    <input type="radio" id="star-1" name="rating-01" value="1"/>
    <label for="star-1"></label>
</div>

【讨论】:

  • 从来没有想过要那样做……但它在 Firefox 中不起作用。它向后兼容吗?
  • @Borsn 我为我的公司做了类似的事情,但我使用了来自文件的 SVG(例如,background: url(image.svg)),它适用于包括 IE8 在内的所有浏览器(在 PNG 上有后备)。我认为 Firefox 不支持在 background 属性中使用 SVG 内联。像文件或 base64 一样使用 SVG 有何意义?
  • 我只是使用了上面的建议并将#替换为%23并修复了它。谢谢现在真的很好用! :)
  • 很好的解决方案。我需要对其进行一些编辑。但这很容易。只需使用伪类after。 Aber 使用伪类hover:after 来切换不透明度。太棒了!
  • @kwoxer 很高兴为您提供帮助!
猜你喜欢
  • 2020-07-24
  • 1970-01-01
  • 2018-10-31
  • 1970-01-01
  • 2019-01-21
  • 1970-01-01
  • 2015-12-01
  • 2013-11-02
  • 2013-09-22
相关资源
最近更新 更多