好的,所以我在为电子商务网站制作东西时遇到了一个问题。
基本上,这是一个屏幕,用户可以将小按钮拖到他们在前一个屏幕上选择的手镯上。他们将纽扣拖到手镯上,一旦他们喜欢这种组合,他们应该能够一次购买所有 4 件物品(1 个手镯,3 个纽扣)。
这是一个 jsFiddle 它的基本工作原理:http: //jsfiddle.net/6xRXj/3/
问题来了,当我需要知道手镯上有哪些按钮,哪些没有时。有没有办法通过拖放来做到这一点?
任何帮助是极大的赞赏。
下面是来自 jsFiddle 的代码...
HTML:
<div id="bracelet">
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
</div>
<br/>
<br/>
<br/>
<br/>
<br/>
<div id="div2" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag1" src="http://upload.wikimedia.org/wikipedia/commons/4/4f/Button-Red.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div3" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag2" src="http://upload.wikimedia.org/wikipedia/commons/a/a8/Button-Blue.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div4" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag3" src="http://upload.wikimedia.org/wikipedia/commons/c/ca/Button-Lightblue.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div5" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag4" src="http://upload.wikimedia.org/wikipedia/commons/b/ba/Button-Purple.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div6" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag5" src="http://upload.wikimedia.org/wikipedia/commons/6/68/Button-Orange.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div7" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag6" src="http://upload.wikimedia.org/wikipedia/commons/d/dc/Button-Green.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div8" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag7" src="http://upload.wikimedia.org/wikipedia/commons/4/4f/Button-Red.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div9" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag8" src="http://upload.wikimedia.org/wikipedia/commons/a/a8/Button-Blue.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div10" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag9" src="http://upload.wikimedia.org/wikipedia/commons/c/ca/Button-Lightblue.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div11" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag10" src="http://upload.wikimedia.org/wikipedia/commons/b/ba/Button-Purple.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div12" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag11" src="http://upload.wikimedia.org/wikipedia/commons/6/68/Button-Orange.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
<div id="div13" class="drop" ondrop="drop(event)" ondragover="allowDrop(event)">
<img id="drag12" src="http://upload.wikimedia.org/wikipedia/commons/d/dc/Button-Green.svg" ondragend="dragEnd(event)" draggable="true" ondragstart="drag(event)" width="50" height="50">
</div>
CSS:
#div1 {
width:50px;
height:50px;
padding:10px;
border:1px solid #aaaaaa;
display: inline;
float: left;
margin-bottom: 25px;
margin-left: 50px
}
.drop {
width:50px;
height:50px;
padding:10px;
border:1px solid #aaaaaa;
display: inline;
float: left;
}
#bracelet {
width:420px;
height:75px;
padding: 10px;
margin-left: 30px;
border:1px solid #aaaaaa;
background-color: gray;
}
JS:
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
var el = ev.target;
var parent = el.getAttribute("data-parent");
if(!parent){
el.setAttribute("data-parent", el.parentNode.id);
}
ev.dataTransfer.setData("Text", el.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
}
function dragEnd(ev){
if(ev.dataTransfer.dropEffect == "none"){
var parent = document.getElementById(ev.target.getAttribute("data-parent"));
parent.appendChild(ev.target);
}
}