如何在图片悬停时在 CSS 中创建分离动画?
How to create a separation animation in CSS upon hover of a picture?
我有下面的图片,我想为从左上角到右下角穿过徽标的线切割的对角线分割设置动画,如下所示。动画会将图片在 X 轴上分开几个像素。
https://i.ibb.co/fDzZ6Sc/Logo.png
到目前为止我已经取得了一些进展,我使用同一张图片两次并将它们相互重叠并在 X 轴上倾斜。然而,这将图片从右上角分开到左下角。我倒是喜欢,想不通。
我也不知道如何将鼠标悬停在整个图片上以使动画在两侧执行。
body { background: gainsboro; }
.pageOption {
overflow: hidden;
position: relative;
margin: 0 auto;
width: 40em; height: 27em;
}
.option, .option img { width: 100%; height: 100%; }
.option {
overflow: hidden;
position: absolute;
/* arctan(27 / 40) = 34.01935deg
* need to skew by 90deg - 34.01935deg = 55.98065deg
*/
transform: skewX(-55.98deg);
}
.option:first-child {
left: -.25em;
transform-origin: 100% 0;
}
.option:last-child {
right: -.25em;
transform-origin: 0 100%;
}
.option img, .option:after {
transform: skewX(55.98deg);
transform-origin: inherit;
}
.option:hover {
left: -.8em;
transition: 1s;
}
<div class='pageOption'>
<a href='#' class='option'>
<img src='https://i.ibb.co/fDzZ6Sc/Logo.png'>
</a>
<a href='#' class='option'>
<img src='https://i.ibb.co/fDzZ6Sc/Logo.png'>
</a>
</div>
基本上我想要发生的事情是当鼠标悬停在整个图片上时,两侧会从中间分开(从左上角到右下角的线切割),当你将鼠标移开时,以便图像一起返回。
您可以考虑 clip-path 并像下面这样轻松地执行此操作。诀窍是使用两个相反的多边形,组合起来将形成完整的图像。
.image {
width:340px;
height:230px;
background-image:url(https://i.ibb.co/fDzZ6Sc/Logo.png);
background-size:0;
position:relative;
}
.image:before,
.image:after {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background-image:inherit;
background-size:cover;
transition:1s;
}
.image:before {
clip-path:polygon(0 0, 15% 0, 97% 100%, 0 100%);
}
.image:after {
clip-path:polygon(100% 0, 15% 0, 97% 100%);
}
.image:hover::before{
transform:translate(-20px,-10px);
}
.image:hover::after{
transform:translate(20px,10px);
}
body {
background:pink;
}
<div class="image">
</div>
如果不想剪The
调整剪裁路径
.image {
width:340px;
height:230px;
background-image:url(https://i.ibb.co/fDzZ6Sc/Logo.png);
background-size:0;
position:relative;
}
.image:before,
.image:after {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background-image:inherit;
background-size:cover;
transition:1s;
}
.image:before {
clip-path:polygon(0 0, 32% 0 ,32% 19%, 97% 100%, 0 100%);
}
.image:after {
clip-path:polygon(100% 0,32% 0,32% 19%,97% 100%);
}
.image:hover::before{
transform:translate(-20px,-10px);
}
.image:hover::after{
transform:translate(20px,10px);
}
body {
background:pink;
}
<div class="image">
</div>
我有下面的图片,我想为从左上角到右下角穿过徽标的线切割的对角线分割设置动画,如下所示。动画会将图片在 X 轴上分开几个像素。
https://i.ibb.co/fDzZ6Sc/Logo.png
到目前为止我已经取得了一些进展,我使用同一张图片两次并将它们相互重叠并在 X 轴上倾斜。然而,这将图片从右上角分开到左下角。我倒是喜欢,想不通。
我也不知道如何将鼠标悬停在整个图片上以使动画在两侧执行。
body { background: gainsboro; }
.pageOption {
overflow: hidden;
position: relative;
margin: 0 auto;
width: 40em; height: 27em;
}
.option, .option img { width: 100%; height: 100%; }
.option {
overflow: hidden;
position: absolute;
/* arctan(27 / 40) = 34.01935deg
* need to skew by 90deg - 34.01935deg = 55.98065deg
*/
transform: skewX(-55.98deg);
}
.option:first-child {
left: -.25em;
transform-origin: 100% 0;
}
.option:last-child {
right: -.25em;
transform-origin: 0 100%;
}
.option img, .option:after {
transform: skewX(55.98deg);
transform-origin: inherit;
}
.option:hover {
left: -.8em;
transition: 1s;
}
<div class='pageOption'>
<a href='#' class='option'>
<img src='https://i.ibb.co/fDzZ6Sc/Logo.png'>
</a>
<a href='#' class='option'>
<img src='https://i.ibb.co/fDzZ6Sc/Logo.png'>
</a>
</div>
基本上我想要发生的事情是当鼠标悬停在整个图片上时,两侧会从中间分开(从左上角到右下角的线切割),当你将鼠标移开时,以便图像一起返回。
您可以考虑 clip-path 并像下面这样轻松地执行此操作。诀窍是使用两个相反的多边形,组合起来将形成完整的图像。
.image {
width:340px;
height:230px;
background-image:url(https://i.ibb.co/fDzZ6Sc/Logo.png);
background-size:0;
position:relative;
}
.image:before,
.image:after {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background-image:inherit;
background-size:cover;
transition:1s;
}
.image:before {
clip-path:polygon(0 0, 15% 0, 97% 100%, 0 100%);
}
.image:after {
clip-path:polygon(100% 0, 15% 0, 97% 100%);
}
.image:hover::before{
transform:translate(-20px,-10px);
}
.image:hover::after{
transform:translate(20px,10px);
}
body {
background:pink;
}
<div class="image">
</div>
如果不想剪The
调整剪裁路径
.image {
width:340px;
height:230px;
background-image:url(https://i.ibb.co/fDzZ6Sc/Logo.png);
background-size:0;
position:relative;
}
.image:before,
.image:after {
content:"";
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
background-image:inherit;
background-size:cover;
transition:1s;
}
.image:before {
clip-path:polygon(0 0, 32% 0 ,32% 19%, 97% 100%, 0 100%);
}
.image:after {
clip-path:polygon(100% 0,32% 0,32% 19%,97% 100%);
}
.image:hover::before{
transform:translate(-20px,-10px);
}
.image:hover::after{
transform:translate(20px,10px);
}
body {
background:pink;
}
<div class="image">
</div>