简体   繁体   English

如何为<a>文本</a>设置动画<a>而不是<span>使用 CSS</span></a>

[英]How to animate the <a> text and not the <span> with CSS

I have made a menu navigation with Bootstrap and I have added a simple transition to hyperlinks inside the menu... See it here .我使用 Bootstrap 进行了菜单导航,并添加了一个简单的过渡到菜单内的超链接......在这里查看

As you can see when you hover over the right links of the menu, they will go left because I have set the CSS like this:正如您所看到的,当您将鼠标悬停在菜单的右侧链接上时,它们会向左移动,因为我已将 CSS 设置为如下所示:

    .rightlinks a:hover{
    padding-right:20px;
}

.rightlinks a, .leftlinks a{
    font-size:15px;
    color:#000 !important;
}

.rightlinks a{
    transition:padding-right 0.45s linear;
}

But the problem is that I want to animate only the text and not the icon!但问题是我只想动画文本而不是图标! Look at my HTML code to understand better:查看我的 HTML 代码以更好地理解:

<nav id="nav_wrapper" class="navbar navbar-default navbar-fixed-top">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header">
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>

            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li class="BKoodakBold leftlinks"><a href="#"><span class="glyphicon glyphicon-user"></span></a></li>
                    <li class="BKoodakBold leftlinks"><a href="#"><span class="glyphicon glyphicon-search"></span></a></li>
                </ul>
                <ul class="nav navbar-nav navbar-center">
                    <a class="navbar-brand" href="#"><img src="assets/img/logo.png" width="50"></a>
                </ul>
                <ul class="nav navbar-nav navbar-right">
                    <li class="BKoodakBold rightlinks"><a href="#">درباره ما &nbsp; <span class="glyphicon glyphicon-info-sign"></span></a></li>
                    <li class="BKoodakBold rightlinks"><a href="#" >میراث گویانت &nbsp; <span class="glyphicon glyphicon-gift"></span></a></li>
                    <li class="BKoodakBold rightlinks"><a href="#">صفحه اصلی &nbsp; <span class="glyphicon glyphicon-globe"></span></a></li>
                </ul>
            </div>
        </div>
    </nav>

As you can see each link contains an span tag which is an icon.正如您所看到的,每个链接都包含一个 span 标记,它是一个图标。 And because of my custom CSS, it has affected the padding-right:20px;而且由于我的自定义 CSS,它影响了padding-right:20px; but I dont want the span tag to be affected by this transition.但我不希望 span 标签受到这种转换的影响。 Only the menu link or text should be animated...只有菜单链接或文本应该是动画...

So if you know the answer to this question, please let me know!所以如果你知道这个问题的答案,请告诉我! Thanks :)谢谢:)

add <p></p> to the text field you want to move.<p></p>到要移动的文本字段。 Then use css like this to animate your icon.然后使用像这样的 css 来为您的图标设置动画。

.rightlinks a p:hover{
    padding-right:20px;
}

.rightlinks a p, .leftlinks a p{
    font-size:15px;
    color:#000 !important;
}

.rightlinks a p{
    transition:padding-right 0.45s linear;
}

Also fixed the position of text corresponding to your icon还固定了与您的图标对应的文本位置

I took the glyphicon spans out and put them in individual <li>我取出了字形跨度并将它们放在单独的<li>

I got it working.我让它工作了。 Keep in mind that the neighboring icons will still be effected.请记住,相邻的图标仍将受到影响。

Codepen代码笔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM