【问题标题】:Changing Icon Font in CSS for active state将 CSS 中的图标字体更改为活动状态
【发布时间】:2013-04-25 01:58:28
【问题描述】:

我目前有一个响应式切换导航菜单,效果很好。我遇到的问题是导航图标。我正在尝试将图标字体用于打开和关闭状态。目前我的 HTML 是:

<a href="#menu" class="menu-link" aria-hidden="true" data-icon="&#xe008;"></a>
                <nav id="menu" role="navigation">
                    <ul>
                        <li><a href="index.html">Home</a></li>
                        <li><a href="index.html#work">work</a></li>
                        <li><a href="index.html#profile">profile</a></li>
                        <li><a href="index.html#life">life</a></li>
                    </ul>
                </nav>

显示的图标是打开的(三行)图标。我无法确定谁可以使用另一种图标字体将其替换为关闭状态?

这是我当前的链接 CSS:

a.menu-link { float: right; display: block; font-size: 1.75em; margin-right: .75em; margin-top: 1em; }

&.active {?}


@font-face {
font-weight: normal;
font-style: normal;
font-family: 'icomoon';
src:url('../fonts/icomoon.eot');
src:url('../fonts/icomoon.eot?#iefix') format('embedded-opentype'),
    url('../fonts/icomoon.woff') format('woff'),
    url('../fonts/icomoon.ttf') format('truetype'),
    url('../fonts/icomoon.svg#icomoon') format('svg');

}

[data-icon]:before {
content: attr(data-icon);
text-transform: none;
font-weight: normal;
font-variant: normal;
font-family: 'icomoon';
line-height: 1;
speak: none;
-webkit-font-smoothing: antialiased;

}

我认为我应该使用 if 语句?但是有没有通过标记或 CSS 的解决方案?如果没有,那么 JS 解决方案会很有帮助。伙计们干杯!

【问题讨论】:

  • 能否展示将data-icon属性转换为icon字体中字形的JS或CSS?
  • 刚刚添加到原帖中。

标签: css navigation responsive-design icon-fonts


【解决方案1】:

看来更新显示的图标只需要更改锚元素的data-icon 属性即可。这是一种方法:

document.querySelector('.menu-link').setAttribute('data-icon', 'GLYPH_CODE_HERE');

【讨论】:

  • 但这不只是更改默认显示的图标吗?有没有办法只在链接处于活动状态时更改图标?
  • 只在鼠标按下锚点时更改图标就足够了吗?这样,您可以在按下鼠标时更改图标,然后在释放鼠标时将其更改回来。另一个想法是将选择器更新为.menu-link:active
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-03
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 2017-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多