如何禁用一个jQuery-ui小部件的可拖动性
在这篇文章中,我们将看到如何使用jQuery-ui禁用可拖动的widget。jQuery UI由GUI部件、视觉效果和使用HTML、CSS和jQuery实现的主题组成,jQuery UI对于构建网页的UI界面非常有用。jQuery UI的Draggable disabled选项是用来禁用可拖动元素的,如果它的值被设置为true。
语法:
$( ".selector" ).draggable({
disabled: true/false
});
CDN链接:首先,添加你的项目需要的jQuery UI脚本。
<link rel=”stylesheet” href=”//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css”>
<script src=”//code.jquery.com/jquery-1.12.4.js”></script>
<script src=”//code.jquery.com/ui/1.12.1/jquery-ui.js”></script>
示例:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href=
"//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.12.4.js"></script>
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js">
</script>
<title>
How to disable jQuery-ui draggable widget ?
</title>
<style>
#div_element {
width: 150px;
height: 150px;
background: green;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
}
</style>
</head>
<body>
<center>
<h1 style="color: green;">GeeksforGeeks</h1>
<h3>
How to disable jQuery-ui draggable widget?
</h3>
<div id="div_element">Div content</div>
<br>
<input type="button" id="disable"
value="Disable Draggable Option">
</center>
<script>
(document).ready(function() {
(function() {
("#div_element").draggable({
disabled: false
});
});
("#disable").on('click', function() {
$("#div_element").draggable({
disabled: true
});
});
});
</script>
</body>
</html>
输出: