.hasClass to expand <div>

Asked

Viewed 30 times

0

Wanted to expand the <div>, but I can’t. Where I’m going wrong?

In principle it will be this, but I want to make a screen with 100% width with this effect, expanding and returning with another click.

Expand box with CSS Transitions

    <div class="expandir-boxes">
        <div class="expandir-box">
            <h2>Box 1</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut euismod eget turpis quis convallis.</p>
        </div>

        <div class="expandir-box">
            <h2>Box 2</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut euismod eget turpis quis convallis.</p>
        </div>

        <div class="expandir-box">
            <h2>Box 3</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut euismod eget turpis quis convallis.</p>
        </div>

        <div class="expandir-box">
            <h2>Box 4</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut euismod eget turpis quis convallis.</p>
        </div>

        <div class="expandir-box">
            <h2>Box 5</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut euismod eget turpis quis convallis.</p>
        </div>

        <div class="expandir-box">
            <h2>Box 6</h2>
            <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut euismod eget turpis quis convallis.?</p>
        </div>
    </div>
</div>

CSS:

.expandir-boxes {
  position: relative;
}

.expandir-box {
  width: 30%;
  height: 130px;
  float: left;
  margin: 0 3% 20px 0;
  border: 1px solid #999;
  border-radius: 12px;
  padding: 10px;
  box-sizing: border-box;
  -webkit-transition: all .3s ease-in-out;
  -moz-transition: all .3s ease-in-out;
  -o-transition: all .3s ease-in-out;
  -ms-transition: all .3s ease-in-out;
  transition: all .3s ease-in-out;

  &.open {
    border-color: #dd0000;
    width: 96%;
    height: 180px;
    margin: 0;
  }

  &.out {
    width: 0;
    height: 0;
    overflow: hidden;
    border: none;
    padding: 0;
    margin: 0;
    opacity: 0;
  }
}

Javascript:

$(document).ready(function() {
  $('.expandir-box').click(function(e) {
    alert("Funcionou");
    if($(this).hasClass('open')) {
      $('.expandir-box.out').not($(this)).removeClass('out');
      $(this).removeClass('open');
    }
    else {
      $('.expandir-box').not($(this)).addClass('out');
      $(this).addClass('open');
    }
  });
});
  • Everton, do you display the small box, when you click it expands and when you click it again "some"? Because that’s the effect you set in CSS. That’s right?

  • Bro, I wish I had something like this https://codepen.io/RobotsPlay/pen/Lniyd

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.