d3拖拽

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .container { width: 600px; height: 600px; background-color: aquamarine; position: relative; } .a { width: 100px; height: 100px; background-color: yellowgreen; position: absolute; } .dragging { background-color: red; } </style> </head> <body> <div class='container' id='container'> <div class="a"></div> </div> <script src="https://d3js.org/d3.v7.min.js"></script> <script> d3.selectAll(".a").call( d3.drag() .on('start', function(d) { d3.select(this).classed("dragging", true) console.log('start') }) .on("end", function (d) { d3.select(this).classed("dragging", false) console.log("end"); }) .on('drag', function(d) { console.log(d3.select(this)) d3.select(this) .style("left", function() { return d.x + 'px' }) .style("top", function() { return d.y + 'px' }); }) ) </script> </body> </html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章