本文实例为大家分享了js实现拖拽拼图游戏的具体代码,供大家参考,具体内容如下
该游戏主要使用了一些拖拽事件,以及对数据传递的应用,直接上代码,感兴趣的可以参考
html:代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
< div class = "box" > < div id = "d1" class = "d1" ></ div > < div id = "d2" class = "d1" ></ div > < div id = "d3" class = "d1" ></ div > < div id = "d4" class = "d1" ></ div > < div id = "d5" class = "d1" ></ div > < div id = "d6" class = "d1" ></ div > < div id = "d7" class = "d1" ></ div > < div id = "d8" class = "d1" ></ div > < div id = "d9" class = "d1" ></ div > </ div > < div id = "but" > < button id = "but1" >一键完成</ button > < button id = "but2" >开始游戏</ button > < button id = "but3" >看一眼原图</ button > < div > < img id = "yan" src = "../../图片/2222.jpg" alt = "" > </ div > </ div > |
css代码:
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
|
* { margin : 0 ; padding : 0 ; } .box { width : 312px ; height : 312px ; border : 3px solid #000 ; margin : 50px auto 5px ; font-size : 0 ; } .box div { width : 100px ; height : 100px ; display : inline- block ; border : 2px solid #000 ; } .d 1 { background-image : url (../../图片/ 2222 .jpg); background- size : 300px 300px ; background-position : 0px 0px ; } #but { border : 1px solid #000 transparent ; width : 300px ; height : 30px ; margin : 0 auto ; } #but img { width : 100px ; height : 100px ; float : right ; display : none ; } button { margin : 1px auto ; border : 1px solid #000 ; } |
js代码:
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
|
var data = [[ '0 0' ], [ '-100px 0' ], [ '-200px 0' ], [ '0 -100px' ], [ '-100px -100px' ], [ '-200px -100px' ], [ '0 -200px' ], [ '-100px -200px' ], [ '-200px -200px' ]] var but1 = document.getElementById( "but1" ) var but2 = document.getElementById( "but2" ) var but3 = document.getElementById( "but3" ) var yan = document.getElementById( "yan" ) var divs = document.querySelectorAll( ".d1" ) // 刚打开保持完整 for ( var i = 0; i < divs.length; i++) { divs[i].style.backgroundPosition = data[i][0] } // 一键完成 but1.addEventListener( "click" , function () { for ( var i = 0; i < divs.length; i++) { divs[i].style.backgroundPosition = data[i][0] } }) // 开始游戏 but2.addEventListener( "click" , function () { var arr = []; var maxTimes = 9 do { var num = Math.floor(Math.random() * 9); if (-1 == arr.indexOf(num)) { arr.push(num); maxTimes--; } } while (maxTimes); for ( var i = 0; i < divs.length; i++) { const k = arr[i] divs[k].style.backgroundPosition = data[i][0] } }) // 看一眼原图 but3.addEventListener( "mousedown" , function () { yan.style.display = "block" }) but3.addEventListener( "mouseup" , function () { yan.style.display = "none" }) divs.forEach( function (v, i) { v.draggable = "true" //开始拖拽的时候触发事件 v.addEventListener( "dragstart" , function (e) { // console.log('666'); e.dataTransfer.setData( "newdivID" ,e.target.getAttribute( "id" )) e.dataTransfer.setData( "newdiv" ,e.target.style.backgroundPosition) }) // 拖拽松开的时候触发事件 v.addEventListener( "drop" , function (e) { e.stopPropagation() e.preventDefault() var oldDiv = document.querySelector( "#" +e.dataTransfer.getData( 'newdivID' )) //取出并保存开始拖拽的div的id属性的div var pos=e.dataTransfer.getData( "newdiv" ) // 取出并保存开始拖拽的div的backgroundPosition属性 oldDiv.style.backgroundPosition=e.target.style.backgroundPosition //把准备松开到的div的backgroundPosition属性值传递给取出的那个旧div e.target.style.backgroundPosition=pos // 把取出的那个旧div的backgroundPosition属性值传递给当前准备松开到的div }) v.addEventListener( "dragover" , function (e){ e.preventDefault() }) }) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/m0_54089303/article/details/120372327