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

  • jQuery UI Cookbook
  • Adam Boduch
  • 261字
  • 2021-08-13 16:40:52

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.

主站蜘蛛池模板: 富蕴县| 龙泉市| 靖边县| 大渡口区| 富民县| 建平县| 禹城市| 滨州市| 莱州市| 阿城市| 肇东市| 神池县| 清新县| 鸡东县| 刚察县| 古田县| 富锦市| 石家庄市| 邻水| 根河市| 焉耆| 读书| 延寿县| 绥宁县| 东光县| 色达县| 海淀区| 南丰县| 古蔺县| 祥云县| 旬邑县| 庆安县| 岑巩县| 阜城县| 阿巴嘎旗| 南京市| 永安市| 同江市| 桦甸市| 万全县| 邯郸市|