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>
posted @ 2021-07-26 17:08  六石  阅读(182)  评论(0编辑  收藏  举报