Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add and remove class different elements

So im currently learning jquery and a little bit of tweenlite for animations (I wanna keep it basic). So im currently building a portfolio grid but I wanna add on a click of an element that the other element is fading in (sliding from right it doesn't matter).

But I can't find a way to make it work that 1 element have 1 box to show and the other element have a different box to show without coping the code over and over and change a simple number everytime, there must be a way to make it work without going to repeat the code over and over.

I created a codepen to show where my struggles are.

I hope I'm pretty clear with describing this problem :)

HTML

  <div class="box">
    <div class="show">Show 1</div>
  </div>

  <div class="bigbox">
    <div class="removeit">
      <div class="bigshow">Bigshow 1</div>
    </div>
  </div>

  <div class="box">
    <div class="show">Show 2</div>
  </div>

  <div class="bigbox">
    <div class="removeit">
      <div class="bigshow">Bigshow 2</div>
    </div>
  </div>

</div>

CSS

.container {
  overflow: auto;
  margin: 0 auto;
  width:500px;
}

.box {
  height:200px;
  width:200px;
  background:yellow;
  text-align:center;
  cursor:pointer;
  margin:0 auto;
  float:left;
  margin-right:50px;
}

.bigbox {
  height:100%;
  width:100%;
  background-color: grey;
  z-index:100;
  left:0;
  opacity: 0;
  position: fixed;
  display:none;
  top:0;
  .removeit {
    height:100px;
    width: 100px;
    top: 0;
    right:0;
    background-color: blue;
    margin:0 auto;
    cursor:pointer;
  }
}

  .show {
    display:block;
  }
  .noscroll {
    overflow:hidden;
  }

Javascript

$(".box").click(function(){
    $(".bigbox").addClass("show");
    TweenLite.to($('.bigbox'), 0.5, {
        opacity:1,
        autoAlpha:1
    });
});

$(".removeit").click(function(){
    TweenLite.to($('.bigbox'), 0.5, {
        autoAlpha:0,
        opacity:0
    });
});

The codepen

http://codepen.io/denniswegereef/pen/MwjOXP

like image 388
Dennis Avatar asked Sep 16 '26 03:09

Dennis


1 Answers

As I mentioned in comments, I think it is possible by finding the common ground between box and bigbox and if we are to not modify HTML. That common ground should be the index value from their respective classes.

  • So store a clickedIndex variable first, inside the click handler like so: var clickedIndex=$('.box').index($(this));.
  • And then feed this clickedIndex to get a selective bigbox like so: var bigbox=$(".bigbox").eq(clickedIndex);.
  • And finally, use this bigbox variable further to fade in or out.

Here is your modified JavaScript:

var bigbox = null;
var clickedIndex = -1;
var boxElements=$(".box");
var bigboxElements=$(".bigbox");
var removeItElements=$(".removeit");
boxElements.click(function() {
  clickedIndex = boxElements.index($(this));
  bigbox = bigboxElements.eq(clickedIndex);
  bigbox.addClass("show");
  TweenLite.to(bigbox, 0.5, {opacity: 1,autoAlpha: 1});
});

removeItElements.click(function() {
  clickedIndex = removeItElements.index($(this));
  bigbox = bigboxElements.eq(clickedIndex);
  TweenLite.to(bigbox, 0.5, {autoAlpha: 0,opacity: 0});
});

The only problem with this approach is that it is very dependant on the order with which the HTML has been laid out.

like image 199
Tahir Ahmed Avatar answered Sep 17 '26 16:09

Tahir Ahmed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!