Question

I have a sortable list of playing cards. In order to just show the tops of the cards, I have given each card a negative bottom margin. Once I do this, the jQuery sortable gets very flickery and difficult to use. How can I eliminate this filcker? On top of the flicker, how can I get the proper vertical alignment for dragging? It seems that I have to go WAY above or below the list to get it to move the placeholder to those sides of the list.

I have put my code at http://jsfiddle.net/otac0n/wDTwX/ so that you can get a feel for it, but here is the gist of it:

// HTML
<div class="deck" data-bind="sortable: { data: Cards, options: { placeholder: 'card', cursorAt: { left: 5, top: 5 }, tolerance: 'pointer' } }">
    <div class="card" data-bind="text: Name, style: { background: Color }"></div>
</div>

// CSS
.deck
{
    margin: 10px;
    padding: 0 0 130px 0;
}

.card
{
    width: 100px;
    height: 150px;
    border: 1px solid black;
    border-radius: 8px;
    background: White;
    color: White;
    margin: 0 0 -130px 0;
    padding: 5px;
}

// JS
var vm = {
    Cards: ko.observableArray([
        { Name: "Red", Color: "#f00" },
        { Name: "Green", Color: "#0f0" },
        { Name: "Blue", Color: "#00f" },
    ])
};

ko.applyBindings(vm);
Was it helpful?

Solution

First things first, you have a run away argument there.

sortable: { data: Cards, 
    options: { placeholder: 'card', cursorAt: { left: 5, top: 5 }, }
    ,tolerance: 'pointer'}

The tolerance should be inside the options array, so its not being picked up :) ie.

sortable: { 
    data: Cards, 
    options: { 
        placeholder: 'card', 
        cursorAt: { left: 5, top: 5 },
        tolerance: 'pointer'
    }
}

About the flickering, it seems to me like its because the events are bubbling though the elements (which are overlapping). Try and come up with a way of ensuring they only get sorted if they are intended to be :)

Edit: this problem seems similar: Dealing with overlapping jQuery sortable lists

OTHER TIPS

I struggled with a flickering sortable too. Noted that it only flickers under 2 conditions:

  1. There's a connectWith option being used
  2. The parent container (in my case, the <ul> tag) has a height or anything else like a <div style="clear:both"> forcing it to have a height.

In the other hand, if the connected UL has no height, we can't actually drop anything inside it. But whenever the UL got a height, it flickers. So I put a height of only 10px and it worked. If that won't work, try to use with overflow: visible or overflow: hidden on UL's parent. Hope it helps.

UPDATE:

It wouldn't work if there are more than one line of items. I could get it fixed by doing that:

over: function( event, ui ) { $('#my_sortable_list').css('overflow', 'visible'); }

And then

stop: function() { $('.selector_for_all_sortable_lists').css('overflow', 'hidden'); }

That on both connected lists

In my actual case, the answer was to use overflow: visible rather than negative margins to get the overlapping effect.

This magically made everything work!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top