代码之家  ›  专栏  ›  技术社区  ›  John Balvin Arias

当css transform:scale()处于活动状态时,单击边框按钮会触发单击

  •  0
  • John Balvin Arias  · 技术社区  · 6 年前

    触发click事件取决于 transform: scale() 。 当有一个相对较大的数字时,它会工作,但当有一个较小的数字时,它不会正常工作。

    有人知道怎么修吗?

    Here is my Demo

    尝试单击边框以查看它。

    document.querySelectorAll("button").forEach((itm)=>{
      itm.addEventListener("click",function(){alert("press")});
    })
    button{cursor:pointer;user-select:none;outline:none;background-color:#c7ffff}
    button{border:25px solid red;box-sizing: border-box;}
    #b1:active{transform:scale(0.4)}
    #b2:active{transform:scale(0.95)}
    <!-- Click on the border on each one of then-->
    <!-- Then click on the center, and it works in both-->
    <button id="b1">Click Me</button>
    <button id="b2">Click Me x2</button>
    1 回复  |  直到 6 年前
        1
  •  0
  •   Pete Irfan TahirKheli    6 年前

    问题是div似乎丢失了click事件,因为活动按钮现在太小,因此click不再在按钮内(尝试较大的按钮编号,但单击变为白色的位,您将看到它有相同的问题)。

    https://developer.mozilla.org/en-US/docs/Web/Events/click - 当在单个元素上按下并释放定点设备按钮(通常是鼠标的主按钮)时,将触发click事件。

    这意味着当按钮被释放时,由于鼠标已缩小而不再位于按钮中,该按钮的单击事件将不会触发

    使用mousedown来捕获事件怎么样:

    document.querySelectorAll("button").forEach((itm) => {
      itm.addEventListener("mousedown", function() {
        console.log("press"); // changed to console so you can see it fire in the snippet as well as the animation taking place (which wuldn't happen with an alert)
      });
    })
    button {
      cursor: pointer;
      user-select: none;
      outline: none;
      background-color: #c7ffff
    }
    
    button {
      border: 25px solid red;
      box-sizing: border-box;
    }
    
    #b1:active {
      transform: scale(0.4)
    }
    
    #b2:active {
      transform: scale(0.95)
    }
    <!-- Click on the border on each one of then-->
    <!-- Then click on the center, and it works in both-->
    <button id="b1">Click Me</button>
    <button id="b2">Click Me x2</button>