2023-09-30 19:09:58

103

Letöltés

huszárLépés

Fogd és vidd (Drag and Drop) funkció szemléltetése. A sakktáblán lévő huszár 'L' alakban a számára kijelölt helyekre mozgatható.

Szemléltetés

Fogd és vidd

js:

  1. const tabla =document.querySelector('#tabla')
  2. const info =document.querySelector('#info')
  3. const lo = document.querySelector('.babu')
  4. let megfogott
  5. let kezdoPoz
  6. function setTabla(){
  7. for(let i=8;i>0;i--){
  8. for(let index = 0; index < 8;index++){
  9. const negyzet = document.createElement('div')
  10. if(i % 2 ==0){
  11. if(index % 2 ==0 ){
  12. negyzet.classList.add('vilagos')
  13. }else{
  14. negyzet.classList.add('sotet')
  15. }
  16. }else{
  17. if(index % 2 ==0 ){
  18. negyzet.classList.add('sotet')
  19. }else{
  20. negyzet.classList.add('vilagos')
  21. }
  22. }
  23. negyzet.classList.add('negyzet')
  24. negyzet.id = String.fromCharCode(index+65) + (i)
  25. negyzet.addEventListener('dragover', felette)
  26. negyzet.addEventListener('drop', elenged)
  27. tabla.append(negyzet)
  28. if(negyzet.id == 'B1'){
  29. const lo1 = document.createElement('img')
  30. lo1.src="kepek/feherLo.png"
  31. lo1.alt="feherLo"
  32. lo1.id="feherLo1"
  33. lo1.name="huszár"
  34. lo1.classList.add('babu')
  35. lo1.classList.add("huszár")
  36. lo1.draggable="true"
  37. lo1.addEventListener('dragstart', megfog)
  38. negyzet.append(lo1)
  39. }
  40. }
  41. }
  42. }
  43. setTabla()
  44. const negyzetek=document.querySelectorAll('.negyzet')
  45. function megfog(e){
  46. megfogott = e.target
  47. info.textContent = (megfogott.name + " ")
  48. kezdoPoz= e.target.parentNode.id
  49. kiemel()
  50. }
  51. function felette(e){
  52. if( e.target.classList.contains("kiemelt") ){
  53. e.preventDefault();
  54. }
  55. }
  56. function elenged(e){
  57. e.target.append(megfogott) //kép áthelyezés
  58. info.textContent += e.target.id
  59. kiemelestMegszutet()
  60. }
  61. function kiemel(){
  62. //console.log("kiemel:" + megfogott.name + " " + kezdoPoz) // huszár B1
  63. negyzetek.forEach( (_mezo, index) =>{
  64. if(_mezo.id == String.fromCharCode(kezdoPoz.charCodeAt(0)+1) + ( parseInt(kezdoPoz.charAt(1)) + 2 )){_mezo.classList.add('kiemelt')}
  65. if(_mezo.id == String.fromCharCode(kezdoPoz.charCodeAt(0)+1) + ( parseInt(kezdoPoz.charAt(1)) - 2 )){_mezo.classList.add('kiemelt')}
  66. if(_mezo.id == String.fromCharCode(kezdoPoz.charCodeAt(0)-1) + ( parseInt(kezdoPoz.charAt(1)) + 2 )){_mezo.classList.add('kiemelt')}
  67. if(_mezo.id == String.fromCharCode(kezdoPoz.charCodeAt(0)-1) + ( parseInt(kezdoPoz.charAt(1)) - 2 )){_mezo.classList.add('kiemelt')}
  68. if(_mezo.id == String.fromCharCode(kezdoPoz.charCodeAt(0)+2) + ( parseInt(kezdoPoz.charAt(1)) + 1 )){_mezo.classList.add('kiemelt')}
  69. if(_mezo.id == String.fromCharCode(kezdoPoz.charCodeAt(0)+2) + ( parseInt(kezdoPoz.charAt(1)) - 1 )){_mezo.classList.add('kiemelt')}
  70. if(_mezo.id == String.fromCharCode(kezdoPoz.charCodeAt(0)-2) + ( parseInt(kezdoPoz.charAt(1)) + 1 )){_mezo.classList.add('kiemelt')}
  71. if(_mezo.id == String.fromCharCode(kezdoPoz.charCodeAt(0)-2) + ( parseInt(kezdoPoz.charAt(1)) - 1 )){_mezo.classList.add('kiemelt')}
  72. })
  73. }
  74. function kiemelestMegszutet(){
  75. negyzetek.forEach( (_mezo, index) =>{ _mezo.classList.remove('kiemelt') })
  76. }

css:

  1. body{
  2. display: flex;
  3. height: 100vh;
  4. justify-content: center;
  5. align-items: center;
  6. flex-direction: column;
  7. }
  8. #tabla {
  9. display: flex;
  10. flex-wrap: wrap;
  11. border: solid 3px black;
  12. width: 400px;
  13. height: 400px;
  14. }
  15. .negyzet{
  16. width: 50px;
  17. height: 50px;
  18. background-color: rgb(220, 220, 200);
  19. border: solid 1px black;
  20. box-sizing: border-box;
  21. display:flex;
  22. justify-content: center;
  23. align-items: center;
  24. }
  25. .vilagos{
  26. background-color: rgb(220, 220, 200);
  27. }
  28. .sotet{
  29. background-color: rgb(120, 120, 100);
  30. }
  31. .kiemelt{
  32. border: solid 3px green;
  33. }
  34. .babu{
  35. width: 50px;
  36. height: 50px;
  37. }

html:

  1. <!doctype html>
  2. <html lang="hu">
  3. <head>
  4. <title>fogdEsVidd</title>
  5. <link rel="stylesheet" href="css.css">
  6. </head>
  7. <body>
  8. <div id="tabla"></div>
  9. <p id="info">Fogd és vidd </p>
  10. <script src=js.js></script>
  11. </body>
  12. </html>