jQuery UI的Droppable scope选项

jQuery UI的Droppable scope选项

jQuery UI由GUI小工具、视觉效果和使用jQuery JavaScript库实现的主题组成。jQuery UI对于构建网页的UI界面非常有用。它可以用来建立高度互动的网络应用程序,也可以用来轻松地添加小工具。

在这篇文章中,我们将学习jQuery UI Droppable范围选项。范围选项是用来对可拖动和可下降的项目进行分组。一个可拖动和可下拉的项目应该有相同的范围值。

语法 :scope选项项需要一个字符串值,并被初始化,如下所示。

$("#dropFirst").droppable({
    scope: "first",
});
  • 获得scope选项。
var scopeOpt = $("#dropFirst").droppable("option", "scope");
  • 设置scope选项。
$("#dropFirst").droppable("option", "scope", "first");

CDN链接。在jQuery UI项目中使用以下CDNs。

<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css” />
<script src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js”></script>
<script src=”https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js”></script>

例子。在下面的例子中,我们有两个可拖动的和两个可放下的项目。scope被分别设置为第一和第二。

<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge" />
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" 
          href=
"https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" />
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js">
    </script>
    <script src=
"https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js">
    </script>
    <style type="text/css">
    .square_draggable {
        width: 50px;
        height: 50px;
        border: 1px solid black;
        margin: 5px;
        text-align: center;
        line-height: 50px;
        background-color: lightblue;
        cursor: pointer;
        border-radius: 10px;
    }
  
    .square_droppable {
        width: 100px;
        height: 100px;
        border: 1px dotted gray;
        margin: 5px;
        text-align: center;
        line-height: 100px;
        background-color: lightgray;
    }
  
    .container {
        background-color: darkgreen;
        width: 500px;
        height:300px;
        margin: auto;
    }
    </style>
</head>
  
<body>
    <div data-role="page" id="gfgpage">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <div data-role="main" class="ui-content">
            <h3>jQuery UI Droppable scope option</h3>
            <div id="gfg_container" 
                 class="container">
                <div id="dragFirst" 
                     class="square_draggable">
                        Item 1</div>
                <div style="float:left">
                    <div id="dragSecond" 
                         class="square_draggable">
                         Item 2</div>
                </div>
                <div style="float:left;margin-left:50px;">
                    <div id="dropFirst" 
                         class="square_droppable">
                         Scope : first</div>
                </div>
                <div style="float:left;margin-left:50px;">
                    <div id="dropSecond" 
                         class="square_droppable">
                         Scope : second</div>
                </div>
            </div>
        </div>
    </div>
    <script>
    (function() {
        ("#dragFirst").draggable({
            scope: "first",
            containment: "#gfg_container"
        });
  
        ("#dragSecond").draggable({
            scope: "second",
            containment: "#gfg_container"
        });
  
        ("#dropFirst").droppable({
            scope: "first",
            drop: function(event, ui) {
                (this).css("background-color", "lightgreen")
            },
            over: function(event, ui) {
                (this).css("background-color", "green")
            },
            out: function(event, ui) {
                (this).css("background-color", "lightgray")
            }
        });
  
        ("#dropSecond").droppable({
            scope: "second",
            drop: function(event, ui) {
                (this).css("background-color", "lightgreen")
            },
            over: function(event, ui) {
                (this).css("background-color", "green")
            },
            out: function(event, ui) {
                $(this).css("background-color", "lightgray")
            }
        });
  
    });
    </script>
</body>
  
</html>

输出

jQuery UI的Droppable scope选项

jQuery UI的Droppable scope选项

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程