You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现带图片淡入淡出+缩放效果的菜单遇到问题求助

实现菜单悬停切换左侧图片的淡入淡出与缩放效果

要实现你需要的交互效果,我们需要调整布局结构、添加CSS过渡动画,并通过JavaScript关联菜单项与对应图片。以下是修改后的完整实现方案:

HTML 结构调整

给左侧图片容器和右侧菜单项添加data-index属性建立一一对应关系,同时给图片统一类名方便批量控制:

<div class="container">
  <!-- leftSide -->
  <div class="left">
    <div class="image-item active" data-index="0" style="height: 100%; width: 100%; background: url('https://images.pexels.com/photos/15792501/pexels-photo-15792501.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2') no-repeat center center / cover;"></div>
    <div class="image-item" data-index="1" style="height: 100%; width: 100%; background: url('https://images.pexels.com/photos/16375253/pexels-photo-16375253.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2') no-repeat center center / cover;"></div>
    <div class="image-item" data-index="2" style="height: 100%; width: 100%; background: url('https://images.pexels.com/photos/14917839/pexels-photo-14917839.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2') no-repeat center center / cover;"></div>
    <div class="image-item" data-index="3" style="height: 100%; width: 100%; background: url('https://images.pexels.com/photos/15413559/pexels-photo-15413559.jpeg?auto=compress&cs=tinysrgb&w=1260&h=750&dpr=2') no-repeat center center / cover;"></div>
  </div>

  <div class="right">
    <div class="menu">
      <a href="#" data-index="0">Home</a>
      <a href="#" data-index="1">About Us</a>
      <a href="#" data-index="2">Services</a>
      <a href="#" data-index="3">Contact Us</a>
    </div>
  </div>
</div>

CSS 样式完善

设置图片叠加定位,添加淡入淡出与缩放的过渡动画,同时优化菜单交互样式:

body {
  margin: 0;
  padding: 0;
}

.container {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

.left {
  width: 50%;
  position: relative;
  height: 100%;
}

.image-item {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  transform: scale(0.8);
  transition: opacity 0.6s ease, transform 0.6s ease;
}

.image-item.active {
  opacity: 1;
  transform: scale(1);
}

.right {
  width: 50%;
  background-color: #111111;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.menu {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.menu a {
  margin: 10px;
  padding: 10px;
  text-decoration: none;
  color: #999;
  font-size: 32px;
  font-weight: bold;
  font-family: Verdana, Geneva, Tahoma, sans-serif;
  transition: color 0.3s ease;
}

.menu a:hover {
  color: white;
}

JavaScript 交互逻辑

监听菜单项的鼠标悬停事件,切换对应图片的显示状态:

const menuItems = document.querySelectorAll('.menu a');
const imageItems = document.querySelectorAll('.image-item');

menuItems.forEach(item => {
  item.addEventListener('mouseenter', () => {
    // 移除所有图片的激活状态
    imageItems.forEach(img => img.classList.remove('active'));
    // 获取当前菜单项索引,激活对应图片
    const index = item.getAttribute('data-index');
    imageItems[index].classList.add('active');
  });
});

// 页面加载时默认显示第一张图片
imageItems[0].classList.add('active');

核心逻辑说明

  • 定位控制:左侧容器设为relative,图片设为absolute,让所有图片叠在同一位置,仅显示带有active类的图片
  • 过渡动画:通过transition属性定义opacity(淡入淡出)和transform: scale(缩放)的动画时长与缓动效果,实现平滑切换
  • 关联映射:利用data-index属性将菜单项与图片一一绑定,鼠标悬停时切换对应图片的激活状态,触发动画

内容的提问来源于stack exchange,提问作者Cloudboy22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 18:35:17