第二个背景图像

时间:2018-02-19 13:25:16

标签: javascript jquery html css

我正在尝试使用动画创建侧边栏菜单,并尝试尽可能使用css。如果不可能,JS / JQuery也可以。

我的问题是,我有一个带有两个背景图像的<span></span>元素,我想为它们设置不同的动画行为。第一个应根据跨度的大小旋转并增加其大小,第二个不应旋转并保持与之前相同的大小,但应仅在动画结束后出现。

对于每个背景图像,是否可以在一个元素上具有完全不同的动画?如果是,怎么样?

到目前为止,这是我的代码:

列表是使用无序列表完成的,每个<li></li>应包含一个菜单项

<ul>
    <li>
        <a class="menu-item-link" href="#"><span class="menu-item">1</span>Active or hovered Menu</a>
    </li>
    <li><li>
</ul>

带动画关键帧的一些CSS

.menu-item-wrapper {
  width: 500px;
  clear: both;
}

.menu-item-link {
  margin-left: 5px;
  color: white;
  text-decoration: none;
}

.menu-item {
  float: left;
  color: white;
  text-align: center;
  padding-top: 5px;
  display: block;
  width: 30px;
  height: 30px;
  background-image: url("hexagon.svg"), url("mars.png");
  background-size: contain;
  background-repeat: no-repeat;
}

.menu-item:hover {
  animation-name: menuAnimation;
    animation-duration: 0.5s;
    animation-fill-mode: forwards;
    color: transparent;
}

@keyframes menuAnimation {
    from {
        width: 30px;
        height: 30px;
        transform: rotate(0deg);
    }

    to {
        width: 50px;
        height: 50px;
        transform: rotate(210deg);
    }
}

And a Fiddle

这就是动画结束后的样子(动画完成后行星图像和链接文字应该只显示)

enter image description here

2 个答案:

答案 0 :(得分:1)

想法是使用伪元素将元素拆分为两个元素,然后您可以单独和同时为它们设置动画:

body {
  background: black;
}

.menu-item-link {
  margin-left: 5px;
  color: white;
  text-decoration: none;
}

.menu-item {
  color: white;
  text-align: center;
  padding-top: 5px;
  display: block;
  width: 30px;
  height: 30px;
  background-image: url("https://openclipart.org/image/2400px/svg_to_png/247374/Mars.png");
  background-size: contain;
  background-repeat: no-repeat;
  position: relative;
}

.menu-item:before {
  content:"";
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index:1;
  background-image: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xnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+CjxnPgo8L2c+Cjwvc3ZnPgo=);
  background-size: contain;
  background-repeat: no-repeat;
}

.menu-item:hover {
  animation-name: anime1;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  color: transparent;
}
.menu-item:hover::before {
  animation-name: anime2;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
}

@keyframes anime1 {
  from {
    width: 30px;
    height: 30px;
    transform: rotate(0deg);
  }
  to {
    width: 50px;
    height: 50px;
    transform: rotate(210deg);
  }
}
@keyframes anime2 {
  from {
    transform: rotate(0deg);
    opacity:0;
  }
  to {
    transform: rotate(-210deg);
    opacity:1;
  }
}
<div class="menu-item-wrapper">
  <a class="menu-item-link" href="#"><span class="menu-item">1</span>Active or hovered Menu</a>
</div>

答案 1 :(得分:1)

你的意思是这样吗?

.menu-item-wrapper {
  width: 500px;
  clear: both;
}

.menu-item-link {
  margin-left: 5px;
  color: white;
  text-decoration: none;
}

.menu-item {
  float: left;
  color: white;
  text-align: center;
  padding-top: 5px;
  display: block;
  width: 30px;
  height: 30px;
  position: relative;
}

.menu-item::before {
  content: "";
  background-color: green;
  background-image: url("hexagon.svg"); /*added bg color because of the missing image*/
  background-size: contain;
  background-repeat: no-repeat;
  position: absolute;
  top: 0;
  left:0;
  width: 30px;
  height: 30px;
  transition: all 0.5s ease;
}
.menu-item::after {
  content: "";
  background-color: red;
  background-image: url("mars.png");
  background-size: contain;
  background-repeat: no-repeat;
  position: absolute;
  top: 0;
  left:0;
  bottom: 0;
  right: 0;
  opacity: 0;
  transition: opacity 0s 0.5s ease;
}

.menu-item:hover::before {
  animation-name: menuAnimation;
  animation-duration: 0.5s;
  animation-fill-mode: forwards;
  width: 50px;
  height: 50px;
  transform: rotate(210deg);
}

.menu-item:hover::after {
  opacity: 1;
}

https://codepen.io/zothynine/pen/GQQdLp