在Div中创建和拖动Div

时间:2017-04-12 15:57:57

标签: javascript jquery html css html5

到目前为止,我有一个容器div,它包含一个按钮,当按钮单击时,它会创建一个div,其id为set =" newcard"可调整大小(此部分正常工作'正确')。但是,当我合并拖动div元素时,根本不拖动。有什么帮助吗?



$(function(){

	$(".createcard").click(function() {
		var domElement = $('<div id="newcard" ></div>');
		$('.notecard-container').append(domElement);

	});
	$('#newcard').draggable();

});
&#13;
body, html {
	margin: 0;
	padding: 0;
}

.container {
	position: absolute;
	left: 0;
	top: 0;
	width: 100%;
	height: 100%;
	background: rgb(255, 255, 255);
}

.createcard {
	bottom: 5%;
	left: 5%;
	width: 125px;
	height: 45px;
	background: rgba(255, 255, 255, .0);
	position: absolute;
	display: block;
	border: 1px solid transparent;	
	
	outline: none;
	font-family: 'Nunito', sans-serif;
	font-size: 20px;
	font-weight: bold;

	-webkit-transition:  .4s ease;
	-moz-transition:  .4s ease;
	-o-transition:  .4s ease;
	-ms-transition:  .4s ease;
	transition:  .4s ease;
	transition: .4.0s
}

.createcard:hover {
	background: rgba(255, 255, 255, .9);
	
	border-radius: 5px;
	border: 1px solid #c0c0c0;	
	
	-webkit-transition:  .4s ease;
	-moz-transition:  .4s ease;
	-o-transition:  .4s ease;
	-ms-transition: .4s ease;
	transition:  .4s ease;
	transition: .4.0s
}

#newcard{
	position: absolute;
	width: 150px;
	height: 150px;
	min-width:150px;
	min-height:150px;
	max-width:300px;
	max-height:300px;
	top:10%;
	left:10%;
	background: white;
    resize: both;
    overflow: auto;
}

.notecard-container {
	position: absolute;
	top: 7%;
	left: 2%;
	right: 2%;
	bottom: 2%;
	background: rgb(255, 228, 181);
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Nunito"
	rel="stylesheet">
<link rel="stylesheet" type="text/css" href="index.css">
<link rel="stylesheet" href="aos.css">
<meta charset="ISO-8859-1">
<title>Post-it note</title>
</head>
<body>
	<div class="container">
		<div class="notecard-container">
			<button class="createcard" id="createcard">New    Card</button>

		</div>
	</div>

	<!-- Input JavaScript and jQuery -->
</body>
</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

draggable()调用移至点击处理程序:

$(function(){

    $(".createcard").click(function() {
        var domElement = $('<div id="newcard" ></div>');
        $('.notecard-container').append(domElement);
        $('#newcard').draggable();
    });

});

答案 1 :(得分:0)

您需要在创建元素后添加draggable()

这是一个jsfiddle示例:https://jsfiddle.net/dk341jjv/1/