简体   繁体   English

悬停上的jQuery可排序列表句柄

[英]jQuery Sortable List Handles on Hover

I'm creating a list with handles for sorting. 我正在创建一个包含句柄的列表。 I don't like the aesthetics of having 20 handles visible, so I'm trying to make the handles appear only when the mouse hovers over a list item. 我不喜欢有20个手柄可见的美学,所以我试图只在鼠标悬停在列表项上时才显示手柄。

This was my first attempt: 这是我的第一次尝试:

jsFiddle #1 jsFiddle#1

As you can see, hovering over items in the list creates a rather jarring movement and misalignment of list items. 如您所见,将鼠标悬停在列表中的项目上会产生相当不稳定的移动和列表项的错位。 To fix this, I've created a blank 16px image that I use to replace the handle when it's not visible. 为了解决这个问题,我创建了一个空白的16px图像,用于替换不可见的句柄。 It creates a much nicer user experience than hovering, as you can see here: 它创建了比悬停更好的用户体验,如下所示:

jsFiddle #2 jsFiddle#2

$(this).prepend("<img src=http://i.imgur.com/tzGrVLc.png class=\"blank-sprite\" / width=16 height=16 border=0>");

The problem is that during sorting the 16px image often disappears, leaving things out of alignment. 问题是在排序过程中16px图像经常会消失,使事情不对齐。 (I'd post an image but I don't have the reputation.) It doesn't always happen, but seems to happen more frequently when I'm sorting very quickly. (我发布了一张图片,但我没有声誉。)它并不总是发生,但是当我排序很快时,似乎更频繁地发生。

I'd love to know why this happens and how to fix it. 我很想知道为什么会发生这种情况以及如何解决这个问题。 Thanks! 谢谢!

You should set position css property of icon element to absolute. 您应该将icon元素的位置css属性设置为absolute。 This is to give you the idea: 这是为了给你这个想法:

SEE DEMO 看看演示

var $icon = $("<span class=\"ui-icon ui-icon-grip-dotted-vertical\" style=\"display:inline-block\" id=\"handle\" /></span>").css({
            position:'absolute',
            top:$(this).offset().top+5,
            left:$(this).offset().left-10
        });
$(this).prepend($icon); 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM