我妈已经三天没打我了 2025-09-25 14:55 采纳率: 86.6%
浏览 8

悬浮窗吸附,为什么div不能点击

为什么不能点击

img


<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"
    />
    <style>
      .icon_fushare_old {
        position: fixed;
        z-index: 999999999999999;
        top: 15rem;
        right: -1.48rem;
        width: 2.88rem;
        height: 6.36rem;
        background: #0099fa;
        box-shadow: 0px 0.28rem 0.28rem 0px rgba(40, 100, 175, 0.4);
        border-radius: 1.44rem;
        transition: transform 0.3s ease;
      }
    </style>
  </head>

  <body>
    <div class="icon_fushare_old" id="icon_fushare_old"></div>
    <script>
      const icon_fushare_old = document.getElementById("icon_fushare_old");
      let isDragging = false;
      let isMenuOpen = false;
      let startX, startY, offsetX, offsetY;
      let inactivityTimer;
      // 边缘吸附逻辑
      function snapToEdge() {
        const rect = icon_fushare_old.getBoundingClientRect();
        const windowWidth = window.innerWidth;
        const windowHeight = window.innerHeight;
        const distances = {
          right: windowWidth - rect.right,
        };
        const minEdge = Object.keys(distances).reduce((a, b) =>
          distances[a] < distances[b] ? a : b
        );
        icon_fushare_old.style.transition = "all 0.3s ease";
        switch (minEdge) {
          case "right":
            icon_fushare_old.style.right = "0.48rem";
            icon_fushare_old.style.left = "";
            icon_fushare_old.style.transform = "translateX(80%)";
            break;
        }
        setTimeout(() => {
          icon_fushare_old.style.transition = "var(--transition)";
        }, 500);
      }
      function resetInactivityTimer() {
        clearTimeout(inactivityTimer);
        inactivityTimer = setTimeout(() => {
          if (!isMenuOpen && !isDragging) snapToEdge();
        }, 3000);
      }
      icon_fushare_old.addEventListener("mousedown", startDrag);
      icon_fushare_old.addEventListener("touchstart", startDrag);
      icon_fushare_old.addEventListener("mouseup", endDrag);
      icon_fushare_old.addEventListener("touchend", endDrag);


      function startDrag(e) {
        isDragging = true;
        icon_fushare_old.style.cursor = "grabbing";
        const rect = icon_fushare_old.getBoundingClientRect();
        offsetX = (e.clientX || e.touches[0].clientX) - rect.left;
        offsetY = (e.clientY || e.touches[0].clientY) - rect.top;
        icon_fushare_old.style.transform = "none";
        e.preventDefault();
      }
      function endDrag() {
    isDragging = false;
    icon_fushare_old.style.cursor = "grab";
    resetInactivityTimer(); // 重置3秒计时
}
      resetInactivityTimer();
    </script>
  </body>
</html>

  • 写回答

6条回答 默认 最新

  • 阿里嘎多学长 2025-09-25 15:05
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    问题分析

    你使用了 HTML 和 CSS 悬浮窗吸附功能,但是发现 div 元素不能被点击。这个问题通常与元素的 Z轴顺序有关,或者是 CSS 的层叠上下文的问题。

    解决方案

    1. 检查 Z轴顺序: 确保被吸附的元素的 z-index 值大于其他元素的 z-index 值。
    2. 设置层叠上下文: 吸附元素的父元素或本身设置 -webkit-transform、opacity 或 -ms-filter 等属性,建立层叠上下文。
    3. 禁用 CSS 事件捕获: 在吸附元素或其父元素上添加事件捕获的 CSS 属性,如 pointer-events: none,或使用 JavaScript 禁用点击事件。
    4. 检查 HTML 结构: 确保吸附元素是可以点击的 HTML 元素,不是非点击元素(如 inputbutton 等)。

    核心部分代码

    &lt;div style="position: fixed; z-index: 1000; top: 10px; left: 10px;" &gt;
      &lt;!-- 内容 --&gt;
    &lt;/div&gt;
    
    /* 禁用 CSS 事件捕获 */
    div {
      pointer-events: all; /* 或 none */
    }
    

    // 禁用点击事件
    document.querySelector('div').addEventListener('click', function(event) {
      event.preventDefault();
    });
    
    评论

报告相同问题?

问题事件

  • 修改了问题 9月25日
  • 创建了问题 9月25日