【问题标题】:Create Carousel after 5 elements has been added添加 5 个元素后创建轮播
【发布时间】:2015-07-01 01:09:27
【问题描述】:

我正在使用 jQuery UI Droppable Cart Demo 插件,一旦 Dropped area 达到 5 个元素,我想创建一个轮播或箭头...如果我点击 Arrows,remaning 应该会显示


FIDDLE


HTML

<div id="products">
  <h2>Drag</h2>
  <div id="catalog">
      <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item 6</li>
      </ul>
  </div>
</div>

<div id="cart">
  <h2>Drop Here...</h2>
  <div class="ui-widget-content">
    <ol>
      <li class="placeholder">Add your items here</li>
    </ol>
  </div>
</div>

jQuery

$(function() {
    $( "#catalog li" ).draggable({
        appendTo: "body",
        helper: "clone"
    });
    $( "#cart ol" ).droppable({
        activeClass: "ui-state-default",
        hoverClass: "ui-state-hover",
        accept: ":not(.ui-sortable-helper)",
        drop: function( event, ui ) {
            $( this ).find( ".placeholder" ).remove();
            $( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
        }
    }).sortable({
        items: "li:not(.placeholder)",
        sort: function() {
            $( this ).removeClass( "ui-state-default" );
        }
    });
});

截图...


直到 5 个元素


拖动 5 个元素后

【问题讨论】:

    标签: jquery jquery-ui jquery-droppable


    【解决方案1】:

    你可以把你的 jQuery 函数改成这样:

    $(function() {
       $( "#catalog li" ).draggable({
           appendTo: "body",
           helper: "clone"
       });
       $( "#cart ol" ).droppable({
           activeClass: "ui-state-default",
           hoverClass: "ui-state-hover",
           accept: ":not(.ui-sortable-helper)",
           drop: function( event, ui ) {
               $( this ).find( ".placeholder" ).remove();
               $( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
    
               if($('.ui-droppable li').length > 5) {
                   //bind your carousel here
               }
    
           }
       }).sortable({
           items: "li:not(.placeholder)",
           sort: function() {
               $( this ).removeClass( "ui-state-default" );
           }
       });
    });
    

    【讨论】:

    • 优秀@Franky...这正是我正在寻找的...谢谢 TON
    • 嗨@Franky...仍然存在一些问题..抱歉迟到...每当我尝试拖动
    • 元素时,
    • 的 css 类将被删除.. .我怎样才能从拖拽的区域找回同一个班级...请找到更新的*** UPDATED FIDDLE HERE FOR YOUR REFERENCE ***
  • 使用这里的课程$( "&lt;li class='whatever_class'&gt;&lt;/li&gt;" ) 我想这就是你想要的,如果不让我知道的话。
  • @Franky 感谢您的回复...我很抱歉,但这应该为每个 li 元素提供相同的类...我找到了答案,即$(ui.draggable).appendTo(this);
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签