var svg = d3.selectAll("body").append("svg").attr("width", "100%").attr("height", "100%");
var data = [ {gind:0, class:"Z",elements:[]},{gind:1,class:"A",elements:[1,2,3]}, {gind:2,class:"B",elements:[1,2,3]} , {gind:3,class:"C",elements:[1,2,3]} ]
var groups = svg.selectAll("gs").data(data).enter().append("g").attr("class",function(d){ return d.class; }).attr("transform",function(d){ return "translate("+d.gind*50+","+d.gind*50+")"});
var elements = groups.selectAll(".element").data(function(d){ return d.elements.map(function(i){ return { id:i , x:i*10, y:i*10 } }); }).enter().append("rect").attr("class","element").attr("transform",function(d){
console.log(d)
return "translate("+d.x+","+d.y+")"
}).attr("width",10).attr("height",10).style("fill","#cccccc")
var draghandle = d3.behavior.drag().origin(Object).on("drag", dragfunc).on("dragstart", dragstart).on("dragend", dragend);
var dragging_elem = false;
elements.on("mouseover",function(d){
\t d3.select(this).style("fill","red");
}).on("mouseout",function(d){ d3.select(this).style("fill","#cccccc") }).call(draghandle)
var prevParent;
var z = document.getElementsByClassName("Z")[0];
function dragstart(d) {
prevParent = d3.select(this).node().parentNode;
d3.select(z).attr("transform", prevParent.getAttribute("transform"));
z.appendChild(d3.select(this).node());
}
function dragend(d) {
prevParent.appendChild(d3.select(this).node());
}
function dragfunc(d){ \t
d.x = d3.event.x
d.y = d3.event.y
d3.select(this).attr("transform","translate("+d3.event.x+","+d3.event.y+")");
}
html, body {
width: 100%;
height: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
在黑暗中只是一個嘗試,但你試圖改變你的'' 標籤' '? –
xdhmoore
同樣,如果您使用的是html minifier,它可能會爲您做上述操作並導致問題,但我不會指望出現問題。 – xdhmoore
@xdhmoore爲了簡潔起見,我做到了。我使用D3來創建所有這些。 – wolfrevo