events.html 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>jQuery UI Draggable - Events</title>
  6. <link rel="stylesheet" href="../../themes/base/jquery.ui.all.css">
  7. <script src="../../jquery-1.10.2.js"></script>
  8. <script src="../../ui/jquery.ui.core.js"></script>
  9. <script src="../../ui/jquery.ui.widget.js"></script>
  10. <script src="../../ui/jquery.ui.mouse.js"></script>
  11. <script src="../../ui/jquery.ui.draggable.js"></script>
  12. <link rel="stylesheet" href="../demos.css">
  13. <style>
  14. #draggable { width: 16em; padding: 0 1em; }
  15. #draggable ul li { margin: 1em 0; padding: 0.5em 0; } * html #draggable ul li { height: 1%; }
  16. #draggable ul li span.ui-icon { float: left; }
  17. #draggable ul li span.count { font-weight: bold; }
  18. </style>
  19. <script>
  20. $(function() {
  21. var $start_counter = $( "#event-start" ),
  22. $drag_counter = $( "#event-drag" ),
  23. $stop_counter = $( "#event-stop" ),
  24. counts = [ 0, 0, 0 ];
  25. $( "#draggable" ).draggable({
  26. start: function() {
  27. counts[ 0 ]++;
  28. updateCounterStatus( $start_counter, counts[ 0 ] );
  29. },
  30. drag: function() {
  31. counts[ 1 ]++;
  32. updateCounterStatus( $drag_counter, counts[ 1 ] );
  33. },
  34. stop: function() {
  35. counts[ 2 ]++;
  36. updateCounterStatus( $stop_counter, counts[ 2 ] );
  37. }
  38. });
  39. function updateCounterStatus( $event_counter, new_count ) {
  40. // first update the status visually...
  41. if ( !$event_counter.hasClass( "ui-state-hover" ) ) {
  42. $event_counter.addClass( "ui-state-hover" )
  43. .siblings().removeClass( "ui-state-hover" );
  44. }
  45. // ...then update the numbers
  46. $( "span.count", $event_counter ).text( new_count );
  47. }
  48. });
  49. </script>
  50. </head>
  51. <body>
  52. <div id="draggable" class="ui-widget ui-widget-content">
  53. <p>Drag me to trigger the chain of events.</p>
  54. <ul class="ui-helper-reset">
  55. <li id="event-start" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-play"></span>"start" invoked <span class="count">0</span>x</li>
  56. <li id="event-drag" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-arrow-4"></span>"drag" invoked <span class="count">0</span>x</li>
  57. <li id="event-stop" class="ui-state-default ui-corner-all"><span class="ui-icon ui-icon-stop"></span>"stop" invoked <span class="count">0</span>x</li>
  58. </ul>
  59. </div>
  60. <div class="demo-description">
  61. <p>Layer functionality onto the draggable using the <code>start</code>, <code>drag</code>, and <code>stop</code> events. Start is fired at the start of the drag; drag during the drag; and stop when dragging stops.</p>
  62. </div>
  63. </body>
  64. </html>