You need to enable JavaScript to run this app. Bootstrap4中如何实现默认显半图、鼠标悬停时全图从右向左滑动效果
优惠活动
大模型
产品
解决方案
定价
更多

Bootstrap4中如何实现默认显半图、鼠标悬停时全图从右向左滑动效果

解决方案

问题根源

  • 缺少外层容器的overflow: hidden规则,图片默认完全展示,平移效果无法被感知
  • 固定平移值适配性差,不同尺寸的容器和图片效果不符合预期
  • 未处理Bootstrap列默认内边距、图片比例适配的问题,和现有位移规则冲突

修复后代码

CSS代码

/* 新增外层容器规则,控制可视区域 */
.feature-image-wrap {
  overflow: hidden;
  width: 100%;
  height: 320px; /* 可根据设计需求调整高度 */
  position: relative;
}
.moving-left{
  width: 200%; /* 宽度设置为外层的2倍,保证默认仅展示一半内容 */
  height: 100%;
  position: relative;
  transition: transform 0.3s ease;
  transform: translateX(50%); /* 默认右移50%,仅露出图片右半部分 */
}
/* 新增图片适配规则 */
.moving-left img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.moving-left:hover{
  transform: translateX(0); /* 悬停时左移到0位,完整展示图片 */
}
.moving-left button{
  left: 14px;
  position: absolute;
  top: 30px;
  z-index: 99;
}

HTML代码

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1Qtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

<div class="row mb-75">
  <!-- 给列加px-0去掉默认左右内边距,避免影响可视区域宽度 -->
  <div class="col-lg-7 px-0">
    <div class="anime-image-wrapper">
      <div class="feature-image-wrap ml--55">
        <div class="moving-left">
          <img src="https://colorlib.com/wp/wp-content/uploads/sites/2/best-bootstrap-admin-templates.jpg" class="wow zoom" alt="astriol feature">
        </div>
      </div>
      <span class="circle" data-parallax='{"y": -70}'></span>
      <div class="anime-dot">
        <img src="media/feature/dot.png" alt="astrion dot">
      </div>
    </div>
    <!-- /.anime-image-wrapper -->
  </div>
  <!-- /.col-md-6 -->
</div>

可调参数说明

  • 可修改.feature-image-wrap的height值调整容器高度
  • 可修改.moving-left的transition属性值调整动画时长和缓动效果
  • 如果需要自定义图片露出比例,可调整.moving-left的width和默认translateX值,比如要默认露出30%,可设置width: calc(100% / 0.3),默认translateX(calc(100% - 30%))即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 18:45:03