Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="drag-and-drop-demo">
  <div class="card-wrapper">
    <div id="card-1" class="card" draggable="true">
      Draggable Card
    </div>
  </div>
  <div id="drop-point">Drop Here!</div>
</div>
              
            
!

CSS

              
                $box-size_width: 120px;
$box-size_height: 110px;
$box-color_background: #CECECE;

@mixin base_box {

  display: flex;
  justify-content: center;
  flex-direction: column;

  height: $box-size_height;
  width: $box-size_width;

  background: $box-color_background;
  text-align: center;
  margin: 1rem;
}

#drag-and-drop-demo {
  display: flex;

  align-items: center;
  align-content: center;
  align-self: center;

  justify-content: center;
  flex-direction: row;

  margin-top: 3rem;

  .card-wrapper {
    @include base_box;
    display: inline-block;
    .card {
      @include base_box;
      font-weight: 200;
      border: 1px dotted #0A0A0A;
    }
  }
  #drop-point {
    @include base_box;
    border: 1px dashed #0A0A0A;

    .card {
      @include base_box;
      border: 1px solid #0A0A0A;
    }
  }
}
              
            
!

JS

              
                // Card の Drag & Drop を操作可能にする
// addeventlistener() にイベントと対応させたメソッド参照を登録する
class DropPoint {
  onDragOver(event) {
    event.preventDefault()

    // TIPS: Drop したい位置に hover したら、ハイライトする CSS スタイルを当てて反応を示すことができる
    // if(!this.classList.contains('drop-point_hover')) {
    //   this.classList.add('drop-point_hover')
    // }
  }

  onDragLeave(event) {
    // TIPS: Drop したい位置から離れたら、CSS スタイルを外して反応を示すことができる
    // if(this.classList.contains('drop-point_hover')) {
    //   this.classList.remove('drop-point_hover')
    // }
  }

  onDragEnd(event) {
    //
    // TIPS: Drag 操作が終わったら #drop-point を非表示に切り替えることもできる
    //

    // var targetElements = document.querySelectorAll('#drop-point')

    // targetElements.forEach((elm) => {
    //   if(!elm.classList.contains('drop-point_hidden')) {
    //     elm.classList.add('drop-point_hidden')
    //   }
    //   if(elm.classList.contains('drop-point_hover')) {
    //     elm.classList.remove('drop-point_hover')
    //   }
    // })
  }

  onDragEnter(event) {
    // Implement some function when need.
  }

  // Drag & Drop のために dataTransfer に要素の情報を格納する
  onDragStart(event) {
    // TIPS: Drag 操作を開始したタイミングでクラスの付与・除去ができる
    // var targetElements = document.querySelectorAll('#drop-point')
    // targetElements.forEach((elm) => {
    //   elm.classList.remove('drop-point_hidden')
    // })

    // Drag & Drop でのデータ転送準備に値をセット
    event.dataTransfer.setData("text/plain", event.target.id) // Drag された要素の ID 属性値をセット
  }

  // Card を #drop-point に Drag & Drop したときに Card を移動する
  onDrop(event) {
    var cardId = event.dataTransfer.getData("text") // Drop された要素のデータ(= ID 属性値) を読み取り
    var card = document.getElementById(cardId) // Drop されてきた要素を取得

    var baseCardInsertPosition =
      document.getElementById('drop-point')

    baseCardInsertPosition.appendChild(card)

    card.draggable = false // 移動先に固定

    event.dataTransfer.clearData()
  }

  // Drag & Drop したい各要素に対して、対応するイベント に/の メソッドの登録
  initDropPoint() {
    // Drag 可能にするカード群
    var targetCardElements = document.querySelectorAll('.card')
    // Drop 操作受け入れ先
    var targetDropPointElements = document.querySelectorAll('#drop-point')

    targetCardElements.forEach((elm) => {
      elm.addEventListener('dragstart', this.onDragStart)
      elm.addEventListener('dragend', this.onDragEnd)
    })

    targetDropPointElements.forEach((elm) => {
      // elm.addEventListener('dragenter', this.onDragEnter) // No any implemented yet.
      elm.addEventListener('dragover', this.onDragOver)
      elm.addEventListener('dragleave', this.onDragLeave)
      elm.addEventListener('drop', this.onDrop)
    })
  }
}

//
// ページ読み込み時に Drag & Drop 操作の処理を各イベントに登録
//

var dropPoint = new DropPoint()

document.addEventListener('DOMContentLoaded', function() {
  dropPoint.initDropPoint();
});

// Rails の turbolinks イベントにも登録
// document.addEventListener('turbolinks:load', function() {
//   dropPoint.initDropPoint();
// });

              
            
!
999px

Console