Add and remove class different elements

[亡魂溺海] 提交于 2019-12-12 15:16:45

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/30382841/add-and-remove-class-different-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!