【问题标题】:If I add a second ''item'', the 2nd "item" can no longer move如果我添加第二个“项目”,则第二个“项目”不能再移动
【发布时间】:2021-07-01 21:50:42
【问题描述】:

由于这段代码,一个球上出现了一个应用程序。那个球可以在桌面和移动设备上移动。一旦添加了第二个球(我添加了<div id="item" ondblclick="openWindow()"><img src="chrome.png" id="chrome"></div>),第二个球就无法移动。我已经尝试过复制和粘贴具有唯一 ID 的函数,但这不起作用。我还能做些什么来完成这项工作?

一个球的应用示例: https://gyazo.com/5576019e3fbb0e272b689b639449dbea

两个球的应用示例: https://gyazo.com/7bf718f244a268ab3a0f246e0aaf7e38

<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"/>
  <title>Drag/Drop/Bounce</title>
  <style>
    #container {
      width: 100%;
      height: 100vh;
      background-color: white;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      touch-action: none;
    }

    #item {
      width: 100px;
      height: 100px;
      background-color: rgb(245, 230, 99);
      border: 10px solid rgba(136, 136, 136, .5);
      border-radius: 50%;
      touch-action: none;
      user-select: none;
    }

    #item:active {
      background-color: rgba(168, 218, 220, 1.00);
    }

    #item:hover {
      cursor: pointer;
    }

    #chrome {
      width: 50px;
      display: block;
      margin-top: 25px;
      margin-right: 25px;
      margin-bottom: 25px;
      margin-left: 25px;
    }

  </style>
</head>

<body>

  <div id="outerContainer">
    <div id="container">
      <div id="item" ondblclick="openWindow()"><img src="chrome.png" id="chrome"></div>
    </div>
  </div>

  

  <script>

    // const card = document.querySelector('aside');

    // card.addEventListener('dblclick', function openWindow() {
    // var win = window.open('popupdiscord.html', 'New Window','width=900,height=900,top=70,left=500,resizable=0,menubar=yes');
    // win.focus();
    // });
    

      function openWindow() {
      var win = window.open('popupdiscord.html', 'New Window','width=900,height=900,top=70,left=500,resizable=0,menubar=yes');
      win.focus();
     }
    
    var dragItem = document.querySelector("#item");
    var container = document.querySelector("#container");

    var active = false;
    var currentX;
    var currentY;
    var initialX;
    var initialY;
    var xOffset = 0;
    var yOffset = 0;

    container.addEventListener("touchstart", dragStart, false);
    container.addEventListener("touchend", dragEnd, false);
    container.addEventListener("touchmove", drag, false);

    container.addEventListener("mousedown", dragStart, false);
    container.addEventListener("mouseup", dragEnd, false);
    container.addEventListener("mousemove", drag, false);

    function dragStart(e) {
      if (e.type === "touchstart") {
        initialX = e.touches[0].clientX - xOffset;
        initialY = e.touches[0].clientY - yOffset;
      } else {
        initialX = e.clientX - xOffset;
        initialY = e.clientY - yOffset;
      }

      if (e.target === dragItem) {
        active = true;
      }
    }

    function dragEnd(e) {
      initialX = currentX;
      initialY = currentY;

      active = false;
    }

    function drag(e) {
      if (active) {
      
        e.preventDefault();
      
        if (e.type === "touchmove") {
          currentX = e.touches[0].clientX - initialX;
          currentY = e.touches[0].clientY - initialY;
        } else {
          currentX = e.clientX - initialX;
          currentY = e.clientY - initialY;
        }

        xOffset = currentX;
        yOffset = currentY;

        setTranslate(currentX, currentY, dragItem);
      }
    }

    function setTranslate(xPos, yPos, el) {
      el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
    }
  </script>
</body>

</html>

【问题讨论】:

  • 如果您添加第二个具有相同 ID 的球 #item 当然事情不会像预期的那样工作,因为您正在复制 ID。
  • @Terry 我该如何解决这个问题?

标签: javascript html css unique


【解决方案1】:

使用class 代替id。选择该类的所有元素(此处为item)。并使用循环为每个项目运行代码。

?Codepen:

function openWindow() {
  var win = window.open(
    "popupdiscord.html",
    "New Window",
    "width=900,height=900,top=70,left=500,resizable=0,menubar=yes"
  );
  win.focus();
}

var dragItem = document.querySelectorAll(".item");
var container = document.querySelector("#container");

const drag = (item) => {
  var active = false;
  var currentX;
  var currentY;
  var initialX;
  var initialY;
  var xOffset = 0;
  var yOffset = 0;

  function dragStart(e) {
    if (e.type === "touchstart") {
      initialX = e.touches[0].clientX - xOffset;
      initialY = e.touches[0].clientY - yOffset;
    } else {
      initialX = e.clientX - xOffset;
      initialY = e.clientY - yOffset;
    }

    if (e.target === item) {
      active = true;
    }
  }

  function dragEnd(e) {
    initialX = currentX;
    initialY = currentY;

    active = false;
  }

  function drag(e) {
    if (active) {
      e.preventDefault();

      if (e.type === "touchmove") {
        currentX = e.touches[0].clientX - initialX;
        currentY = e.touches[0].clientY - initialY;
      } else {
        currentX = e.clientX - initialX;
        currentY = e.clientY - initialY;
      }

      xOffset = currentX;
      yOffset = currentY;

      setTranslate(currentX, currentY, item);
    }
  }

  function setTranslate(xPos, yPos, el) {
    el.style.transform = "translate3d(" + xPos + "px, " + yPos + "px, 0)";
  }

  container.addEventListener("touchstart", dragStart, false);
  container.addEventListener("touchend", dragEnd, false);
  container.addEventListener("touchmove", drag, false);

  container.addEventListener("mousedown", dragStart, false);
  container.addEventListener("mouseup", dragEnd, false);
  container.addEventListener("mousemove", drag, false);
};

dragItem.forEach(drag);
#container {
  width: 100%;
  height: 100vh;
  background-color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  touch-action: none;
}

.item {
  width: 100px;
  height: 100px;
  background-color: rgb(245, 230, 99);
  border: 10px solid rgba(136, 136, 136, 0.5);
  border-radius: 50%;
  touch-action: none;
  user-select: none;
}

.item:active {
  background-color: rgba(168, 218, 220, 1);
}

.item:hover {
  cursor: pointer;
}

.chrome {
  width: 50px;
  display: block;
  margin-top: 25px;
  margin-right: 25px;
  margin-bottom: 25px;
  margin-left: 25px;
}
<div id="outerContainer">
  <div id="container">
    <div class="item" ondblclick="openWindow()">
      <img src="https://i.stack.imgur.com/esHFl.png" class="chrome">
    </div>
    <div class="item" ondblclick="openWindow()">
      <img src="https://i.stack.imgur.com/esHFl.png" class="chrome">
    </div>
    <div class="item" ondblclick="openWindow()">
      <img src="https://i.stack.imgur.com/esHFl.png" class="chrome">
    </div>
  </div>
</div>

现在,您可以移动它们了。

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-11
    • 2020-03-07
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    相关资源
    最近更新 更多