JQueryUI-拖动(Draggable)-约束运动
更新:HHH   时间:2023-1-7


说明

通过定义 draggable 区域的边界来约束每个 draggable 的运动。设置 axis 选项来限制 draggable 的路径为 x 轴或者 y 轴


示例

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<title>draggable</title>
	<link rel="stylesheet" href="js/jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/jquery-ui.min.css">
	<style>
		.draggable{
			width: 90px;
			height: 90px;
			padding: 0.5em;
			float: left;
			margin: 0 10px 10px 0;
		}

		#draggable1,#draggable2{
			margin-bottom: 20px;
		}

		#draggable1{
			cursor: e-resize;
		}

		#draggable2{
			cursor:n-resize;
		}

		h4{
			clear:left;
		}
	</style>
</head>
<body>
	<h4>沿着X轴约束运动:</h4>
	<div id="draggable1" class="draggable ui-widget-content">
	  	<p>只能水平拖拽</p>
	</div>

	<h4>沿着Y轴约束运动:</h4>
	<div id="draggable2" class="draggable ui-widget-content">
	  	<p>只能垂直拖拽</p>
	</div>

	<script src="js/jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/external/jquery/jquery.js" type="text/javascript" ></script>
	<script src="js/jquery-ui-1.12.1.custom/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
	<script>
		$("#draggable1").draggable({axis:"x"});
		$("#draggable2").draggable({axis:"y"});
	</script>
</body>
</html>


输出

返回web开发教程...