PHP前端开发

如何通过悬停在图像或图标上来翻译它?

百变鹏仔 4周前 (09-20) #CSS
文章标签 图标

在网页开发中,互动性是提供令人难忘的用户体验的关键。一种常用的技术是在图像或图标上悬停以显示更多信息或改变外观。通过悬停在图像或图标上进行翻译是为您的网站增添一些动感和趣味的好方法。

在本文中,我们将学习如何在悬停时翻译图像或图标。为了完成这个任务,我们将学习使用仅限HTML和CSS的不同方法。

在悬停时翻译图像或图标的不同方法

方法1:CSS过渡效果

通过使用CSS过渡,可以实现在悬停时翻译图像或图标的第一种方法。CSS过渡用于平滑地改变属性值,比如在悬停在一个元素上时等等。使用过渡,可以指定动画的持续时间和时间函数。

语法

以下是使用CSS过渡来转换图像或图标的语法。

<img  src="your-image.jpg" class="trans-image" alt="如何通过悬停在图像或图标上来翻译它?" ><style>   .trans-image {      transition: transform 0.3s ease-in-out;   }   .trans-image:hover {      transform: translateX(20px);   }</style>

Example

在下面的示例中,我们使用了一个带有类名为“trans-image”的图像标签。在CSS部分,我们将过渡属性设置为“transform”,持续时间为0.3秒,并使用“ease-in-out”的缓动函数。当我们悬停在元素上时,如果是图像,则将transform属性设置为向右平移30像素,如果是图标,则向右平移20像素。

<html><head>   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">   <style>      .translate-image {         transition: transform 0.7s ease-in-out;      }      .translate-image:hover {         transform: translateX(30px);      }      #icon {         transition: transform 0.7s ease-in-out;      }      #icon:hover {         transform: translateX(20px);      }           </style></head><body>   <h2>Translating image and icon using CSS Transitions</h2>   <p> Hover over the below image or icon to see the transition </p>   <!-- Translating image on hover using CSS transitions -->   <img  src="https://www.tutorialspoint.com/static/images/logo.png?v2" class="translate-image" alt="如何通过悬停在图像或图标上来翻译它?" >   <br>   <!-- Translating icon on hover using CSS transitions -->   <i class="fa fa-html5" id="icon" style="color: green; font-size: 50px;" /></body></html>

方法二:CSS动画

将图像或图标在悬停时进行翻译的第一种方法是使用CSS动画。 CSS允许使用HTML来对元素进行动画处理,而无需使用JavaScript或Flash。在这里,我们可以根据需要更改任意数量的CSS属性,任意次数。

要使用CSS动画,我们首先必须为动画指定一些关键帧。关键帧确定元素在某些时间点上的样式。使用动画可以让我们创建比过渡更复杂和动态的效果。

语法

下面是使用CSS动画来转换图像或图标的语法。

<i class="your-icon"></i><style>   .your-icon {      display: inline-block;      width: 50px;      height: 50px;      background-color: #ccc;      animation: translate 0.3s ease-in-out;   }   .your-icon:hover {      animation-name: translate-hover;   }   @keyframes translate {      from {         transform: translateX(0);      }      to {         transform: translateX(10px);      }   }   @keyframes translate-hover {      from {         transform: translateX(10px);      }      to {         transform: translateX(20px);      }   }</style>

Example

在下面的示例中,我们使用了一个class为"icon"的"i"标签和一个class为"image"的标签。在这里,我们将display属性设置为"inline-block"。我们还将animation属性设置为"translate",持续时间为0.3秒,缓动函数为"ease-in-out"。现在当我们悬停时,通过使用关键帧将动画名称设置为"translate-hover",将图标和图片向右移动10像素,然后在后续悬停时向右移动20像素。

<html><head>   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">   <style>      .image {         display: inline-block;         width: 100px;         height: 100px;         animation: translate 0.3s ease-in-out;      }      .image:hover {animation-name: translate-hover;}      #icon {         display: inline-block;         width: 100px;         height: 100px;         animation: translate 0.3s ease-in-out;      }      #icon:hover {animation-name: translate-hover;}      @keyframes translate {         from {transform: translateX(0);}         to {transform: translateX(10px);}      }      @keyframes translate-hover {         from {transform: translateX(10px);}         to {transform: translateX(20px);}      }   </style></head><body>   <h2>Translating image and icon using CSS Animations</h2>   <p> Hover over the imgae orr icon to see the effect</p>   <!-- Translating image on hover using CSS Animations -->   <img  src="https://fastly.picsum.photos/id/213/200/300.jpg?hmac=t-54teMEgFL3q9WPaRq2t7YdGCU9aIRw77OCaHlSVRs" class="image" alt="如何通过悬停在图像或图标上来翻译它?" > <br>   <!-- Translating icon on hover using CSS Animations -->   <i class="fa fa-html5" id="icon" style="color: green; font-size: 50px;" /></body></html>

方法三:CSS Grid

通过使用CSS网格,将图像或图标在悬停时进行翻译的第一种方法是。CSS网格使用基于网格的布局系统,具有行和列,使得设计网页更容易,而无需使用浮动和定位。在这里,我们使用grid-row和grid-column属性指定网格项的位置,然后对要翻译的网格项应用CSS transform属性,如旋转或平移。

语法

下面是使用CSS网格来转换图像或图标的语法。

<div class="grid-container">   <img  src="your-image.jpg" class="trans-image" alt="如何通过悬停在图像或图标上来翻译它?" ></div><style>   .grid-container {      display: grid;      grid-template-columns: repeat(3, 1fr);      grid-template-rows: repeat(3, 1fr);      grid-gap: 10px;   }   .trans-image {      grid-row: 2 / 3;      grid-column: 2 / 3;      transition: transform 0.3s ease-in-out;   }   .trans-image:hover {      grid-column: 3 / 4;      transform: translateX(10px);   }</style>

Example

In the below example, we have defined a "div" tag with a class of "container". Here, in CSS we have set the display property to "grid", and define the grid template with three columns and three rows, each with a fraction unit of 1. To transform the image and icon, we have used the transition property to "transform" with a duration of 0.3 seconds and an easing function of "ease-in-out” which when hovered translate the image or icon 10 pixels to the right.

<html><head>   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">   <style>      .image {         grid-row: 2 / 3;         grid-column: 2 / 3;         transition: transform 0.3s ease-in-out;      }      .image:hover {         grid-column: 3 / 4;         transform: translateX(10px);      }      #icon {         grid-row: 2 / 3;         grid-column: 2 / 3;         transition: transform 0.3s ease-in-out;      }      #icon:hover {         grid-column: 3 / 4;         transform: translateX(10px);      }   </style></head><body>   <div>      <h2>Translating image and icon using CSS Grid</h2>      <p> Hover over the image or icon to see the effect </p>      <!-- Translating image on hover using CSS Grid -->      <img  src="https://www.tutorialspoint.com/static/images/logo.png?v2" class="image" alt="如何通过悬停在图像或图标上来翻译它?" >      <br>      <!-- Translating icon on hover using CSS Grid -->      <i class="fa fa-html5" id="icon" style="color: green; font-size: 50px;" />   </div></body></html>

结论

将互动性添加到我们的网站可以增强用户体验,而一种实现这一目标的方法是在悬停时翻译图像或图标。这种效果可以使用HTML和CSS实现,有不同的方法可以实现,例如使用CSS过渡或动画或网格。所有这些方法都允许我们指定动画的持续时间和时间函数,并创建动态效果。使用这些技术,我们可以创建一个更具吸引力的网站,给您的访问者留下深刻的印象。