在本教程中,我们将学习使用 HTML 和 CSS 在用户悬停时摇动按钮。创建摇动按钮可以使应用程序的用户体验更具吸引力。
我们需要使用 CSS“关键帧”规则创建自定义动画来摇动任何 HTML 元素。之后,我们可以使用自定义关键帧作为“animation”CSS属性的值,以便当用户将鼠标悬停在按钮上时摇动按钮。
语法
用户可以按照以下语法使用 HTML 和 CSS 来摇动悬停按钮。
.btn:hover {
animation: key_frame_name animation_time repetition;
}
@keyframes key_frame_name {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(10deg);
}
}
在上面的语法中,我们创建了自定义 CSS 规则来向按钮添加晃动动画。用户可以将“animation_time”替换为时间单位,并将“repetition”替换为数字来重复动画。
Example
的中文翻译为:
示例
在下面的示例中,我们垂直摇动按钮。我们使用“button”标签创建了普通的 HTML 按钮,并给出了“btn”类名称。我们使用类名访问该按钮并设置其样式。
在 CSS 中,我们使用“animation”属性在用户悬停按钮时向按钮添加“晃动”关键帧。在“摇动”关键帧中,我们在 0% 的动画时间将按钮旋转“0 度”,在 20% 的时间旋转“5 度”,在 50% 的时间旋转按钮“0 度”,在 50% 的时间旋转按钮“5 度” 70% 的时间为“0 度”,100% 的时间为“0 度”。
在输出中,用户可以观察到按钮在垂直方向上的晃动。
<html>
<style>
.btn {
justify-content: center;
align-items: center;
height: fit-content;
padding: 10px 20px;
border: 1px solid #000;
border-radius: 5px;
background-color: red;
color: white;
font-size: 40px;
}
.btn:hover {animation: shaking 0.5s infinite;}
@keyframes shaking {
0% {transform: rotate(0deg);}
20% {transform: rotate(-4deg);}
50% {transform: rotate(0deg);}
70% {transform: rotate(4deg);}
100% {transform: rotate(0deg);}
}
</style>
<body>
<h2> Shaking the button vertically using HTML and CSS </h2>
<p> Please hover the cursor over the button below to see the shaking effect.</p>
<div>
<button class = "btn"> Submit </button>
</div>
</body>
</html>
Example
的中文翻译为:
示例
在下面的示例中,我们使用HTML和CSS水平晃动按钮。
我们使用了CSS属性 ‘transform: translateX()’ 来在水平方向上抖动按钮。首先,我们将按钮向负方向移动。接下来,我们将按钮移动到原始位置。然后,我们将按钮向正方向移动,最后,我们使用CSS的 ‘keyframes’ 规则将按钮移动到原始位置
<html>
<style>
.btn {
justify-content: center;
align-items: center;
height: fit-content;
padding: 10px 20px;
border: 1px solid #000;
border-radius: 5px;
background-color: black;
color: white;
font-size: 40px;
}
.btn:hover {animation: shaking 0.4s infinite;}
@keyframes shaking {
0% {transform: translateX(-10px);}
20% {transform: translateX(-5px);}
50% {transform: translateX(-5px);}
70% {transform: translateX(-5px);}
80% {transform: translateX(10px);}
90% {transform: translateX(-10px);}
}
</style>
<body>
<h2> Shaking the button Horizontally using HTML and CSS </h2>
<p> Please hover the cursor over the button below to see the shaking effect.</p>
<div>
<button class = "btn"> Hover the Button </button>
</div>
</body>
</html>
Example
的中文翻译为:
示例
在下面的示例中,我们将学习如何水平和垂直地摇晃按钮。我们使用‘translateX()’和‘rotate()’一起作为‘transform’ CSS属性的值。
‘translateX()’将按钮水平移动,‘rotate()’函数将按钮垂直移动。在输出中,用户可以观察到当他们悬停在按钮上时,它会稍微水平和垂直移动。然而,用户可以增加‘translateX()’函数的参数值,以在水平方向上抖动更多。
<html>
<style>
.btn {
justify-content: center;
align-items: center;
height: fit-content;
padding: 10px 20px;
border: 1px solid #000;
border-radius: 5px;
background-color: green;
color: white;
font-size: 25px;
}
.btn:hover {animation: shaking 0.4s infinite;}
@keyframes shaking {
0% {transform: translateX(0) rotate(0deg);}
25% {transform: translateX(15px) rotate(5deg);}
50% {transform: translateX(0px) rotate(0deg);}
75% {transform: translateX(-15px) rotate(-5deg);}
100% {transform: translateX(0px) rotate(0deg);}
}
</style>
<body>
<h3> Shaking the button Horizontally and vartically using HTML and CSS</h3>
<div>
<button class = "btn"> Point out the Button </button>
</div>
</body>
</html>
结论
在本教程中,用户学会了只使用CSS来抖动HTML按钮。在第一个示例中,我们学会了垂直抖动按钮。在第二个示例中,我们学会了水平抖动按钮;在最后一个示例中,我们学会了水平和垂直方向上抖动按钮。
以上就是使用HTML和CSS在悬停时抖动按钮的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 使用HTML和CSS在悬停时抖动按钮
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?