剪切路径更改时的悬停过渡

问题描述 投票:1回答:2

使用此代码,悬停效果有效,右下角消失但没有过渡,这有什么问题吗?

.mydiv:hover{
      background-color: blue;
      clip-path: polygon(0% 0%, 100% 0%, 80% 100%, 0% 100%);
      transition: 0.5s ease;
 }
css clip-path
2个回答
3
投票

您需要添加初始的clip-path定义才能在两种状态之间进行转换:

.box {
  background-color: blue;
  clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
  transition: 0.5s ease;
  height:150px;
}

.box:hover {
  clip-path: polygon(0% 0%, 100% 0%, 80% 100%, 0% 100%);
}
<div class="box">

</div>

您也可以在后台进行同样的操作,您将获得更好的支持:

.box {
  background: 
    linear-gradient(blue,blue) left,
    linear-gradient(to bottom right,blue 49.5%,transparent 50%) right;
  background-size:100% 100%,0% 100%;
  background-repeat:no-repeat;
  transition: 0.5s ease;
  height:150px;
}

.box:hover {
  background-size:80.1% 100%,20% 100%;
}
<div class="box">

</div>

0
投票

如果要将转换属性用于伪类,则需要将其设置为类

.mydiv {
  background: red;
  clip-path: polygon(0% 0%, 100% 0%, 100% 100%, 0% 100%);
  transition: all 0.5s ease;
}

.mydiv:hover {
   background: blue;
   clip-path: polygon(0% 0%, 100% 0%, 80% 100%, 0% 100%);
 }
<div class="mydiv">
  Hello world
</div>
© www.soinside.com 2019 - 2024. All rights reserved.