jQuery - 可拖拽交互



描述

在JqueryUI中,可拖拽函数可以用于交互。此函数可以在任何DOM元素上启用可拖拽功能。我们可以通过鼠标点击可拖拽元素来拖拽它。

语法

以下是使用可拖拽的简单语法:

$( "#droppable" ).droppable();

示例

以下是一个简单的示例,展示了可拖拽的使用:

<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" 
         src = "https://tutorialspoint.com/jquery/jquery-3.6.0.js">
      </script>
		
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js">
      </script>
		
      <script type = "text/javascript" language = "javascript">
   
         $(function() {
            $( "#draggable" ).draggable();
				
            $( "#droppable" ).droppable({
               drop: function( event, ui ) {
                  $( this ).addClass( "ui-state-highlight" ).find( "p" ).html( "Dropped!" );
               }
            });
         });
		 
      </script>
		
      <style>
         #draggable { width: 100px; height: 100px; 
            padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
         #droppable { width: 150px; height: 150px; 
            padding: 0.5em; float: left; margin: 10px; }
      </style>
   </head>
	
   <body>
      <div id = "draggable" class = "ui-widget-content">
         <p>Drag</p>
      </div>

 
      <div id = "droppable" class = "ui-widget-header">
         <p style = "background-color: aquamarine;height: 50;">Drop here</p>
      </div>
	 
   </body>
</html>

这将产生以下结果:

jquery-interactions.htm
广告