rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » Javascript

jQuery拖拽动作,样例注解

jQuery拖拽动作,定义事件函数

<!doctype html>
<html lang="en">
<head>
<title>jQuery UI Draggable - Events</title>
<link type="text/css" href="../../themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="../../jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../ui/ui.core.js"></script>
<script type="text/javascript" src="../../ui/ui.draggable.js"></script>
<link type="text/css" href="../demos.css" rel="stylesheet" />
<style type="text/css">
   #draggable { width: 16em; padding: 0 1em; }
   #draggable ul li { margin: 1em 0; padding: 0.5em 0; } * html #draggable ul li { height: 1%; }
   #draggable ul li span.ui-icon { float: left; }
   #draggable ul li span.count { font-weight: bold; }
</style>
<script type="text/javascript">
   $(function() { //将层储存到不同的变量中
    var $start_counter = $('#event-start'), $drag_counter = $('#event-drag'), $stop_counter = $('#event-stop');
    var counts = [0,0,0]; //设置点击函数

    $("#draggable").draggable({ //事件有三种拖动start(开始),drag(拖动中),stop(拖动停止)
     start: function() { //start为开始事件,大括号中是要执行函数
      counts[0]++;
      updateCounterStatus($start_counter,counts[0]);      drag: function() {
      counts[1]++;
      updateCounterStatus($drag_counter,counts[1]);      stop: function() {
      counts[2]++;
      updateCounterStatus($stop_counter,counts[2]);     });
   });

   function updateCounterStatus($event_counter,new_count) { //更新count的函数在不同的事件中调用的都是这个测试函数
    // first update the status visually...
    if (!$event_counter.hasClass('ui-state-hover')) { //判断有没有‘ui-state-hover’样式
     $event_counter.addClass('ui-state-hover') //没有添加样式
      .siblings().removeClass('ui-state-hover'); //同时移除平级元素的样式     // ...then update the numbers
    $('span.count',$event_counter).text(new_count); //将count在span.count这个元素标记中显示 </script>
</head>
<body>

<div class="demo">

<div id="draggable" class="ui-widget ui-widget-content">

<p>Drag me to trigger the chain of events.</p>

<ul class="ui-helper-reset">
   <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>
   <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>
   <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>
</ul>
</div>

</div><!-- End demo -->

<div class="demo-description">

<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>

</div><!-- End demo-description -->

</body>
</html>

jQuery拖拽动作,限制移动范围

<!doctype html>
<html lang="en">
<head>
<title>jQuery UI Draggable - Constrain movement</title>
<link type="text/css" href="../../themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="../../jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../ui/ui.core.js"></script>
<script type="text/javascript" src="../../ui/ui.draggable.js"></script>
<link type="text/css" href="../demos.css" rel="stylesheet" />
<style type="text/css">
.draggable { width: 90px; height: 90px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
#draggable, #draggable2 { margin-bottom:20px; }
#draggable { cursor: n-resize; }
#draggable2 { cursor: e-resize; }
#containment-wrapper { width: 95%; height:150px; border:2px solid #ccc; padding: 10px;}
</style>
<script type="text/javascript">
$(function() { //axis 限制拖拽是沿轴移动
   $("#draggable").draggable({ axis: 'y' }); //垂直移动
   $("#draggable2").draggable({ axis: 'x' }); //水平移动
                              //containment 限制元素在选定的DOM元素中移动
   $("#draggable3").draggable({ containment: '#containment-wrapper', scroll: false });
   $("#draggable4").draggable({ containment: '#demo-frame' });
   $("#draggable5").draggable({ containment: 'parent' });

});
</script>
</head>
<body>
<div class="demo">

<h3 class="docs">Constrain movement along an axis:</h3> <div id="draggable" class="draggable ui-widget-content">
<p>I can be dragged only vertically</p>
</div>

<div id="draggable2" class="draggable ui-widget-content">
<p>I can be dragged only horizontally</p>
</div>

<h3 class="docs">Or to within another DOM element:</h3>
<div id="containment-wrapper">
<div id="draggable3" class="draggable ui-widget-content">
<p>I'm contained within the box</p>
</div>

<div id="draggable4" class="draggable ui-widget-content">
<p>I'm contained within the box's parent</p>
</div>

<div class="draggable ui-widget-content">
<p id="draggable5" class="ui-widget-header">I'm contained within my parent</p>
</div>
</div>

</div><!-- End demo -->

<div class="demo-description">

<p>
Constrain the movement of each draggable by defining the boundaries of the draggable area. Set the <code>axis</code> option to limit the draggable's path to the x- or y-axis, or use the <code>containment</code> option to specify a parent DOM element or a jQuery selector, like 'document.'
</p>

</div><!-- End demo-description -->
</body>
</html>

延迟显示:

<!doctype html>
<html lang="en">
<head>
<title>jQuery UI Draggable - Delay start</title>
<link type="text/css" href="../../themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="../../jquery-1.3.2.js"></script>
<script type="text/javascript" src="../../ui/ui.core.js"></script>
<script type="text/javascript" src="../../ui/ui.draggable.js"></script>
<link type="text/css" href="../demos.css" rel="stylesheet" />
<style type="text/css">
#draggable, #draggable2 { width: 120px; height: 120px; padding: 0.5em; float: left; margin: 0 10px 10px 0; }
</style>
<script type="text/javascript">
$(function() {
   $("#draggable").draggable({ distance: 20 });//鼠标滑动20px以后执行
   $("#draggable2").draggable({ delay: 1000 });//1000毫秒以后执行
   $(".ui-draggable").disableSelection();//不允许选择css"ui-draggable"(效果表现为不能选择拖拽层中的文字) </script>
</head>
<body>
<div class="demo"> <div id="draggable" class="ui-widget-content">
<p>Only if you drag me by 20 pixels, the dragging will start(在鼠标移动20px以后拖动开始执行)</p>
</div>

<div id="draggable2" class="ui-widget-content">
<p>Regardless of the distance, you have to drag and wait for 1000ms before dragging starts(在一秒以后执行拖拽)</p>
</div>

</div><!-- End demo -->

<div class="demo-description">

<p>
Delay the start of dragging for a number of milliseconds with the <code>delay</code> option; prevent dragging until the cursor is held down and dragged a specifed number of pixels with the <code>distance</code> option.
(<code>delay</code>选项:拖拽事件在拖拽开始多少毫秒后执行。
<code>distance</code>选项:拖拽事件在鼠标移动多少像素以后执行。)
</p>

</div><!-- End demo-description -->
</body>
</html>

顶一下
(1)
踩一下
(0)