HTML, CSS and jQuery: Tips for implementing image shrinking effects
In modern web design, implementing some cool special effects can make web pages more attractive. Among them, image shrinking effects are often used to highlight important content on web pages. This article will introduce how to use HTML, CSS and jQuery to achieve image shrinking effects, and provide specific code examples.
<!DOCTYPE html> <html> <head> <title>图片缩小特效</title> <link rel="stylesheet" type="text/css" href="style.css"> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="script.js"></script> </head> <body> <div class="image-container"> <img src="images/image.jpg" alt="图片"> </div> </body> </html>
.image-container { width: 500px; /* 设置容器宽度 */ height: 500px; /* 设置容器高度 */ overflow: hidden; /* 隐藏超出容器的部分 */ position: relative; /* 为了让子元素绝对定位 */ } .image-container img { width: 100%; /* 将图片宽度设置为容器宽度 */ height: auto; /* 根据图片比例自适应高度 */ position: absolute; /* 绝对定位 */ top: 0; /* 图片相对于容器顶部位置 */ left: 0; /* 图片相对于容器左侧位置 */ transition: transform 0.5s; /* 添加动画过渡效果 */ } .image-container img:hover { transform: scale(0.7); /* 当鼠标悬停时,缩小图片至原大小的70% */ }
$(document).ready(function() { $(".image-container img").hover( function() { $(this).addClass("hovered"); /* 添加hovered类 */ }, function() { $(this).removeClass("hovered"); /* 移除hovered类 */ } ); });
.image-container img.hovered { transform: scale(0.7); /* 缩小图片至原大小的70% */ transition: transform 0.5s; /* 添加过渡效果 */ }
Through the above steps, we used HTML, CSS and jQuery to achieve the image shrinking effect. When the mouse is hovered over the image, the image will animate to shrink to 70% of its original size. This special effect can not only highlight the important content of the web page, but also improve the user experience. I hope the code examples in this article will be helpful to you and allow you to easily implement various cool web page effects.
The above is the detailed content of HTML, CSS and jQuery: Tips for achieving image shrinking effects. For more information, please follow other related articles on the PHP Chinese website!