如何修改此 HTML 和 JavaScript,以便在网页上提供多个可移动项目?

我在网上找到了这段代码,它创建了一个可在屏幕上拖动的“卡片”。


我想添加更多卡。我尝试简单地制作更多的卡片,但这让我得到的卡片根本不动。


<!DOCTYPE html>


<html>


 <style>

body {

  background-image: url('background1.jpg');

  background-repeat: no-repeat;

  background-attachment: fixed;

  background-size: cover;

}

</style> 



<style>

#mydiv {

  position: absolute;

  z-index: 9;

  background-color: #f1f1f1;

  text-align: center;

  border: 1px solid #d3d3d3;

}


#mydivheader {

  padding: 10px;

  cursor: move;

  z-index: 10;

  background-color: #2196F3;

  color: #fff;

}

</style>

<body>


<h1>Fun</h1>


<p>Click to move the card around</p>


<div id="mydiv">

  <div id="mydivheader">Click here</div>

  <p>This is a card with stuff on it.</p>

</div>



<script>

//Make the DIV element draggagle:

dragElement(document.getElementById("mydiv"));


function dragElement(elmnt) {

  var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;

  if (document.getElementById(elmnt.id + "header")) {

    /* if present, the header is where you move the DIV from:*/

    document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;

  } else {

    /* otherwise, move the DIV from anywhere inside the DIV:*/

    elmnt.onmousedown = dragMouseDown;

  }


  function dragMouseDown(e) {

    e = e || window.event;

    e.preventDefault();

    // get the mouse cursor position at startup:

    pos3 = e.clientX;

    pos4 = e.clientY;

    document.onmouseup = closeDragElement;

    // call a function whenever the cursor moves:

    document.onmousemove = elementDrag;

  }


  function elementDrag(e) {

    e = e || window.event;

    e.preventDefault();

    // calculate the new cursor position:

    pos1 = pos3 - e.clientX;

    pos2 = pos4 - e.clientY;

    pos3 = e.clientX;

    pos4 = e.clientY;

    // set the element's new position:

    elmnt.style.top = (elmnt.offsetTop - pos2) + "px";

    elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";

  }


  function closeDragElement() {

    /* stop moving when mouse button is released:*/

    document.onmouseup = null;

    document.onmousemove = null;

  }

}

</script>


</body>

</html>

如何添加不止一张可移动的卡?


jeck猫
浏览 140回答 3
3回答

SMILET

仅仅复制 html 的问题是,您还需要复制 css 和 javascript。最重要的是,您需要更改新元素的 id,否则一切都会变得一团糟。这个例子不会教你好的代码,而是回答你的问题://Make the DIV element draggagle:dragElement(document.getElementById("mydiv"));dragElement(document.getElementById("mydiv2"));function dragElement(elmnt) {&nbsp; var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;&nbsp; if (document.getElementById(elmnt.id + "header")) {&nbsp; &nbsp; /* if present, the header is where you move the DIV from:*/&nbsp; &nbsp; document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;&nbsp; } else {&nbsp; &nbsp; /* otherwise, move the DIV from anywhere inside the DIV:*/&nbsp; &nbsp; elmnt.onmousedown = dragMouseDown;&nbsp; }&nbsp; function dragMouseDown(e) {&nbsp; &nbsp; e = e || window.event;&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; // get the mouse cursor position at startup:&nbsp; &nbsp; pos3 = e.clientX;&nbsp; &nbsp; pos4 = e.clientY;&nbsp; &nbsp; document.onmouseup = closeDragElement;&nbsp; &nbsp; // call a function whenever the cursor moves:&nbsp; &nbsp; document.onmousemove = elementDrag;&nbsp; }&nbsp; function elementDrag(e) {&nbsp; &nbsp; e = e || window.event;&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; // calculate the new cursor position:&nbsp; &nbsp; pos1 = pos3 - e.clientX;&nbsp; &nbsp; pos2 = pos4 - e.clientY;&nbsp; &nbsp; pos3 = e.clientX;&nbsp; &nbsp; pos4 = e.clientY;&nbsp; &nbsp; // set the element's new position:&nbsp; &nbsp; elmnt.style.top = (elmnt.offsetTop - pos2) + "px";&nbsp; &nbsp; elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";&nbsp; }&nbsp; function closeDragElement() {&nbsp; &nbsp; /* stop moving when mouse button is released:*/&nbsp; &nbsp; document.onmouseup = null;&nbsp; &nbsp; document.onmousemove = null;&nbsp; }}#mydiv {&nbsp; position: absolute;&nbsp; z-index: 9;&nbsp; background-color: #f1f1f1;&nbsp; text-align: center;&nbsp; border: 1px solid #d3d3d3;}#mydivheader {&nbsp; padding: 10px;&nbsp; cursor: move;&nbsp; z-index: 10;&nbsp; background-color: #2196F3;&nbsp; color: #fff;}#mydiv2 {&nbsp; position: absolute;&nbsp; z-index: 9;&nbsp; background-color: #f1f1f1;&nbsp; text-align: center;&nbsp; border: 1px solid #d3d3d3;}#mydivheader2 {&nbsp; padding: 10px;&nbsp; cursor: move;&nbsp; z-index: 10;&nbsp; background-color: #2196F3;&nbsp; color: #fff;}<h1>Fun</h1><p>Click to move the card around</p><div id="mydiv">&nbsp; <div id="mydivheader">Click here</div>&nbsp; <p>This is a card with stuff on it.</p></div><div id="mydiv2">&nbsp; <div id="mydivheader2">Click here</div>&nbsp; <p>This is a card with stuff on it.</p></div>但您最好使用基于类的脚本。这将为您提供更少的冗余代码。

ABOUTYOU

查看代码,我们会得到它<html>&nbsp;<style>body {&nbsp; background-image: url('background1.jpg');&nbsp; background-repeat: no-repeat;&nbsp; background-attachment: fixed;&nbsp; background-size: cover;}</style>&nbsp;<style>#mydiv,#mydiv1,#mydiv2 {&nbsp; position: absolute;&nbsp; z-index: 9;&nbsp; background-color: #f1f1f1;&nbsp; text-align: center;&nbsp; border: 1px solid #d3d3d3;}#mydivheader,#mydivheader1,#mydivheader2 {&nbsp; padding: 10px;&nbsp; cursor: move;&nbsp; z-index: 10;&nbsp; background-color: #2196F3;&nbsp; color: #fff;}</style><body><h1>Fun</h1><p>Click to move the card around</p><div id="mydiv" onmousedown="dragElement(this)">&nbsp; <div id="mydivheader">Click here</div>&nbsp; <p>This is a card with stuff on it.</p></div><br/><div id="mydiv1" onmousedown="dragElement(this)">&nbsp; <div id="mydivheader1">Click here</div>&nbsp; <p>This is a card with stuff on it.</p></div><br/><div id="mydiv2" onmousedown="dragElement(this)">&nbsp; <div id="mydivheader2">Click here</div>&nbsp; <p>This is a card with stuff on it.</p></div>//Make the DIV element draggagle://dragElement(document.getElementById("mydiv"));function dragElement(elmnt) {&nbsp; var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;&nbsp; if (document.getElementById(elmnt.id + "header")) {&nbsp; &nbsp; /* if present, the header is where you move the DIV from:*/&nbsp; &nbsp; document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;&nbsp; } else {&nbsp; &nbsp; /* otherwise, move the DIV from anywhere inside the DIV:*/&nbsp; &nbsp; elmnt.onmousedown = dragMouseDown;&nbsp; }&nbsp; function dragMouseDown(e) {&nbsp; &nbsp; e = e || window.event;&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; // get the mouse cursor position at startup:&nbsp; &nbsp; pos3 = e.clientX;&nbsp; &nbsp; pos4 = e.clientY;&nbsp; &nbsp; document.onmouseup = closeDragElement;&nbsp; &nbsp; // call a function whenever the cursor moves:&nbsp; &nbsp; document.onmousemove = elementDrag;&nbsp; }&nbsp; function elementDrag(e) {&nbsp; &nbsp; e = e || window.event;&nbsp; &nbsp; e.preventDefault();&nbsp; &nbsp; // calculate the new cursor position:&nbsp; &nbsp; pos1 = pos3 - e.clientX;&nbsp; &nbsp; pos2 = pos4 - e.clientY;&nbsp; &nbsp; pos3 = e.clientX;&nbsp; &nbsp; pos4 = e.clientY;&nbsp; &nbsp; // set the element's new position:&nbsp; &nbsp; elmnt.style.top = (elmnt.offsetTop - pos2) + "px";&nbsp; &nbsp; elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";&nbsp; }&nbsp; function closeDragElement() {&nbsp; &nbsp; /* stop moving when mouse button is released:*/&nbsp; &nbsp; document.onmouseup = null;&nbsp; &nbsp; document.onmousemove = null;&nbsp; }}#mydiv,#mydiv1,#mydiv2 {&nbsp; position: absolute;&nbsp; z-index: 9;&nbsp; background-color: #f1f1f1;&nbsp; text-align: center;&nbsp; border: 1px solid #d3d3d3;}#mydivheader,#mydivheader1,#mydivheader2 {&nbsp; padding: 10px;&nbsp; cursor: move;&nbsp; z-index: 10;&nbsp; background-color: #2196F3;&nbsp; color: #fff;}<!DOCTYPE html><html><body><h1>Fun</h1><p>Click to move the card around</p><div id="mydiv" onmousedown="dragElement(this)">&nbsp; <div id="mydivheader">Click here</div>&nbsp; <p>This is a card with stuff on it.</p></div><br/><div id="mydiv1" onmousedown="dragElement(this)">&nbsp; <div id="mydivheader1">Click here</div>&nbsp; <p>This is a card with stuff on it.</p></div><br/><div id="mydiv2" onmousedown="dragElement(this)">&nbsp; <div id="mydivheader2">Click here</div>&nbsp; <p>This is a card with stuff on it.</p></div></body></html><script>//Make the DIV element draggagle://dragElement(document.getElementById("mydiv"));your all script no change</script>

森林海

首先,更改 CSS,以便使用通用类,而不是 ID:&nbsp;.my-div-wrapper {&nbsp; &nbsp; position: absolute;&nbsp; &nbsp; z-index: 9;&nbsp; &nbsp; background-color: #f1f1f1;&nbsp; &nbsp; text-align: center;&nbsp; &nbsp; border: 1px solid #d3d3d3;}.div-header {&nbsp; &nbsp; padding: 10px;&nbsp; &nbsp; cursor: move;&nbsp; &nbsp; z-index: 10;&nbsp; &nbsp; background-color: #2196F3;&nbsp; &nbsp; color: #fff;}然后,根据脚本将这些类以及适当的 ID(`#mydivtwo 等)应用到您的两张(或其他)卡片上:<div id="mydiv" class="my-div-wrapper">&nbsp; &nbsp; <div id="mydivheader" class="div-header">Click here</div>&nbsp; &nbsp; <p>This is a card with stuff on it.</p></div><div id="mydivtwo" class="my-div-wrapper">&nbsp; &nbsp; <div id="mydivtwoheader" class="div-header">Click here</div>&nbsp; &nbsp; <p>This is a card with stuff on it.</p></div>然后,只需在相关元素上调用相关函数即可:&nbsp; dragElement(document.getElementById("mydiv"));&nbsp; dragElement(document.getElementById("mydivtwo"));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript