【问题标题】:change Google Material Icon on hover with only html/css仅使用 html/css 更改悬停时的 Google Material Icon
【发布时间】:2017-11-11 18:22:10
【问题描述】:

我想在按钮悬停时更改Google Material Icon(实际图标本身)。我只想用HTMLCSS 来做这件事。我希望它从“加号”图标变为“检查”图标,特别是使用“完成”图标。下面的代码。谢谢

.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

【问题讨论】:

  • 材质图标的工作方式取决于您在&lt;i class="material-icons md1"&gt;&lt;/i&gt; 之间传递的内容。 CSS 无法更改该值。为什么你不能使用 JavaScript?
  • 我找到了一个仅 HTML/CSS 的路由。

标签: html css google-material-icons


【解决方案1】:

所以我在这里回答我自己的问题.. 将 html 留空,在 CSS 中添加 'content' 和 ':before',然后使用悬停状态更改 ':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

【讨论】:

    【解决方案2】:

    HTML

    <i class="material-icons md1"></i>
    

    CSS

    .material-icons.md1::before{
        content:"add"; 
    }
    
    .material-icons.md1:hover::before{
        content:"remove"; 
    }
    

    https://codepen.io/Funny0Frank/pen/RMYBvQ

    【讨论】:

      猜你喜欢
      • 2018-09-12
      • 2013-09-19
      • 1970-01-01
      • 2022-11-26
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 1970-01-01
      • 2018-03-03
      相关资源
      最近更新 更多