当 mousedown 处于活动状态时让元素跟随光标

Have elements follow cursor while mousedown is active

我有几个图片元素在 mousemove 事件中跟随鼠标光标。 该脚本非常简单,效果很好,但我希望它只在按下鼠标时 运行,这样你就有一种在四处拖动这些元素的感觉。

我已将初始脚本放入函数中,当 mousedown 处于活动状态并且您开始拖动时会调用它,但它存在错误。现在,当您按下鼠标并拖动时,它不会开始跟随您,直到您停止拖动然后它开始并且永远不会停止。

有人可以帮我解决这个问题吗?

这里是 JSfiddle 你可以看到它在工作,但不是预期的那样,下面是完整的代码。

HTML

<div class="container">
  <h2 class="object" data-value="3">Space<br><span>Background</span></h2>
  <img src="1.png" class="object" data-value="-2" alt="">
  <img src="2.png" class="object" data-value="6" alt="">
  <img src="3.png" class="object" data-value="4" alt="">
  <img src="4.png" class="object" data-value="-6" alt="">
  <img src="5.png" class="object" data-value="8" alt="">
  <img src="6.png" class="object" data-value="-4" alt="">
  <img src="7.png" class="object" data-value="5" alt="">
  <img src="8.png" class="object" data-value="-9" alt="">
  <img src="9.png" class="object" data-value="-5" alt="">
</div>

CSS

.container{
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
    -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none; 
  -ms-user-select: none; 
  user-select: none; 
}

.container img{
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  object-fit: contain;
}

.container h2{
  z-index: 1;
  position: relative;
  color: #fff;
  font-size: 90px;
  text-transform: uppercase;
  font-weight: 900;
  letter-spacing: 32px;
  line-height: 60px;
}

.container h2 span{
  font-size: 48px;
  font-weight: 500;
  letter-spacing: 10px;
}

@media (max-width:800px){
  .container h2{
    font-size: 60px;
    letter-spacing: 19px;
    line-height: 35px;
  }

  .container h2 span{
    font-size: 26px;
  }
}

Javascript

function movingObjects() {
  document.addEventListener("mousemove", parallax);
  function parallax(e){
    document.querySelectorAll(".object").forEach(function(move){

      var moving_value = move.getAttribute("data-value");
      var x = (e.clientX * moving_value) / 250;
      var y = (e.clientY * moving_value) / 250;

      move.style.transform = "translateX(" + x + "px) translateY(" + y + "px)";
    });
  }
}

$(".container").mousedown(function () {
  $(this).mousemove(function () {
    movingObjects();
  });
}).mouseup(function () {
  $(this).unbind('mousemove');
}).mouseout(function () {
  $(this).unbind('mousemove');
});

您的代码就快完成了,只有两个问题需要解决。

首先,您需要知道鼠标何时被按住并移动。嵌套事件不是执行此操作的最佳方法,实际上会导致问题。同样,binding/unbinding 运行时事件变得难以维护并导致其他问题。

完成所需操作的最简单方法是使用布尔变量来指示鼠标按钮是否被按住。它在 mousedownmouseup 和其他相关事件上设置。然后,您可以在 parallax() 函数中使用它来检查处理是否应该继续。

第二个问题是,因为 img 元素呈现在所有其他元素之上,并填充了完整的 .container 元素,所以它们拦截了鼠标 click/drag 事件。要停止此操作,请在 CSS.

中对他们应用 pointer-events: none

有了这些更正,代码就可以工作了:

function parallax(e) {
  document.querySelectorAll(".object").forEach(function(move) {
    var moving_value = move.dataset.value;
    var x = (e.clientX * moving_value) / 250;
    var y = (e.clientY * moving_value) / 250;
    move.style.transform = `translateX(${x}px) translateY(${y}px)`;
  });
}

let mousedown = false;

$('body').on({
  mousedown: () => mousedown = true,
  mouseup: () => mousedown = false,
  mouseleave: () => mousedown = false,
  mousemove: e => mousedown && parallax(e)
});
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: "Segoe UI", sans-serif;
  background: #666 url(bg.png) no-repeat;
  background-size: cover;
  height: 100vh;
}

.container {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  user-select: none;
}

.container img {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  object-fit: contain;
  pointer-events: none;
}

.container h2 {
  z-index: 1;
  position: relative;
  color: #fff;
  font-size: 90px;
  text-transform: uppercase;
  font-weight: 900;
  letter-spacing: 32px;
  line-height: 60px;
}

.container h2 span {
  font-size: 48px;
  font-weight: 500;
  letter-spacing: 10px;
}

@media (max-width:800px) {
  .container h2 {
    font-size: 60px;
    letter-spacing: 19px;
    line-height: 35px;
  }
  .container h2 span {
    font-size: 26px;
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div class="container">
  <h2 class="object" data-value="3">Space<br><span>Background</span></h2>
  <img src="https://i.imgur.com/5yrwyPE.png" class="object" data-value="-2" alt="">
  <img src="https://i.imgur.com/C688O6f.png" class="object" data-value="6" alt="">
  <img src="https://i.imgur.com/zQMI9zR.png" class="object" data-value="4" alt="">
  <img src="https://i.imgur.com/JSzKUzo.png" class="object" data-value="-6" alt="">
  <img src="https://i.imgur.com/dxgHvQV.png" class="object" data-value="8" alt="">
  <img src="https://i.imgur.com/kbNROja.png" class="object" data-value="-4" alt="">
  <img src="https://i.imgur.com/2Ud0NwP.png" class="object" data-value="5" alt="">
  <img src="https://i.imgur.com/c5BGOj2.png" class="object" data-value="-9" alt="">
  <img src="https://i.imgur.com/Qxs8luV.png" class="object" data-value="-5" alt="">
</div>