簡體   English   中英

jQuery的可拖動的droppable與sortable

[英]jquery draggable droppable with sortable

我試圖做到這一點,以便當我將所有單詞都放在底磚中時,可以在“最喜歡我到最不喜歡我”可放置區域之間對它們進行排序。 現在,我可以將它們拖放到不同的框中,但是它將在一個可放置區域中將兩個可拖動對象放在一起。 我希望它可以將另一個可拖動對象開箱即用,並且不允許兩個可拖動對象位於同一個可放置區域中,同時仍然允許用戶改變主意,如果他們犯錯或想求助於他們。 請在下面找到代碼:

的HTML

<div id="content">
  <div id="cardPile">
  <div id="question1">Spontaneous</div>
  <div id="question2">Decisive</div>
  <div id="question3">Realistic</div>
  <div id="question4">Logical</div>
  </div>

  <div id="cardSlots">
  <div>Most Like Me</div>
  <div>2nd Most Like Me</div>
  <div>3rd Most Like Me</div>
  <div>Least Like Me</div>

  </div>

</div>

的CSS

@charset "utf-8";
/* CSS Document */

/* Add some margin to the page and set a default font and colour */

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

/* Give headings their own font */

h1, h2, h3, h4 {
  font-family: Century Gothic, sans-serif;
  font-weight:normal;
}

/* Main content area */

#content {
  margin: 65px 0px;
  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: 10px auto 0 auto;
  background:#D9F2FF;
}

/* The initial pile of unsorted cards */

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

#cardSlots {
  width: 910px;
  height: 55px;
  padding: 20px;
  border: 1px solid #999;
  -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);
}
 #cardPile {
width: 910px;
  height: 55px;
  padding: 20px;
  -moz-border-radius: 10px;
  -webkit-border-radius: 10px;
  border-radius: 10px;
 }
/* Individual cards and slots */

#cardSlots div, #cardPile div {
  float: left;
  width: 195px;
  height: 20px;
  padding: 10px;
  padding-top: 10px;
  padding-bottom: 20px;
  border: 2px solid #999;
  -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: #009;
  color: #fff;
  font-size: 20px;
  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);
}

Java腳本

$(document).ready(function(){    

$( init );

function init() {
  // Create the pile of shuffled cards
  {
    $('#cardPile div').draggable( {
      containment: '#content',
      stack: '#cardPile div',
      cursor: 'move',
      revert: true
    } );
  }

  // Create the card slots
  {
    $('#cardSlots div').droppable( {
      accept: '#cardPile div',
      hoverClass: 'hovered',
      drop: handleCardDrop
    } );



  }

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

    if ( cardNumber1 == cardNumber1 ) {
        ui.draggable.addClass ( 'correct' );
        ui.draggable.position( {of: $(this), my: 'left top', at: 'left top' } );
        ui.draggable.draggable( 'option', 'revert', false );
    }
}

}



});

看這個: 工作演示

$(document).ready(function () {

$(init);

function init() {

    $('#cardSlots').sortable({placeholder: "ui-state-highlight"});
    // Create the pile of shuffled cards
    {
        $('#cardPile div').draggable({
            containment: '#content',
            stack: '#cardPile div',
            cursor: 'move',
            connectToSortable: '#cardSlots'

        });
    }

    // Create the card slots
    {
        $('#cardSlots').droppable({
            accept: '#cardPile',
            hoverClass: 'hovered',
            drop: handleCardDrop

        });}

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

        if (cardNumber1 == cardNumber1) {
            ui.draggable.addClass('correct');
            ui.draggable.position({
                of: $(this),
                my: 'left top',
                at: 'left top'
            });

          ui.draggable.draggable('option', 'revert', false);

        }

    }

 }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM