dir.by  
  Search  
Programming, development, testing
jQuery UI - buttons, menus, dialog, slider, spinner and so on. Effects: stretching, dragging, and so on
Droppable (a dragged item can be thrown) jQuery UI
  Looked at 5945 times    
 Droppable (a dragged item can be thrown) jQuery UI 
last updated: 5 February 2020
Method droppable in the jQuery UI creates a place where an item can be thrown.
Example we look, we test
Example code
  Html  
<html>

<!-- heading -->
<head>
     <meta charset="utf-8">
     <title>Example</title>
</head>

<!-- page -->
<body>
     <!-- connect libraries jQuery and jQuery UI -->
     <script src="https://dir.by/example_lib/jquery/jquery-3.3.1.min.js"></script>
     <script src="https://dir.by/example_lib/jquery_ui/jquery-ui-1.12.1/jquery-ui.min.js"></script>

     <!-- HTML Elements -->
     <div id="myElement1" style='height:100px; width:100px; float:left; background-color:#e9e9e9; cursor:hand; margin-right:20px;'>
          <p>Click on me and drag me with your mouse</p>
     </div>
     <div id="myElement2" style='height:250px; width:250px; float:left; background-color:#d0d0d0;'>
          <p>Quit here</p>
     </div>
    
     <!-- JavaScript -->
     <script>
          // the page has opened
          $( function()
          {
               // call the method draggable so that the item can be dragged myElement1
               $("#myElement1").draggable();

               // Call droppable to an element myElement1 could be thrown into an item myElement2
               $("#myElement2").droppable(
               {
                    // handler (what we will do when throwing myElement1)
                    drop: function(event, ui)
                    {
                         // change color
                         $(this).css("background-color", "#f0f0a0");

                         // change the text
                         $(this).find("p").html("Item thrown");
                    }
               });
          });
     </script>
</body>

</html>
Note!
The example works locally!

Run the example on your computer:
1) Create a new file my.html
2) Copy source code in the my.html file
3) Run in browser my.html

By the way!
The example allows you to drag and drop onto your computer.
Example does not allow you to drag and drop on mobile devices (smartphones, tablets).
Drag and drop on mobile devices (smartphones, tablets)
Example we look, we test
Explanation
One line is added to the code, this is the connection of the library jQuery UI Touch Punch ... to drag and drop on tablet, phone.
  Html  
<script src="https://dir.by/example_lib/touch-punch/jquery.ui.touch-punch.js"></script>


All example code to drag and drop on tablet, phone
In the code, the added line is highlighted Blue.
  Html  
<html>

<!-- heading -->
<head>
     <meta charset="utf-8">
     <title>Example</title>
</head>

<!-- page -->
<body>
     <!-- connect libraries jQuery and jQuery UI -->
     <script src="https://dir.by/example_lib/jquery/jquery-3.3.1.min.js"></script>
     <script src="https://dir.by/example_lib/jquery_ui/jquery-ui-1.12.1/jquery-ui.min.js"></script>

     <!-- drag-and-drop library for tablet, phone -->
     <script src="https://dir.by/example_lib/touch-punch/jquery.ui.touch-punch.js"></script>

     <!-- HTML Elements -->
     <div id="myElement1" style='height:100px; width:100px; float:left; background-color:#e9e9e9; cursor:hand; margin-right:20px;'>
          <p>Click on me and drag me with your mouse</p>
     </div>
     <div id="myElement2" style='height:250px; width:250px; float:left; background-color:#d0d0d0;'>
          <p>Quit here</p>
     </div>
    
     <!-- JavaScript -->
     <script>
          // the page has opened
          $( function()
          {
               // call the method draggable to an element myElement1 could be dragged
               $("#myElement1").draggable();

               // Call droppable to an element myElement1 could be thrown into an item myElement2
               $("#myElement2").droppable(
               {
                    // handler (what we will do when throwing myElement1)
                    drop: function(event, ui)
                    {
                         // change color
                         $(this).css("background-color", "#f0f0a0");

                         // change the text
                         $(this).find("p").html("Item thrown");
                    }
               });
          });
     </script>
</body>

</html>
 
← Previous topic
Draggable (the item can be moved with the mouse) jQuery UI
 
Next topic →
Resizable (resize an item with the mouse) jQuery UI
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
What the JQuery UI ?
jQuery UI where to download and how to connect?
Interactions (interaction)
Draggable (the item can be moved with the mouse) jQuery UI
Droppable (a dragged item can be thrown) jQuery UI
Resizable (resize an item with the mouse) jQuery UI
Selectable (select one or more elements with the mouse) jQuery UI
WWW sites to explore JQuery UI
Sites to explore JQuery UI

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика