javascript - How to keep divs position fixed even after deleting its neighbors? -


i have simple script creates , deletes divs when pushing buttons.

$("#add").click(function(){     $(document.body).append('<div class="bloc">'+(++_index)+'</div>'); });  $("#del").click(function(){     $(".bloc:first").remove(); }); 

my issue "delete" part, need divs stay @ current position when deleting anything.

and once recreate new ones, should take blank space left deleted ones.

could done through html/css ? if not, how can solve ?

please see code example here: https://jsfiddle.net/t6wvlyjb/

it difficult because once div has been removed dom rest of elements rearrange. 1 way create elements absolutely(which difficult) , store locations. while deleting not have problem while adding have add them stored locations.

another option change visibility of blocks instead of removing them. when adding instead of creating new ones first make blocks visible first , create new ones.

you can this

var _index = 0;  $(document).ready(function(){      $("#add").click(function(){         if(!$('.bloc.removed').length){             $(document.body).append('<div class="bloc">'+(++_index)+'</div>');         }else{             $(".bloc.removed").first().removeclass('removed');         }     });      $("#del").click(function(){         $(".bloc:not(.removed)").first().addclass('removed');     });  }); 

var _index = 0;    $(document).ready(function() {      $("#add").click(function() {      if (!$('.bloc.removed').length) {        $(document.body).append('<div class="bloc">' + (++_index) + '</div>');      } else {        $(".bloc.removed").first().removeclass('removed');      }    });      $("#del").click(function() {      $(".bloc:not(.removed)").first().addclass('removed');    });    });
.bloc {    background-color: red;    margin: 8px;    padding: 6px;    float: left;    width: 200px;    height: 64px;  }  .removed {    visibility: hidden;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <input type="button" value="add" id="add" />  <input type="button" value="del" id="del" />  <br/>

here demo https://jsfiddle.net/dhirajbodicherla/t6wvlyjb/1/


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -