简体   繁体   English

jQuery在单击功能上添加和删除类的问题

[英]jQuery adding and removing classes issue on click function

I have created a Jquery toggle class function which adds and removes the class flipped on a click eventListner. 我创建了一个Jquery切换类函数,该函数添加和删除在单击eventListner上flipped的类。 click here . 请点击这里 At the moment this function is working when the user clicks on any of the elements wrapped in the .card class. 目前,当用户单击.card类中包装的任何元素时,此功能均起作用。 I when want to restrict where the user can click, therefore only allow the click function to work when the user clicks the .active-btn . 我想限制用户可以单击的位置,因此仅在用户单击.active-btn时才允许单击功能起作用。

I have made attempts to do this however once the card is flipped the active-btn class doesn't seem to work. 我已经尝试过这样做,但是一旦卡被翻转, active-btn类似乎就不起作用了。 by the looks of things the 'front div' is sitting on top of the 'back div Below is a snippet of my code … 从事物的外观来看,“前div”位于“后div”的顶部下面是我的代码的片段...

$('.active-btn').click(function(e){
             e.preventDefault();
            $card = $(this).closest('.card');
            $('.flipped').not($card).removeClass('flipped');
            $card.toggleClass('flipped');
        });

I basically change the z-index to -1 on .flip .card .face , and 我基本上将.flip .card .facez-index更改为-1 ,然后

.flip .card {
  -webkit-transform-style: flat;
}

The problem with your original is that the perserved-3d keep changing the z-index which causing issues with the order. 您的原件的问题是,perserved-3d不断更改z-index,这会导致订单出现问题。

z-index is canceled by setting transform(rotate) 通过设置transform(rotate)取消z-index

Here is the post which goes in more detail. 这是更详细的帖子。

You need to use the toggle element, right now the active btn will remove the class and won't add it after. 您需要使用toggle元素,现在活动的btn会删除该类,之后将不再添加它。

Do it like this: 像这样做:

$( ".active-btn" ).toggle(function() {
    e.preventDefault();
            $card = $(this).closest('.card');
            $('.flipped').not($card).removeClass('flipped');
            $card.toggleClass('flipped');
}, function() {
    e.preventDefault();
            $card = $(this).closest('.card');
            $('.flipped').not($card).addClass('flipped');
            $card.toggleClass('flipped'); );
});

use !hasClass (class has not exist) rather than not, 使用!hasClass(类不存在)而不是不使用,

$('.active-btn').click(function(e){
         e.preventDefault();
        $card = $(this).closest('.card');
       if (!$('.flipped').hasClass($card)) {
                 removeClass('flipped');
                 $card.toggleClass('flipped');
            }
    });

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

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