使用 jQueryUI 和视差效果拖动元素

Dragging elements with jQueryUI and with parallax effect

当使用 jQueryUI 单击鼠标时,我可以在容器周围拖动元素。我也想移动整个容器,但是当我引入 draggable 时,第一个效果停止工作。

Here it is第一个效果生效(点击鼠标拖拽一下就生效) 和 once I introduce draggable 使用 jQueryUI,我只有可拖动效果起作用。

有没有办法让两种效果同时起作用?

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)
});

$('.container').draggable({
  scroll: false,
  containment: '#grid'
});
* {
  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;
}

#grid {
  position: fixed;
  width: 120%;
  height: 120%;
  left: -10%;
  top: -10%;
}

.container {
  position: relative;
  width: 90%;
  height: 90%;
  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.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div id="grid">
  <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="2" alt="">
    <img src="https://i.imgur.com/zQMI9zR.png" class="object" data-value="1" alt="">
    <img src="https://i.imgur.com/JSzKUzo.png" class="object" data-value="-3" alt="">
    <img src="https://i.imgur.com/dxgHvQV.png" class="object" data-value="3" alt="">
    <img src="https://i.imgur.com/kbNROja.png" class="object" data-value="-2" alt="">
    <img src="https://i.imgur.com/2Ud0NwP.png" class="object" data-value="1" alt="">
    <img src="https://i.imgur.com/c5BGOj2.png" class="object" data-value="-3" alt="">
    <img src="https://i.imgur.com/Qxs8luV.png" class="object" data-value="0" alt="">
  </div>
</div>

好吧,我只是注意到我的代码确实有效,只是第一个效果不够强烈,无法在第二个效果开始时注意到,哈哈。

还是有一些问题,比如第二次点击拖动的时候,是从之前的位置开始...

无论如何,如果有人需要这个,只需增加 data-value 上的数字即可。

听起来您已经有了答案,您可以考虑以下内容。

$(function() {
  function parallax(e) {
    $(".object").each(function(i, 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)`;
    });
  }

  $('.container').draggable({
    scroll: false,
    containment: '#grid',
    drag: function(event, ui) {
      parallax(event);
    }
  });
});
* {
  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;
}

#grid {
  position: fixed;
  width: 120%;
  height: 120%;
  left: -10%;
  top: -10%;
}

.container {
  position: relative;
  width: 90%;
  height: 90%;
  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.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div id="grid">
  <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="2" alt="">
    <img src="https://i.imgur.com/zQMI9zR.png" class="object" data-value="1" alt="">
    <img src="https://i.imgur.com/JSzKUzo.png" class="object" data-value="-3" alt="">
    <img src="https://i.imgur.com/dxgHvQV.png" class="object" data-value="3" alt="">
    <img src="https://i.imgur.com/kbNROja.png" class="object" data-value="-2" alt="">
    <img src="https://i.imgur.com/2Ud0NwP.png" class="object" data-value="1" alt="">
    <img src="https://i.imgur.com/c5BGOj2.png" class="object" data-value="-3" alt="">
    <img src="https://i.imgur.com/Qxs8luV.png" class="object" data-value="0" alt="">
  </div>
</div>

这利用了 drag 回调,仅在拖动元素(mousedown 和 mousemove)时执行。