Schütteln Sie die Schaltflächen beim Schweben mit HTML und CSS

WBOY
Freigeben: 2023-09-11 19:41:02
nach vorne
689 Leute haben es durchsucht

Schütteln Sie die Schaltflächen beim Schweben mit HTML und 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);
   }
}
Nach dem Login kopieren

在上面的语法中,我们创建了自定义 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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

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>
Nach dem Login kopieren

结论

在本教程中,用户学会了只使用CSS来抖动HTML按钮。在第一个示例中,我们学会了垂直抖动按钮。在第二个示例中,我们学会了水平抖动按钮;在最后一个示例中,我们学会了水平和垂直方向上抖动按钮。

Das obige ist der detaillierte Inhalt vonSchütteln Sie die Schaltflächen beim Schweben mit HTML und CSS. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
Quelle:tutorialspoint.com
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!