WEBデザインの勉強

初心者が1からWEBデザインを勉強するブログ。

jQuery UIのDraggableで要素をドラッグできるようにする

f:id:mmmmofu:20161026205038p:plain

デモはこちら

http://efu.sakura.ne.jp/web/drag/

<!DOCTYPE html>
<html lang="ja">
<head>
	<meta charset="UTF-8">
	<title>jQuery UI Draggable</title>
	<link rel="stylesheet" href="css/style.css">
	<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
	<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
	<script src="js/script.js"></script>
</head>
<body>
	<div class="content">
		<p class="text">boxをドラッグしてください</p>
		<div class="box5">
			<p>ここにドラッグしてください</p>
		</div>
		<div class="box box1">
			<p>box1</p>
		</div>
		<div class="box box2">
			<p>box2</p>
		</div>
		<div class="box box3">
			<p>box3</p>
		</div>
		<div class="box box4">
			<p>box4</p>
		</div>
	</div>
</body>
</html>
/* http://meyerweb.com/eric/tools/css/reset/
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
  display: block;
}
body {
  line-height: 1;
}
ol, ul {
  list-style: none;
}
blockquote, q {
  quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
  content: '';
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
body {
  font-family: "游ゴシック", YuGothic, "ヒラギノ角ゴ ProN W3", "Hiragino Kaku Gothic ProN", "メイリオ", Meiryo, "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-weight: 500;
}

.content {
  position: relative;
  width: 800px;
  height: 800px;
  background: #333;
  margin: 0 auto;
}

.box {
  position: absolute;
  color: #FFF;
  text-align: center;
  cursor: pointer;
}
.box1 {
  background: #2E94B9;
  width: 100px;
  height: 200px;
}
.box2 {
  width: 200px;
  height: 300px;
  background: #FFFDC1;
}
.box3 {
  width: 100px;
  height: 100px;
  background: #F0B775;
}
.box4 {
  width: 300px;
  height: 100px;
  background: #D25565;
}
.box5 {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  width: 300px;
  height: 400px;
  margin: auto;
  background: #EEE;
  text-align: center;
  line-height: 400px;
}
.text {
  position: absolute;
  bottom: 20px;
  left: 0;
  width: 100%;
  color: #FFF;
  font-size: 40px;
  font-weight: bold;
  text-align: center;
}
.text span {
  display: block;
  font-size: 16px;
  font-weight: normal;
  line-height: 1.6;
}
$(function(){
  $('.content .box').each( function(i){
    var randT = Math.floor( Math.random() * 701 );//topの値をランダムに
    var randL = Math.floor( Math.random() * 701 );//leftの値をランダムに
    $(this).css({'top' : randT, 'left' : randL});//boxをランダムに配置
  });

  //ドラッグを可能にする
  $(".box").draggable({
    containment: ".content",//指定要素の中でのみ移動
    snap:".box5",//指定要素のすみにドラッグした要素がくっつく
  });
  $(".box5").droppable({
    accept : ".box",//受け入れる要素
    drop : function (event , ui) {//ドロップしたら
      var thisBox = ui.draggable.find('p').text();
      $('.text').append('<span class="text_' + thisBox +'">' + thisBox + 'が置かれました。</span>');
    },
    out : function (event , ui){
      var thisBox = ui.draggable.find('p').text();
      $('.text').find(".text_" + thisBox).remove();
    }
  });

});

参考サイト

syncer.jp