2023使用HTML和CSS在悬停时抖动按钮

 所属分类:web前端开发

 浏览:98次-  评论: 0次-  更新时间:2023-09-27
描述:更多教程资料进入php教程获得。 在本教程中,我们将学习&#2035...
更多教程资料进入php教程获得。

使用HTML和CSS在悬停时抖动按钮

在本教程中,我们将学习使用 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按钮。在第一个示例中,我们学会了垂直抖动按钮。在第二个示例中,我们学会了水平抖动按钮;在最后一个示例中,我们学会了水平和垂直方向上抖动按钮。

积分说明:注册即送10金币,每日签到可获得更多金币,成为VIP会员可免金币下载! 充值积分充值会员更多说明»

讨论这个素材(0)回答他人问题或分享使用心得奖励金币

〒_〒 居然一个评论都没有……

表情  文明上网,理性发言!