javascript - Remove and then add same element to Packery JS -
i want remove 2 elements , add them grid built packery when button clicked. code given below:
$('.append-button').on( 'click', function() { // create new item elements var $items = getitemelement().add( getitemelement() ).add( getitemelement() ); var $items2 = $('.big'); var el = $items2; // remove , append elements container $grid.packery('remove', $items2).packery('layout') $grid.append( $items2 ) // add , lay out newly appended elements .packery( 'appended', $items2 ); }); if this, elements referenced $items2 appear on reload of packery disappears. codepen demo here. know how can delete 2 elements , add them in stable manner? in advance!!
i added .clone() function duplicate element before removing it. removed old one. worked quite well.
new codepen:
http://codepen.io/anon/pen/qrzxwg
// external js: packery.pkgd.js var $grid = $('.grid').packery({ itemselector: '.grid-item' }); $('.append-button').on( 'click', function() { // create new item elements var $items = getitemelement().add( getitemelement() ).add( getitemelement() ); var $new = $('.big').clone(); var $items2 = $('.big'); var el = $items2; // append elements container $grid.packery('remove', $items2).packery('layout') $grid.append( $new ) // add , lay out newly appended elements .packery( 'appended', $new ); }); // make <div class="grid-item grid-item--width# grid-item--height#" /> function getitemelement() { var $item = $('<div class="grid-item"></div>'); // add width , height class var wrand = math.random(); var hrand = math.random(); var widthclass = wrand > 0.85 ? 'grid-item--width3' : wrand > 0.7 ? 'grid-item--width2' : ''; var heightclass = hrand > 0.85 ? 'grid-item--height3' : hrand > 0.5 ? 'grid-item--height2' : ''; $item.addclass( widthclass ).addclass( heightclass ); return $item; } /* unmodified minified */ *{box-sizing: border-box;}body{font-family: sans-serif;}/* ---- grid ---- */.grid{background: #ddd; max-width: 1200px;}/* clear fix */.grid:after{content: ''; display: block; clear: both;}/* ---- .grid-item ---- */.grid-item{float: left; width: 80px; height: 80px; background: #c09; border: 2px solid hsla(0, 0%, 0%, 0.5);}.grid-item--width2{width: 160px;}.grid-item--height2{height: 160px;}.grid-item--width3{width: 240px;}.grid-item--height3{height: 240px;}button{font-size: 20px;} <!-- left unmodified it's minified --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="http://mfzy.co/packery.pkgd.js"></script><div class="grid"> <div class="grid-item grid-item--width2"></div><div class="grid-item grid-item--height2"></div><div class="grid-item big"></div><div class="grid-item big"></div><div class="grid-item grid-item--height2"></div></div><p><button class="append-button">append items</button></p>
Comments
Post a Comment