简体   繁体   English

可排序元素的项目在被拖动时丢失其CSS样式? (如果appendTo:'body')

[英]Item of sortable element loses its CSS styles when it is being dragged? (If appendTo: 'body')

I have a sortable list of items that returns results based on what the user types in the search box. 我有一个可排序的项目列表,它根据用户在搜索框中输入的内容返回结果。 The results always overflows and here i am using the following css for it: 结果总是溢出,在这里我使用以下css:

#list { overflow-x: visible; overflow-y: hidden; }

This allows me to have only a vertical scrollbar. 这允许我只有一个垂直滚动条。 I then drag the individual li's that are in the list over to a droppable area. 然后,我将列表中的各个li拖到可放置的区域。 The sortable functionality is added to the list using the JQuery below: 使用下面的JQuery将可排序功能添加到列表中:

$("#list").sortable({
   connectWith: ".connectedSortable",
   helper: 'clone',
   appendTo: 'body',
   zIndex: 999
});

The reason i use the appendTo: 'body' is to ensure that the item that is being dragged is on top of everything and will not be under the list's other items when being dragged. 我使用appendTo:'body'的原因是为了确保被拖动的项目位于所有内容之上,并且在被拖动时不会在列表的其他项目下。 However, whenever I drag any item from the list, the DIVs that are in the item will have their CSS styling gone. 但是,每当我从列表中拖动任何项目时,项目中的DIV都将使其CSS样式消失。

I understand that this is due to the fact that when the item is dragged, it is appended to 'body' and thus does not have any parent to inherit the original CSS styles. 我理解这是因为当拖动项目时,它被附加到'body' ,因此没有任何父项继承原始CSS样式。

My question is how do i style the dragged item back to its original styling to make sure it stays the same even if I am dragging/not dragging it? 我的问题是如何将拖动的项目设置回其原始样式,以确保即使我拖动/不拖动它也保持不变? through the events? 通过活动?

EDIT: 编辑:

Found the reason for the css messing up. 找到了css乱搞的原因。 It was a random br thrown in between two div's causing it to be interpreted differently when the item was being dragged and appended to the body. 它是在两个div之间抛出的随机br ,导致在拖动项目并将其附加到正文时对其进行不同的解释。

You have two options to sort the problem. 您有两种方法可以对问题进行排序。 One is to create your own helper with the function. 一个是使用该函数创建自己的帮助器。 This way you can style is any way you want, wrap it in an element, add classes, etc. 通过这种方式,您可以以任何方式设置样式,将其包装在元素中,添加类等。

The following demo shows the difference, the top one works, the bottom one is broken. 以下演示显示了差异,顶部的一个工作,底部的一个工作。 http://jsfiddle.net/hPEAb/ http://jsfiddle.net/hPEAb/

$('ul').sortable({
    appendTo: 'body',
    helper: function(event,$item){
        var $helper = $('<ul></ul>').addClass('styled');
        return $helper.append($item.clone());
    }
});

The other option is not to use append:'body' , but to play with zIndex. 另一种选择是不使用append:'body' ,而是使用zIndex。 Your zIndex:999 clearly has no effect, since the default value is 1000. :) The problem with zIndex is that it only matters for siblings, elements within the same parent. 你的zIndex:999显然没有效果,因为默认值是1000. :) zIndex的问题是它只对兄弟姐妹,同一父母中的元素很重要。 So if you have another sortable on your form with a greater zIndex than your current sortable, its items could easily be on top of your dragged one, regardless of the zIndex of your currently dragged item. 因此,如果您的表单上有另一个可排序的zIndex比当前可排序的更大,则无论您当前拖动的项目的zIndex如何,其项目都可以轻松地位于您拖动的项目之上。

The solution is to push your whole sortable on top when dragging starts and restore it when it stops: 解决方案是在拖动开始时将整个可排序项置于顶部,并在停止时将其恢复:

$('#mySortable').sortable({
    start: function(){
        // Push sortable to top
        $(this).css('zIndex', 999);
    },
    stop: function(){
        // Reset zIndex
        $(this).css('zIndex', 0);
    }
});

If the original value matters, you can even save the original zIndex with .data() and retrieve it afterwards. 如果原始值很重要,您甚至可以使用.data()保存原始zIndex并在之后检索它。

Thank you DarthJDG. 谢谢DarthJDG。 I am aware this thread is a little old but I hope to help others that had the same issue I did. 我知道这个帖子有点旧,但我希望能帮助那些遇到同样问题的人。

I had to edit your solution a little bit because the styling was off when appending the item to the helper. 我不得不稍微编辑你的解决方案,因为在将项目附加到帮助程序时样式已关闭。 I ended up just recreating the list element. 我最终只是重新创建了list元素。 Just in case others run into the same issue I did. 以防其他人遇到同样的问题我做了。

I added this into the area where I created the sortable. 我将其添加到我创建可排序区域。

I took the text out of the sortable and created a new list item with that as text. 我从可排序的文本中取出文本并创建了一个新的列表项,并将其作为文本。

Javascript: 使用Javascript:

       appendTo: 'body',
       helper: function(event,$item){
          console.log(event);
          var $helper = $('<ul class = "styled" id="' + event.originalEvent.target.id + '"><li>' + event.originalEvent.target.innerText + '</li></ul>');
          return $helper;
        }

I was then able to add custom styling to the draggable object, including custom text with out an issue. 然后,我可以为可拖动对象添加自定义样式,包括没有问题的自定义文本。 The styling I ended up using was that of JQuery Smoothness. 我最终使用的样式是JQuery Smoothness的样式。

CSS: CSS:

    .styled li{
      margin-left: 0px;
    }
    .styled{
      cursor:move;
      text-align:left;
      margin-left: 0px;
      padding: 5px;
      font-size: 1.2em;
      width: 390px;
      border: 1px solid lightGrey;
      background: #E6E6E6 url(https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png) 50% 50% repeat-x;
      font-weight: normal;
      color: #555;
      list-style-type: none;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ngx-bootstrap bs-sortable 在将菜单项拖动到可排序部分时显示先前拖动的项目 - ngx-bootstrap bs-sortable shows prevously dragged item when a menu item is dragged to the sortable section 样式未传递到body元素 - Styles not being passed to body element 分配“translateX”值时,元素会丢失其“rotateZ”值 - CSS - Element loses its “rotateZ” value when “translateX” value is assigned - CSS 仅在不拖动元素时获取输出 - Only fetch output when element is not being dragged 拖动文件时更改元素的 CSS 属性 - Changing CSS properties of an element while a file is being dragged 拖动元素的克隆,并使用可排序的jqueryui将其放在div区域中 - clone of dragged element and place it in dragged div area using sortable jqueryui 是应该在html元素还是body元素上设置全局css样式? - Should global css styles be set on the html element or the body element? 当父项可拖动时,防止拖动子元素 - Prevent child element from being dragged when parent is draggable 拖动锚点元素时停止锚点 href 导航? - Stopping anchor href navigation when the anchor element is being dragged? jQueryUI禁用/取消图标被拖动到可排序列表中吗? - jQueryUI disable / cancel icon from being dragged in a sortable list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM