0

I have 5 pieces of a image in a container which i have to drop in another container to complete the image. After the image is dropped in another container i want to add a different class to that image depending upon its ID. so that this image piece will stick to the previous image present in this container.

I am able to implement the drag and drop event and add a single class to each image after drop event. what changes i need to make in my code to add a different class to eachimage depending upon its ID

My code

  <style>

   .add-style {
     position:absolute;
    }
   .north-style {
     width: 375px;top: 10px;left: 11px;

    }
   .south-style {
     width: 375px;top: 158px;left: 11px;

    }
    .east-style {
     width: 163px;top: 10px;left: 223px;height: 250px;

    }
    .west-style {
      width: 163px;top: 9px;left: 11px;height: 249px;
    }
    .center-style {
      width: 126px;top: 71px;left: 135px;
    }
 </style>

 <script>
   $(function() {

     $( "#sortable1" ).sortable({
        connectWith: "div"
       });

     $( "#sortable2" ).sortable({
        connectWith: "div",
        stop: function( event, ui ) {
           ui.item.addClass( "add-style" )

         }
     });

     $( "#sortable1, #sortable2" ).disableSelection();
    });
   </script>

Bodypart of code

 <body>
   <div class="row-fluid" >
     <div class="span4">
       <div class="span1"></div>
       <div id="sortable1" class="well sidebar-nav sidebar-nav-fixed span11">

        <legend>Collect Coupon parts</legend>
        <span>1st part</span>
        <img id="north-img" class="ui-state-highlight" src="images/demo/north.png" >
        <span>2nd part</span>
        <img id="south-img" class="ui-state-highlight" src="images/demo/south.png" >
        <span>3rd part</span>
        <img id="east-img" class="ui-state-highlight" src="images/demo/east.png" >
        <span">4th part</span>
        <img id="west-img" class="ui-state-highlight" src="images/demo/west.png" >
        <span>5th part</span>
        <img id="center-img" class="ui-state-highlight" src="images/demo/center.png">

    </div>
  </div>
  <div id="sortable2"  class=" well sidebar-nav sidebar-nav-fixed span8">

    <legend>Canvas section</legend>
  </div>
  </div>
  </body>
4

1 回答 1

1

首先将您的课程更改为以下内容。

显示了两个示例,也更改为其他 3 个。

     .north-img-style {
         width: 375px;top: 10px;left: 11px;
       }
     .south-img-style {
         width: 375px;top: 158px;left: 11px;
     }

然后在这部分代码中添加这两行

     stop: function( event, ui ) {

          var theID = ui.item.attr('id');
          ui.item.addClass(theID + '-style');

     }

var获取元素的 ,然后我们只是 addClass 具有相同的 ID,但添加 -style 到它theIDid所以这将适用于所有五个不同的课程。


编辑

是的,您一次可以添加多个课程。只需用逗号分隔,如下所示:

          var theID = ui.item.attr('id');
          ui.item.addClass(theID + '-style', 'add-style');
于 2014-01-22T07:31:48.700 回答