JqueryUI可选方向

时间:2014-02-13 12:07:17

标签: javascript jquery jquery-ui jquery-ui-selectable

我正在尝试构建一些JQuery UI可选演示。 是否可以知道用户是从左到右还是从右到左选择? 我试过这个解决方案,但它不能正常工作

var last_selected_id="first";

$( ".selectable" ).selectable({
    selecting: function(event, ui) {
if(last_selected_id=="first")
{
//first, do nothing
}
else
{
    if(ui.selecting.id>last_selected_id)
    {
    //left to right
       console.log('left to right');
    }
    else
    {
      console.log('right to left');
    }
    last_selected_id = ui.selecting.id;
 },....

ID从左到右依次变大

2 个答案:

答案 0 :(得分:3)

您应该使用JQuery UI事件对象提供的鼠标指针的坐标。

  var startX;
  var direction;

  $( ".selectable" ).selectable({
    start: function( event, ui ) {
      startX = event.pageX;
      direction = "";
    },
    selecting: function( event, ui ) {
      direction = (event.pageX >= startX) ? "right" : "left";
    }
  });

答案 1 :(得分:0)

你错过了两个声明,其中一个在开头说ui.selecting.id> 0,而另外一个,如果ui.selecting.id低于last_element_id这个代码应该工作吗?

if(ui.selecting.id>0)
        {
            if(last_selected_id=="first")
                {
                console.log(last_selected_id);
                }
                else
                {
                    if(ui.selecting.id>last_selected_id)
                    {
                    //left to right
                       direction = "ltr";
                    }else if(ui.selecting.id<last_selected_id){
                    //right to left
                        direction = "rtl";
                    }

                }
                if(ui.selecting.id>0){
                    last_selected_id = ui.selecting.id;             
                }
            }
    },