拖放-元素不会掉落 [英] drag and drop - the elements don't drop

查看:116
本文介绍了拖放-元素不会掉落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行拖放测验,这是完整的代码

i'm making a drag and drop quiz here's the full code

 <!doctype html>
    <html>
    <head>

    <title>A jQuery Drag-and-Drop quiz</title>

    <link rel="stylesheet" type="text/css" href="style.css">

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
    <script type="text/javascript">

    var correctCards = 0;
    $( init );

    function init() {

      // Hide the success message
      $('#successMessage').hide();
      $('#successMessage').css( {
        left: '580px',
        top: '250px',
        width: 0,
        height: 0
      } );

      // Reset the game
      correctCards = 0;
      $('#cardPile').html( '' );
      $('#cardSlots').html( '' );

      // Create the pile of shuffled cards
       var numbers = ["A","B","C"];


  numbers.sort( function() { return Math.random() - .5 } );

  for ( var i=0; i<3; i++ ) {
    $('<div>' + "<img src = \"" + numbers [i]  + ".gif\" width = \"105\" height = \"100\" />"+ '</div>').data( 'number', numbers[i] ).attr( 'id', 'card'+numbers[i] ).appendTo( '#cardPile' ).draggable( {
      containment: '#content',
      stack: '#cardPile div',
      cursor: 'move',
      revert: true
    } );
  }

      // Create the card slots
      var words = [ 'A', 'B', 'C'];
      for ( var i=1; i<=3; i++ ) {
        $('<div>' + words[i-1] + '</div>').data( 'number', i ).appendTo( '#cardSlots' ).droppable( {
          accept: '#cardPile div',
          hoverClass: 'hovered',
          drop: handleCardDrop
        } );
      }

    }

    function handleCardDrop( event, ui ) {
      var slotNumber = $(this).data( 'number' );
      var cardNumber = ui.draggable.data( 'number' );

      // If the card was dropped to the correct slot,
      // change the card colour, position it directly
      // on top of the slot, and prevent it being dragged
      // again

      if ( slotNumber == cardNumber ) {
        ui.draggable.addClass( 'correct' );
        ui.draggable.draggable( 'disable' );
        $(this).droppable( 'disable' );
        ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
        ui.draggable.draggable( 'option', 'revert', false );
        correctCards++;
      } 

      // If all the cards have been placed correctly then display a message
      // and reset the cards for another go

      if ( correctCards == 3 ) {
        $('#successMessage').show();
        $('#successMessage').animate( {
          left: '380px',
          top: '200px',
          width: '400px',
          height: '100px',
          opacity: 1
        } );
      }

    }

    </script>

    </head>
    <body>

    <div class="wideBox">
      <h1>Drag-and-Drop with jQuery: Your Essential Guide</h1>
      <h2>A Number Cards Game</h2>
    </div>

    <div id="content">

      <div id="cardPile"> </div>
      <div id="cardSlots"> </div>

      <div id="successMessage">
        <h2>You did it!</h2>
        <button onclick="init()">Play Again</button>
      </div>

    </div>

    </body>
    </html>

最初在数字数组中曾经是数字,但工作正常,但是当我将数字更改为图片时,我可以拖动它们,但不能将其放到正确的位置,例如..它们是可拖动但不能拖放的!我没有触摸代码,只是更改了数组和循环以显示图片,任何人都不知道该怎么办,请吗?

originally in the numbers array used to be numbers and it was working fine but then when i changed the numbers into pictures, i'm able to drag them but i'm not able to drop in the right place, like .. they are dragable but not dropable ! i didn't touch the code i only changed the array and the loop to show the pictures, anyone have any idea what to do, please ?

这是style.css文件

this is the style.css file

/* Main content area */


.wideBox {
  clear: both;
  text-align: center;
  margin: 70px;
  padding: 10px;
  background: #ebedf2;
  border: 1px solid #333;
}



/* Slots for final card positions */

#cardSlots {
  margin: 50px auto 0 auto;
  background: #ddf;
}

/* The initial pile of unsorted cards */

#cardPile {
  margin: 0 auto;
  background: #ffd;
}

#cardSlots, #cardPile {
  width: 910px;
  height: 120px;
  padding: 20px;
  border: 2px solid #333;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  -moz-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
  -webkit-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
  box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
}

/* Individual cards and slots */

#cardSlots div, #cardPile div {
  float: left;
  width: 58px;
  height: 78px;
  padding: 10px;
  padding-top: 40px;
  padding-bottom: 0;
  border: 2px solid #333;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  margin: 0 0 0 10px;
  background: #fff;
}

#cardSlots div:first-child, #cardPile div:first-child {
  margin-left: 0;
}

#cardSlots div.hovered {
  background: #aaa;
}

#cardSlots div {
  border-style: dashed;
}

#cardPile div {
  background: #666;
  color: #fff;
  font-size: 50px;
  text-shadow: 0 0 3px #000;
}

#cardPile div.ui-draggable-dragging {
  -moz-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
  -webkit-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
  box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
}

/* Individually coloured cards */

#card1.correct { background: red; }
#card2.correct { background: brown; }
#card3.correct { background: orange; }
#card4.correct { background: yellow; }
#card5.correct { background: green; }
#card6.correct { background: cyan; }
#card7.correct { background: blue; }
#card8.correct { background: indigo; }
#card9.correct { background: purple; }
#card10.correct { background: violet; }


/* "You did it!" message */
#successMessage {
  position: absolute;
  left: 580px;
  top: 250px;
  width: 0;
  height: 0;
  z-index: 100;
  background: #dfd;
  border: 2px solid #333;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
  -moz-box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
  -webkit-box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
  box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
  padding: 20px;
}

该代码最初用于数组中的10个元素,请帮助

the code is originally for 10 elements in an array please help

推荐答案

    /* style.css */
    /* This game works in Chrome */
    /* Add some margin to the page and set a default font and colour */

    body {
      margin: 30px;
      font-family: "Georgia", serif;
      line-height: 1.8em;
      color: #333;
    }

    /* Give headings their own font */

    h1, h2, h3, h4 {
      font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
    }

    /* Main content area */

    #content {
      margin: 80px 70px;
      text-align: center;
      -moz-user-select: none;
      -webkit-user-select: none;
      user-select: none;
    }
    /* Header/footer boxes */

    .wideBox {
      clear: both;
     text-align: center;
      margin: 70px;
      padding: 10px;
      background: #ebedf2;
      border: 1px solid #333;
    }

    .wideBox h1 {
      font-weight: bold;
      margin: 20px;
      color: #666;
      font-size: 1.5em;
    }

    /* Slots for final card positions */

    #cardSlots {
      margin: 50px auto 0 auto;
      background: #ddf;
    }

    /* The initial pile of unsorted cards */

    #cardPile {
      margin: 0 auto;
      background: #ffd;
    }

    #cardPile {
      width: 360px;
      height: 120px;
      padding: 20px;
      border: 2px solid #333;
      -moz-border-radius: 10px;
      -webkit-border-radius: 10px;
      border-radius: 10px;
      -moz-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
      -webkit-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
      box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
    }
    #cardSlots {
     width: 360px;
      height: 120px;
      padding: 20px;
      border: 2px solid #333;
      -moz-border-radius: 10px;
      -webkit-border-radius: 10px;
      border-radius: 10px;
      -moz-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
      -webkit-box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
      box-shadow: 0 0 .3em rgba(0, 0, 0, .8);
    }

    /* Individual cards and slots */

    #cardPile div {
      float: left;
      width: 58px;
      height: 78px;
      padding: 10px;
      padding-top: 5px;
      padding-bottom: 34px;
      border: 2px solid #333;
      -moz-border-radius: 10px;
      -webkit-border-radius: 10px;
      border-radius: 10px;
      margin: 0 0 0 10px;
      background: #fff;
    }

    #cardSlots div {
      float: left;
      width: 58px;
      height: 78px;
      padding: 10px;
      padding-top: 40px;
      padding-bottom: 0;
      border: 2px solid #333;
      -moz-border-radius: 10px;
      -webkit-border-radius: 10px;
      border-radius: 10px;
      margin: 0 0 0 10px;
      background: #fff;
    }
    #cardSlots div:first-child {
      margin-left: 0;
    }

    #cardPile div:first-child {
      margin-left: 0;
    }

    #cardSlots div.hovered {
      background: #aaa;
    }

    #cardSlots div {
      border-style: dashed;
      background: #666;
      font-size: 50px; 
    }
    #cardPile div {
      background: red;  
      color: #fff;
      font-size: 50px; 
      text-shadow: 0 0 3px #000;
    }

    #cardPile div.ui-draggable-dragging {
      -moz-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
      -webkit-box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
      box-shadow: 0 0 .5em rgba(0, 0, 0, .8);
    }

    }
    /* Individually coloured cards */
    /* Note carda will not turn to a new colour */

    #carda.correct { background: blue; }
    #cardb.correct { background: green; }
    #cardc.correct { background: orange; }
    #cardd.correct { background: yellow; }


    /* "You did it!" message */
    #successMessage {
      position: absolute;
      left: 580px;
      top: 250px;
      width: 0;
     height: 0;
      z-index: 100;
      background: #dfd;
      border: 2px solid #333;
      -moz-border-radius: 10px;
      -webkit-border-radius: 10px;
      border-radius: 10px;
      -moz-box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
      -webkit-box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
      box-shadow: .3em .3em .5em rgba(0, 0, 0, .8);
      padding: 20px;
    }

这篇关于拖放-元素不会掉落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆