要实现这个效果,你可以使用HTML、CSS和JavaScript来完成。以下是一个简单的示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Image Hover Effect</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="image-container">
<img src="your-image.jpg" alt="Your Image" class="image">
<a href="your-link.html" class="link">Link</a>
</div>
<script src="script.js"></script>
</body>
</html>
.image-container {
position: relative;
display: inline-block;
}
.image {
width: 100%;
transition: opacity 0.3s ease;
}
.link {
position: absolute;
top: 0;
left: 0;
width: 100%;
text-align: center;
background-color: rgba(0, 0, 0, 0.5);
color: white;
padding: 10px 0;
opacity: 0;
transition: opacity 0.3s ease;
}
.image-container:hover .image {
opacity: 0.7;
}
.image-container:hover .link {
opacity: 1;
}
document.addEventListener('DOMContentLoaded', () => {
const imageContainer = document.querySelector('.image-container');
const link = document.querySelector('.link');
imageContainer.addEventListener('mousemove', (event) => {
const rect = imageContainer.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
if (event.clientX >= centerX - 5 && event.clientX <= centerX + 5 && event.clientY >= centerY - 5 && event.clientY <= centerY + 5) {
link.style.display = 'block';
} else {
link.style.display = 'none';
}
});
imageContainer.addEventListener('mouseleave', () => {
link.style.display = 'none';
});
});
这个效果可以用于图像库、产品展示页面等,当用户将鼠标悬停在图像中心时,显示更多信息或操作链接。
通过这种方式,你可以实现一个交互性强、用户体验好的图像悬停效果。
领取专属 10元无门槛券
手把手带您无忧上云