Frage

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>
War es hilfreich?

Lösung

First change your classes to the following.

Two example shown, change for the other 3 too.

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

Then in this part of your code add these two lines

     stop: function( event, ui ) {

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

     }

the var theID grabs the id of the element, and then we just addClass with the same ID but add -style to it. So this will work for all five of your different classes.


EDIT

Yep, you can add more than one class at a time. Just separate with a comma like so:

          var theID = ui.item.attr('id');
          ui.item.addClass(theID + '-style', 'add-style');
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top