仅使用HTML/CSS在鼠标悬停时更改Google Material图标

10

我想在按钮悬停时更改Google Material Icon(实际图标本身)。 我想仅使用HTMLCSS来完成此操作。 我希望它从'plus'图标变为类似于'check'图标,具体而言是使用'done'图标。 Codepen如下。 谢谢。

.material-icons.md1 {
    font-family: 'Material Icons';
    font-weight: normal;
    font-style: normal;
    font-size: 33px;
    margin-top: 12px;
}
.btnwrap {
    position: fixed;
    z-index: 2;
    height: 60px;
    width: 300px;
    background-color: #074fb2;
    font-size: 20px;
    display: block;
    text-align: center;
    vertical-align: middle;
    line-height: 60px;
    color: #fff;
    border-radius: 50px;
    cursor: pointer;
}
.btntext1 {
    position: absolute;
    left:85px; 
    transition: all .1s;
}
.btntext2 {
    position: absolute;
    width: 100%;
    transition: all .2s;
    opacity: 0;
}
.innerbtn {
    z-index: 1;
    position: relative;
    float: left;
    height: 56px;
    width: 56px;
    background-color: #3e81dc;
    border-radius: 50px;
    display: inline-block;
    margin-top: 2px;
    margin-left:2px; 
    transition: all 1s;
}
.btnwrap:hover .btntext1 {
    opacity: 0;
    transition: all .5s;
}
.btnwrap:hover .btntext2 {
    opacity: 1;
    transition: all .5s;
}
.btnwrap:hover .innerbtn {
    margin-left: 242px;
}
<link href="https://fonts.googleapis.com/icon?family=Material+Icons"
  rel="stylesheet">
<div id="button1" class="btnwrap" title="download_btn">
<div class="innerbtn"><i class="material-icons md1">add</i></div> 
<div class="btntext2">Click Now</div>
<div class="btntext1">Download for FREE</div>
</div>

https://codepen.io/anon/pen/YYEXYy


Material Icons的工作方式基于您在<i class="material-icons md1"></i>之间传递的内容。CSS无法更改该值。为什么不能使用JavaScript? - Baruch
我找到了一种只使用HTML/CSS的方法。 - scott.schaffer
2个回答

15

我在这里回答自己的问题。。将HTML保留为空,使用CSS中的“:before”添加“content”,然后使用悬停状态更改那个“:before”..请参见派生的Codepen:

<i class="material-icons md1" style="margin-top: 12px;"></i></div> 

.material-icons.md1::before{
    font-family: 'Material Icons';
    font-weight: normal;
    font-style: normal;
    font-size: 33px;
    content:"add"; 
}

.btnwrap:hover .material-icons.md1::before{
    content:"done"; 
}

https://codepen.io/anon/pen/aEVOEr


3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接