简体   繁体   English

基于切换时删除/添加类

[英]Remove / add class when based on toggle

I have a column that slides to the left on a click function. 我有一列在click功能上向左滑动。 I then add the class of w to the container to change its width once the column slides out. 然后,在列滑出后,将w类添加到容器中以更改其宽度。 What I am trying to do is add the class of e when it slides back in and then remove the class of w by wrapping it in the toggle functions, but the container still contains the class of w and won't add the class of e . 我想做的是在回滑时添加e的类,然后通过将其包装在toggle函数中来删除w的类,但是容器仍然包含w的类,并且不会添加e的类。 Thoughts? 有什么想法吗?

FIDDLE: http://jsfiddle.net/QDUQk/1926/ 内容: http : //jsfiddle.net/QDUQk/1926/

.container {
  border: 1px solid #000;
  width: 200px;
}

.container.e {
  width: 80%;
}

.container.w {
  width: 100%;
}

<div class="togl">Menu</div>
<div class="col">
  SLIDE ME SLIDE ME PLX PLX
</div>
<div class="container">
</div>

$('.togl').click(function() {
  if ($('.container').is(':visible')) {
    $('.col').toggle('slide', {
      direction: 'left'
    }, 1000, function() {
      $('.container').addClass('w');
      $('.container').removeClass('e');
    });
  } else {
    $('.col').toggle('slide', {
      direction: 'left'
    }, 1000, function() {
      $(".container").removeClass('w');
      $(".container").addClass('e')
    });
  }
});

The visibility of .container does not change from true . .container的可见性不会从true更改。 Check for "w" className at .is() .is()检查"w" className

$('.togl').click(function() {
  if (!$('.container').is('.w')) {
    $('.col').toggle('slide', {
      direction: 'left'
    }, 1000, function() {
      $('.container').addClass('w');
      $('.container').removeClass('e');
    });
  } else {
    $('.col').toggle('slide', {
      direction: 'left'
    }, 1000, function() {
      $(".container").removeClass('w');
      $(".container").addClass('e')
    });
  }
});

jsfiddle http://jsfiddle.net/QDUQk/1927/ jsfiddle http://jsfiddle.net/QDUQk/1927/

Your container visibility does not change on click. 您的容器可见性在点击时不会改变。 Try following, 尝试跟随,

$('.togl').click(function() {
   if ($('.container').hasClass('e')) {
      $('.col').toggle('slide', {
                      direction: 'left'
      }, 1000, function() {
    $('.container').addClass('w');
    $('.container').removeClass('e');
  });
} else {
$('.col').toggle('slide', {
  direction: 'left'
}, 1000, function() {
  $(".container").removeClass('w');
  $(".container").addClass('e')
});
}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM