您好,欢迎访问一九零五行业门户网

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

在网页开发中,互动性是提供令人难忘的用户体验的关键。一种常用的技术是在图像或图标上悬停以显示更多信息或改变外观。通过悬停在图像或图标上进行翻译是为您的网站增添一些动感和趣味的好方法。
在本文中,我们将学习如何在悬停时翻译图像或图标。为了完成这个任务,我们将学习使用仅限html和css的不同方法。
在悬停时翻译图像或图标的不同方法方法1:css过渡效果通过使用css过渡,可以实现在悬停时翻译图像或图标的第一种方法。css过渡用于平滑地改变属性值,比如在悬停在一个元素上时等等。使用过渡,可以指定动画的持续时间和时间函数。
语法以下是使用css过渡来转换图像或图标的语法。
<img src=your-image.jpg class=trans-image><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> <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的<img>标签。在这里,我们将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> <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></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>
examplein 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> <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过渡或动画或网格。所有这些方法都允许我们指定动画的持续时间和时间函数,并创建动态效果。使用这些技术,我们可以创建一个更具吸引力的网站,给您的访问者留下深刻的印象。
以上就是如何通过悬停在图像或图标上来翻译它?的详细内容。
其它类似信息

推荐信息