js实现卷轴,中间可滑动方块,左右两侧对比

html 复制代码
<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Div Swipe 示例</title>
    <style>
      * {
        box-sizing: border-box;
      }

      html,
      body {
        padding: 0;
        margin: 0;
        height: 100%;
        font-family: "Segoe UI", "PingFang SC", sans-serif;
        background: #f6f8fb;
      }

      .wrapper {
        position: relative;
        width: min(95vw, 1200px);
        height: min(80vh, 700px);
        margin: 30px auto 20px;
        border-radius: 12px;
        overflow: hidden;
        box-shadow: 0 8px 25px rgba(15, 23, 42, 0.18);
      }

      .panel {
        position: absolute;
        inset: 0;
        display: grid;
        place-items: center;
        color: #fff;
        font-size: 32px;
        font-weight: 600;
        text-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
      }

      .panel.panel-left {
        background: linear-gradient(135deg, #43cea2 0%, #185a9d 100%);
      }

      .panel.panel-right {
        background: linear-gradient(135deg, #ff512f 0%, #dd2476 100%);
        clip-path: inset(0 0 0 50%);
      }

      .panel iframe {
        width: 100%;
        height: 100%;
        border: none;
      }

      .handle {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 4px;
        background: #fff;
        border-radius: 4px;
        box-shadow: 0 0 8px rgba(0, 0, 0, 0.4);
        cursor: ew-resize;
        transform: translateX(-50%);
      }

      .handle::before {
        content: "";
        position: absolute;
        top: 50%;
        left: 50%;
        width: 36px;
        height: 36px;
        border-radius: 50%;
        border: 3px solid #fff;
        background: rgba(24, 90, 157, 0.95);
        transform: translate(-50%, -50%);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
      }

      .tip {
        text-align: center;
        color: #4b4d52;
        font-size: 14px;
      }
    </style>
  </head>
  <body>
    <div class="wrapper" id="swipeWrapper">
      <div class="panel panel-left"></div>
      <div class="panel panel-right" id="panelRight">After</div>
      <div class="handle" id="handle"></div>
    </div>
    <p class="tip">在两个 div 上实现的 Swipe,对比任意内容或样式</p>

    <script>
      const wrapper = document.getElementById("swipeWrapper");
      const handle = document.getElementById("handle");
      const panelRight = document.getElementById("panelRight");

      let isDragging = false;

      const setPosition = (clientX) => {
        const rect = wrapper.getBoundingClientRect();
        const clamped = Math.min(Math.max(clientX - rect.left, 0), rect.width);
        const percent = (clamped / rect.width) * 100;
        panelRight.style.clipPath = `inset(0 ${100 - percent}% 0 0)`;
        handle.style.left = `${percent}%`;
      };

      const onPointerMove = (event) => {
        if (!isDragging) return;
        setPosition(event.clientX ?? event.touches?.[0]?.clientX ?? 0);
      };

      const startDrag = (event) => {
        isDragging = true;
        wrapper.setPointerCapture?.(event.pointerId);
        setPosition(event.clientX ?? event.touches?.[0]?.clientX ?? 0);
      };

      const stopDrag = (event) => {
        isDragging = false;
        wrapper.releasePointerCapture?.(event.pointerId);
      };

      handle.addEventListener("pointerdown", startDrag);
      window.addEventListener("pointermove", onPointerMove);
      window.addEventListener("pointerup", stopDrag);

      // 初始化到中间位置
      setPosition(wrapper.getBoundingClientRect().width / 2);
    </script>
  </body>
</html>
相关推荐
灵感__idea2 分钟前
Hello 算法:贪心的世界
前端·javascript·算法
killerbasd3 小时前
牧苏苏传 我不装了 4/7
前端·javascript·vue.js
爱上好庆祝4 小时前
svg图片
前端·css·学习·html·css3
橘子编程4 小时前
JavaScript与TypeScript终极指南
javascript·ubuntu·typescript
叫我一声阿雷吧5 小时前
JS 入门通关手册(45):浏览器渲染原理与重绘重排(性能优化核心,面试必考
javascript·前端面试·前端性能优化·浏览器渲染·浏览器渲染原理,重排重绘·reflow·repaint
大家的林语冰5 小时前
《前端周刊》尤大开源 Vite+ 全家桶,前端工业革命启动;尤大爆料 Void 云服务新产品,Vite 进军全栈开发;ECMA 源码映射规范......
前端·javascript·vue.js
jiayong235 小时前
第 8 课:开始引入组合式函数
前端·javascript·学习
天若有情6736 小时前
【C++原创开源】formort.h:一行头文件,实现比JS模板字符串更爽的链式拼接+响应式变量
开发语言·javascript·c++·git·github·开源项目·模版字符串
yuki_uix6 小时前
重排、重绘与合成——浏览器渲染性能的底层逻辑
前端·javascript·面试
止观止7 小时前
拥抱 ESNext:从 TC39 提案到生产环境中的现代 JS
开发语言·javascript·ecmascript·esnext