Expand div HTML

by tanaan

HTML

<expandables></expandables>

CSS

expandables {
  max-width: 1200px;
  min-height: 100vh;
  display: block;
  margin: 0 auto;
}

.expandables {
  display: flex;
  flex-wrap: wrap;
  list-style-type: none;
  position: relative;
}

.expandables > div {
  flex-basis: calc(25% - 1rem);
  cursor: pointer;
}

@media (max-width: 991px) {
  .expandables > div {
    flex-basis: calc(33.33% - 1rem);
  }
}

@media (max-width: 767px) {
  .expandables > div {
    flex-basis: calc(50% - 1rem);
  }
}

@media (max-width: 539px) {
  .expandables > div {
    flex-basis: calc(100% - 1rem);
  }
}

.expandables > div > .content {
  display: none;
}

.expandables > .animator > .content {
  display: block;
}

.expandables > .animator {
  flex-basis: calc(100% - 1rem);
}


/* above are just basics, covering positioning and layout 
* below is a little basic styling
*/

body {
  margin: 0;
}

* {
  box-sizing: border-box;
}

.expandables {
  padding: .5rem;
}

.expandables>div {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid #ccc;
  margin: .5rem;
}

.expandables>.animator {
  background-color: #777;
  color: white;
  margin: 0 .5rem;
  cursor: initial;
}


/* now let's animate it */

.expandables >.animator {
  padding: 0;
  overflow: hidden;
  max-height: 0;
  transition: max-height .3s cubic-bezier(.4, 0, .2, 1);
  border: 0;
}

.expandables >.animator >.content {
  padding: 3rem 0;
}

.expandables .buttons {
  display: flex;
  justify-content: space-around;
  margin: 1rem
}

.expandables .buttons > * {
  padding: 3px 10px;
  border: 1px solid white;
  margin: 0 .5rem;
  cursor: pointer;
}


/* final touch: adding an arrow to the open div */

.expandables > div {
  position: relative;
}

.expandables > div::after {
  content: '';
  bottom: -2rem;
  left: 50%;
  width: 0;
  border: 1rem solid transparent;
  border-bottom-color: #777;
  position: absolute;
  transform: translate(-50%, 1px);
  opacity: 0;
  transition: all .3s cubic-bezier(.4, 0, .2,...

JavaScript

var htmlFile1 = $( '.content' ).get( 'file1.html');

var expandables = [{
    title: 'Title grid 1',
    content: htmlFile1
  }, {
    title: 'Title grid 2',
    content: 'Content 2'
  }],
  buttons = [{
    text: 'Previous',
    class: 'btn-prev'
  }, {
    text: 'Next',
    class: 'btn-next'
  }, {
    text: 'Close',
    class: 'btn-close'
  }],
  expContainer = $('<div />', {
    class: 'expandables'
  }),
  btnContainer = $('<div />', {
    class: 'buttons'
  });

$.each(expandables, function(i, e) {
  var item = $('<div />', {
    class: 'item',
    html: '<div><h2>' + e.title + '</h2></div>' +
      '<div class="content"><p>' + e.content + '</p></div></div>'
  })
  expContainer.append(item);
});
expContainer.appendTo($('expandables'));

$.each(buttons, function(i, e) {
  var button = $('<div />', {
    class: e.class,
    text: e.text
  });
  btnContainer.append(button);
});


$('.expandables').on('click', '.item', function(e) {
  closeItems();
  var item = $(e.target).closest('.item'),
    content = item.find('.content'),
    nextItem = item.next(),
    nextItemPosition = nextItem.position();
  item.addClass('open');
  while (nextItemPosition && (nextItemPosition.left > 20)) {
    nextItem = nextItem.next();
    nextItemPosition = nextItem.position();
  };
  var clone = $('<div />', {
    class: 'animator',
    html: content.clone()
  });
  clone.append(btnContainer.clone());

  if (nextItem.is('.item, .animator')) {
    clone.insertBefore(nextItem);
    openItem(clone);
  } else {
    clone.appendTo('.expandables');
    openItem(clone);
  }
}).on('click', '.btn-prev', function(e) {
  $('.item.open').prevAll('.item').first().click();
}).on('click', '.btn-next', function(e) {
  $('.item.open').nextAll('.item').first().click();
}).on('click', '.btn-close', function(e) {
  closeItems();
});

$(window).on('resize', function() {
  closeItems();
});

function closeItems() {
  var opened = $('.expandables>.animator');
  opened.css('max-height', 0);
 ...