我希望我的导航栏品牌使用 Bootstrap 4 在点击时旋转

我正在通过使用 Bootstrap 4 制作一个 Twitter 新界面来测试我的技能,但我对徽标有疑问,我希望 Twitter 徽标在单击时旋转。我怎么能做到这一点?


这是我所做的预览


https://gyazo.com/bf3626eef72bed0556f3c638c64351d0


HTML


  <a class="navbar-brand" href="#">  <img src="img/twitter-logo.png" width="auto" height="30" class="mx-auto d-inline-block align-top" alt=""></a>

  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">

    <span class="navbar-toggler-icon"></span>

  </button>

  <div class="collapse navbar-collapse" id="navbarNav">

    <ul class="navbar-nav ml-auto">

      <li class="nav-item">

        <a class="nav-link" href="#first">Home <span class="sr-only">(current)</span></a>

      </li>

      <li class="nav-item">

        <a class="nav-link" href="#second">Who are we?</a>

      </li>

      <li class="nav-item">

        <a class="nav-link" href="#third">Functions</a>

      </li>

    </ul>

  </div>

</nav>

CSS 即使我认为拥有它并不重要


.navbar-brand {

    position: absolute;

    left: 50%;

    margin-left: -50px !important;  /* 50% of your logo width */

    display: block;

}


.navbar-brand > img {

    transform:scale(1);

    transition:transform .2s ease;

}


.navbar-brand > img:hover {

    transform:scale(1.2);

}


.navbar{

  position: fixed !important;

  width:100%;

  transition: background-color 1s;

  box-shadow:0 0 30px rgba(0,0,0,0.03);

  color:#000;

}


.navbar-nav li > a {

    color:rgba(0,0,0,0.22);

    font-weight:400;

}


.navbar-nav li > a:hover {

    color:rgba(0,0,0,1);

}


.navbar-nav > li{

  padding-left:5px;

  padding-right:5px;

}


四季花海
浏览 117回答 1
1回答

慕妹3146593

添加这种风格.navbar-brand.spining{&nbsp; animation-name: spin;&nbsp; animation-duration: 1000ms;&nbsp; animation-timing-function: ease;&nbsp;}@keyframes spin {&nbsp; &nbsp; from {&nbsp; &nbsp; &nbsp; &nbsp; transform:rotate(0deg);&nbsp; &nbsp; }&nbsp; &nbsp; to {&nbsp; &nbsp; &nbsp; &nbsp; transform:rotate(360deg);&nbsp; &nbsp; }}spining并通过切换类<a class="navbar-brand" href="#" onclick="this.classList.toggle('spining')">...</a>&nbsp;.navbar-brand {&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; left: 50%;&nbsp; &nbsp; margin-left: -50px !important;&nbsp; /* 50% of your logo width */&nbsp; &nbsp; display: block;}.navbar-brand > img {&nbsp; &nbsp; transform:scale(1);&nbsp; &nbsp; transition:transform .2s ease;}.navbar-brand > img:hover {&nbsp; &nbsp; transform:scale(1.2);}.navbar{&nbsp; position: fixed !important;&nbsp; width:100%;&nbsp; transition: background-color 1s;&nbsp; box-shadow:0 0 30px rgba(0,0,0,0.03);&nbsp; color:#000;}.navbar-nav li > a {&nbsp; &nbsp; color:rgba(0,0,0,0.22);&nbsp; &nbsp; font-weight:400;}.navbar-nav li > a:hover {&nbsp; &nbsp; color:rgba(0,0,0,1);}.navbar-nav > li{&nbsp; padding-left:5px;&nbsp; padding-right:5px;}.navbar-brand.spining{&nbsp; animation-name: spin;&nbsp; animation-duration: 1000ms;&nbsp; animation-timing-function: ease;&nbsp;}@keyframes spin {&nbsp; &nbsp; from {&nbsp; &nbsp; &nbsp; &nbsp; transform:rotate(0deg);&nbsp; &nbsp; }&nbsp; &nbsp; to {&nbsp; &nbsp; &nbsp; &nbsp; transform:rotate(360deg);&nbsp; &nbsp; }}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"><script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script><script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script><nav class="navbar navbar-expand-lg navbar-light"><a class="navbar-brand" href="#" onclick="this.classList.toggle('spining')">&nbsp; <img src="https://i.stack.imgur.com/plLel.png" width="auto" height="30" class="mx-auto d-inline-block align-top" alt=""></a>&nbsp; <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">&nbsp; &nbsp; <span class="navbar-toggler-icon"></span>&nbsp; </button>&nbsp; <div class="collapse navbar-collapse" id="navbarNav">&nbsp; &nbsp; <ul class="navbar-nav ml-auto">&nbsp; &nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="#first">Home <span class="sr-only">(current)</span></a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="#second">Who are we?</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; <li class="nav-item">&nbsp; &nbsp; &nbsp; &nbsp; <a class="nav-link" href="#third">Functions</a>&nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; </ul>&nbsp; </div></nav>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript