這篇文章主要介紹JQueryUI的Draggable如何在DOM元素中約束運動,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
通過定義 draggable 區域的邊界來約束每個 draggable 的運動,使用 containment 選項來指定一個父級的 DOM 元素或者一 個 jQuery 選擇器
<!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;
cursor: move
}
h4{
clear:left;
}
#containment-wrapper{
width:95%;
height: 450px;
border:2px solid #ccc;
padding:10px;
}
</style>
</head>
<body>
<h4>在DOM 元素中約束運動:</h4>
<div id="containment-wrapper">
<div id="draggable1" class="draggable ui-widget-content">
<p>我被約束在盒子里</p>
</div>
<div class="draggable ui-widget-content">
<p id="draggable2" class="ui-widget-header">我被約束在父元素內</p>
</div>
</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({
containment:"#containment-wrapper",
scroll:false
});
$("#draggable2").draggable({containment:"parent"});
</script>
</body>
</html>
以上是“JQueryUI的Draggable如何在DOM元素中約束運動”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。