在使用jQuery UI draggable时给元素一个临时的固定宽度?

时间:2022-05-26 09:50:23

I have some elements that have a relative width. however, when I drag one of these elements using the jquery UI's draggable() method, it shifts based on where I'd like to drag it. I tried the following code, attempting to use the start and stop callbacks:

我有一些具有相对宽度的元素。但是,当我使用jquery UI的draggable()方法拖动其中一个元素时,它会根据我要拖动它的位置而移动。我尝试了以下代码,尝试使用start和stop回调:

HTML:

<ul id='item-list'>  
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>

CSS

#item-list {
  width: 40%;
}

#item-list li {
  width:  100%;
}

JavaScript

$("#item-list li").draggable({
  revert : true,
  helper : 'clone',
  start : function() {
    $(this).css('width',$(this).width());
  },
  stop : function() {
    $(this).css('width','100%');
  }
});

However, this only seems to work the second time I drag objects. is there some way to get this to work properly?

但是,这似乎只是第二次拖动对象时才起作用。有什么方法可以让它正常工作?

1 个解决方案

#1


8  

Is that it? http://jsfiddle.net/CGQkB/

是吗? http://jsfiddle.net/CGQkB/

The trick is to address the of the ui.helper not just $(this) since that just points to the original <li> tag.

诀窍是解决ui.helper而不只是$(this),因为它只指向原始的

  • 标签。

  • #1


    8  

    Is that it? http://jsfiddle.net/CGQkB/

    是吗? http://jsfiddle.net/CGQkB/

    The trick is to address the of the ui.helper not just $(this) since that just points to the original <li> tag.

    诀窍是解决ui.helper而不只是$(this),因为它只指向原始的

  • 标签。