gpt4 book ai didi

javascript - 绘制元素边框(动画)

转载 作者:太空狗 更新时间:2023-10-29 15:03:30 26 4
gpt4 key购买 nike

我正在尝试找出一种方法,让按钮为其边框设置动画效果,就好像有人在绘制它一样。

到目前为止我得到的最接近的是这个片段,尽管它不能很好地与 border-radius 集一起工作。 (注意 Angular 落)

https://codepen.io/anon/pen/MbWagQ

<button class="draw">draw</button>

//Colors
$cyan: #60daaa;
$red: #f45e61;

// Basic styles
button {
background: none;
border: 0;
box-sizing: border-box;
color: $red;
font-size: inherit;
font-weight: 700;
padding: 1em 2em;
text-align: center;
margin: 20px;

// Required, since we're setting absolute on pseudo-elements
position: relative;
vertical-align: middle;

&::before,
&::after {
box-sizing: border-box;
content: '';
position: absolute;
width: 100%;
height: 100%;
}
}

.draw {
transition: color 0.25s;
border-radius: 7px;

&::before,
&::after {
border-radius: 7px;
border: 3px solid transparent; // Set border to invisible, so we don't see a 4px border on a 0x0 element before the transition starts
width: 0;
height: 0;
}

// This covers the top & right borders (expands right, then down)
&::before {
top: 0;
left: 0;
}

// And this the bottom & left borders (expands left, then up)
&::after {
bottom: 0;
right: 0;
}

&:hover {
color: $cyan;
}

// Hover styles
&:hover::before,
&:hover::after {
width: 100%;
height: 100%;
}

&:hover::before {
border-top-color: $cyan; // Make borders visible
border-right-color: $cyan;
transition:
width 0.25s ease-out, // Width expands first
height 0.25s ease-out 0.25s; // And then height
}

&:hover::after {
border-bottom-color: $cyan; // Make borders visible
border-left-color: $cyan;
transition:
border-color 0s ease-out 0.5s, // Wait for ::before to finish before showing border
width 0.25s ease-out 0.5s, // And then exanding width
height 0.25s ease-out 0.75s; // And finally height
}
}

我尽量避免使用 svg 文件,最好是使用纯 html 和 css,但 javascript 没问题。

最佳答案

所以发生的事情是您的 draw::before/draw::after 元素在过渡开始时的高度为 0px。这意味着边界半径将非常倾斜。

button {
background: none;
border: 0;
box-sizing: border-box;
color: #f45e61;
font-size: inherit;
font-weight: 700;
padding: 200px;
text-align: center;
margin: 20px;
position: relative;
vertical-align: middle;
}
button::before,
button::after {
box-sizing: border-box;
content: '';
position: absolute;
width: 100%;
height: 100%;
}
.draw {
-webkit-transition: color 0.25s;
transition: color 0.25s;
border-radius: 70px;
}
.draw::before,
.draw::after {
border-radius: 70px;
border: 30px solid transparent;
width: 0;
height: 0;
}
.draw::before {
top: 0;
left: 0;
}
.draw::after {
bottom: 0;
right: 0;
}
.draw:hover {
color: #60daaa;
}
.draw:hover::before,
.draw:hover::after {
width: 100%;
height: 100%;
}
.draw:hover::before {
border-top-color: #60daaa;
border-right-color: #60daaa;
-webkit-transition: width 1.25s ease-out, height 1.25s ease-out 1.25s;
transition: width 1.25s ease-out, height 1.25s ease-out 1.25s;
}
.draw:hover::after {
border-bottom-color: #60daaa;
border-left-color: #60daaa;
-webkit-transition: border-color 0s ease-out 2.5s, width 1.25s ease-out 2.5s, height 1.25s ease-out 3.75s;
transition: border-color 0s ease-out 2.5s, width 1.25s ease-out 2.5s, height 1.25s ease-out 3.75s;
}
<h1>CSS Border Transitions</h1>
<button class="draw">draw</button>

放大/放慢动画后,您可以看到问题所在。我还建议在右/左边框上放置一个过渡,直到高度过渡,以避免“绘图点”成为奇怪的形状。

这是我所说的边界半径倾斜的另一个例子:

Skewed

http://codepen.io/anon/pen/mOdOyQ

关于javascript - 绘制元素边框(动画),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40412779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com