在网页开发中,互动性是提供令人难忘的用户体验的关键。一种常用的技术是在图像或图标上悬停以显示更多信息或改变外观。通过悬停在图像或图标上进行翻译是为您的网站增添一些动感和趣味的好方法。
在本文中,我们将学习如何在悬停时翻译图像或图标。为了完成这个任务,我们将学习使用仅限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>
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">
<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过渡或动画或网格。所有这些方法都允许我们指定动画的持续时间和时间函数,并创建动态效果。使用这些技术,我们可以创建一个更具吸引力的网站,给您的访问者留下深刻的印象。
以上就是如何通过悬停在图像或图标上来翻译它?的详细内容,更多请关注双恒网络其它相关文章!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!
云资源网 » 如何通过悬停在图像或图标上来翻译它?
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 你们有qq群吗怎么加入?