带有剪切路径的自定义链接形状和动画

时间:2020-08-10 04:15:29

标签: html css css-animations clip-path

我最近一直在学习CSS中clip-path的神奇特性,但是在尝试创建自定义图像链接时遇到了一个问题。我无法将实际形状变成可点击的链接。

当我尝试在裁剪的div中放置<a>时,形状本身将无法点击-即使我将其设置为与父div相同的尺寸。这是我的可链接剪辑路径(https://css-tricks.com/the-many-ways-to-link-up-shapes-and-images-with-html-and-css/)的参考站点。

我想知道它是否能够成为链接,因为它具有鼠标悬停时的动画效果?这是我的代码段。

/* Styles the link */
#inner-link {
  width: 200px;
  height: 200px;
}

/* Styles the parent container */
#button-container {
  width: 200px;
  height: 200px;
  margin: 10%;
  background-color: #ed991c;
  clip-path: polygon(50% 0%, 100% 100%, 0% 100%, 0% 100%, 0% 100%, 0% 100%, 0% 100%);
}
#button-container:hover {
  animation: arrow 0.5s forwards;
}

/* animation from triangle to arrow */
@keyframes arrow {
  0% {
    clip-path: polygon(50% 0%, 100% 100%, 0% 100%, 0% 100%, 0% 100%, 0% 100%, 0% 100%);
    background-color: #ed991c;
  }
  100% {
    clip-path: polygon(30% 0%, 100% 50%, 30% 100%, 30% 75%, 0% 75%, 0% 25%, 30% 25%);
    background-color: #edd11c;
  }
}
  
  
<div id="button-container">
  <a id="inner-link" href="https://www.target.com/"><a>
 </div>

1 个答案:

答案 0 :(得分:0)

a标签是一个内联元素,并且内联元素没有高度或宽度。

一种快速的解决方法是将display: blockdisplay: inline-block添加到您的#inner-link中。

#inner-link {
    width: 200px;
    height: 200px;
    display: block;
}

#inner-link {
  width: 200px;
  height: 200px;
  display: block;
}


/* Styles the parent container */

#button-container {
  width: 200px;
  height: 200px;
  margin: 10%;
  background-color: #ed991c;
  clip-path: polygon( 50% 0%, 100% 100%, 0% 100%, 0% 100%, 0% 100%, 0% 100%, 0% 100%);
}

#button-container:hover {
  animation: arrow 0.5s forwards;
}


/* animation from triangle to arrow */

@keyframes arrow {
  0% {
    clip-path: polygon( 50% 0%, 100% 100%, 0% 100%, 0% 100%, 0% 100%, 0% 100%, 0% 100%);
    background-color: #ed991c;
  }
  100% {
    clip-path: polygon( 30% 0%, 100% 50%, 30% 100%, 30% 75%, 0% 75%, 0% 25%, 30% 25%);
    background-color: #edd11c;
  }
}
<div id="button-container">
  <a id="inner-link" href="https://www.target.com/"></a>
</div>