官术网_书友最值得收藏!

Sorting buttons within a group

We can use the sortable() interaction widget to provide the user which some flexibility. Why not let the user move buttons around? Especially given the small amount of code it takes.

Getting ready

We'll use a list to organize our buttons, as follows:

<ul>
    <li><a href="#">Button 1</a></li>
    <li><a href="#">Button 2</a></li>
    <li><a href="#">Button 3</a></li>
</ul>

We'll use the following CSS to fix the list layout to better display button widgets.

ul {
    list-style-type: none;
    padding: 0;
}

li {
    margin: 4px;
}

How to do it...

The JavaScript code to make this happen is actually quite miniscule—we create the buttons, then we apply the sortable interaction widget.

$(function() {
    $( "a" ).button();
    $( "ul" ).sortable({
        opacity: 0.6
    });
});

At this point, we're able to drag-and-drop buttons—but only within the designated container element, in this case, ul.

How it works...

The first thing we're doing in this example, once the document is ready, is creating the button widgets. We're using anchors as the underlying element, which works just as well as button elements. You'll notice too, that we've structured the button widgets on the page inside of an unordered list. The styles added to the page just remove the list indentation and the bullets. But the ul element is what we're targeting for the sortable interaction. By default, the sortable widget looks for all child elements and makes them the sortable items, in our case, these are li elements. The opacity option specified in the example tells sortable to change the visual opacity of the element being dragged.

主站蜘蛛池模板: 石河子市| 和田市| 当雄县| 公主岭市| 凤冈县| 霍山县| 台中县| 清新县| 赤壁市| 朝阳市| 凤山市| 南江县| 盘锦市| 浦县| 温泉县| 平山县| 河池市| 香河县| 灌南县| 厦门市| 合江县| 肇州县| 元江| 焉耆| 潮州市| 海安县| 肥东县| 航空| 灌云县| 武川县| 鄂温| 洛阳市| 台中市| 杨浦区| 望江县| 禄丰县| 出国| 双流县| 乐清市| 山阴县| 辉南县|