wujingjing
2024-12-30 f12fd0b36f027cbbae0fb322b237abe857a32e2b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//---元素拖动插件
const dragging = (data) => {
    var $this = data.dom;
    var xPage;
    var yPage;
    var X; //
    var Y; //
    var xRand = 0; //
    var yRand = 0; //
    var father = getParent($this);
    var defaults = {
        move: 'both',
        randomPosition: true,
        hander: 1,
        notelement: '.bf-container',
    };
    var opt = Object.assign(defaults, data);
    var movePosition = opt.move;
    var random = opt.randomPosition;
 
    var hander = opt.hander;
    var notelement = opt.notelement;
    if (hander == 1) {
        hander = $this;
    } else {
        hander = document.querySelectorAll('.hander')[0];
    }
    // hander.setAttribute('style', 'cursor: move');
    //---初始化
    father.style.position = 'relative';
    father.style.overflow = 'hidden';
    $this.style.position = 'absolute';
    hander.style.cursor = 'move';
    var faWidth = father.clientWidth;
    var faHeight = father.clientHeight;
    var computedStyle = getComputedStyle($this);
    var thisWidth = $this.clientWidth + parseInt(computedStyle.paddingLeft) + parseInt(computedStyle.paddingRight);
    var thisHeight = $this.clientHeight + parseInt(computedStyle.paddingTop) + parseInt(computedStyle.paddingBottom);
 
    var mDown = false; //
    var positionX;
    var positionY;
    var moveX;
    var moveY;
 
    if (random) {
        thisRandom();
    }
    function thisRandom() {
        //随机函数
        this.forEach(function (elem) {
            var randY = Math.floor(Math.random() * (faHeight - thisHeight));
            var randX = Math.floor(Math.random() * (faWidth - thisWidth));
            if (movePosition.toLowerCase() === 'x') {
                elem.style.left = randX + 'px';
            } else if (movePosition.toLowerCase() === 'y') {
                elem.style.top = randY + 'px';
            } else if (movePosition.toLowerCase() === 'both') {
                elem.style.top = randY + 'px';
                elem.style.left = randX + 'px';
            }
        });
    }
    hander.addEventListener('mousedown', function (e) {
        // 假设 father 是你的父元素的 ID
        var children = father.children; // 获取所有子元素
        for (var i = 0; i < children.length; i++) {
            if (children[i] !== notelement) {
                children[i].style.zIndex = '1';
            }
        }
 
        $this.style.zIndex = '2';
        mDown = true;
        X = e.pageX;
        Y = e.pageY;
        var rect = $this.getBoundingClientRect();
        positionX = rect.left;
        positionY = rect.top;
        return false;
    });
    document.addEventListener('mouseup', function (e) {
        mDown = false;
    });
 
    document.addEventListener('mousemove', function (e) {
        xPage = e.pageX; //--
        moveX = positionX + xPage - X;
 
        yPage = e.pageY; //--
        moveY = positionY + yPage - Y;
 
        const thisXMove = () => {
            //x轴移动
            if (mDown) {
                $this.style.left = moveX + 'px';
            } else {
                return;
            }
 
            if (moveX < 0) {
                $this.style.left = '0';
            }
            if (moveX > faWidth - thisWidth) {
                $this.style.left = faWidth - thisWidth + 'px';
            }
 
            return moveX;
        };
 
        const thisYMove = () => {
            //y轴移动
            if (mDown) {
                $this.style.top = moveY + 'px';
            } else {
                return;
            }
 
            if (moveY < 0) {
                $this.style.top = '0';
            }
            if (moveY > faHeight - thisHeight) {
                $this.style.top = faHeight - thisHeight + 'px';
            }
 
            return moveY;
        };
 
        const thisAllMove = () => {
            //全部移动
            if (mDown) {
                $this.style.left = moveX - thisWidth + 'px';
                $this.style.top = moveY - thisHeight + 'px';
            } else {
                return;
            }
 
            if (moveX < 0) {
                $this.style.left = '0';
            }
            if (moveX > faWidth - thisWidth) {
                $this.style.left = faWidth - thisWidth + 'px';
            }
 
            if (moveY < 0) {
                $this.style.top = '0';
            }
            if (moveY > faHeight - thisHeight) {
                $this.style.top = faHeight - thisHeight + 'px';
            }
        };
        if (movePosition.toLowerCase() == 'x') {
            thisXMove();
        } else if (movePosition.toLowerCase() == 'y') {
            thisYMove();
        } else if (movePosition.toLowerCase() == 'both') {
            thisAllMove();
        }
    });
};
 
const getParent = (dom) => {
    return dom.parentNode;
};
export default dragging;