多次应用css转换

时间:2015-09-04 06:12:37

标签: jquery css css-transitions

我希望这很简单,但我一直在努力。

我想将.content的可见性+不透明度转换为0.(这样可行)。

然后点击按钮(计数器c = 1)再次重新,然后将其.content的可见性+不透明度转换为0.(这只是不起作用 - 我甚至试图过渡到中性。

JSFiddle此处)

感谢您的帮助。



    var c = 0;

    var $content = $('.content');


    $('.button').click(function() {
        $content
//                .removeClass('hidden-transition')
//                .addClass('neuter-transition')
//                .addClass('visible')
                .html('new stuff: ' + c)
                .removeClass('visible')
                .addClass('hidden-transition');
        c += 1;
    });

       .actionWrapper {
            width: 10em;
            background: cadetblue;
            color: white;

        }

        .content {
            padding: 1em;
            overflow: auto;
            height: 4em;
        }

        .button {
            background-color: lightsalmon;
            height: 2em;
            width: 5em;
            line-height: 2em;
            text-align: center;
            margin-top: 1em;
        }
        .button:hover {
            cursor: pointer;
        }


        .hidden-transition {
            visibility: hidden;
            opacity: 0;
            -webkit-transition: visibility 0s 5s, opacity 5s linear;
            -moz-transition: visibility 0s 5s, opacity 5s linear;
            -o-transition: visibility 0s 5s, opacity 5s linear;
            transition: visibility 0s 5s, opacity 5s linear;
        }

        .neuter-transition {
            -webkit-transition: none;
            -moz-transition: none;
            -o-transition: none;
            transition: none;    
        }
        
        .visible {
            visibility: visible;
            opacity: 1;
        }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="actionWrapper">
    <div class="content">
    </div>
</div>

<div class="button">click</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

你有3个选项

首先使用超时,

var c = 0;
var $content = $('.content');
$('.button').click(function () {
    var rmc = 'visible',
        adc = 'hidden-transition';
    if (c == 0) {
        setTimeout(function () {
            c = 0;
            adc = 'visible';
            rmc = 'hidden-transition';
            $content.html('new stuff: ' + c)
                .removeClass(rmc)
                .addClass(adc);
        }, 5000);
    }
    $content.html('new stuff: ' + c)
        .removeClass(rmc)
        .addClass(adc);
    c++;
});

&#13;
&#13;
var c = 0;
var $content = $('.content');
$('.button').click(function() {
  var rmc = 'visible',
      adc = 'hidden-transition';
  if (c == 0) {
    setTimeout(function() {
      c = 0;
      adc = 'visible';
      rmc = 'hidden-transition';
      $content.html('new stuff: ' + c)
      .removeClass(rmc)
      .addClass(adc);
    }, 5000);
  }
  $content.html('new stuff: ' + c)
  .removeClass(rmc)
  .addClass(adc);
  c++;
});
&#13;
.actionWrapper {
  width: 10em;
  background: cadetblue;
  color: white;
}
.content {
  padding: 1em;
  overflow: auto;
  height: 3em;
}
.button {
  background-color: lightsalmon;
  height: 2em;
  width: 5em;
  line-height: 2em;
  text-align: center;
  margin-top: 1em;
}
.button:hover {
  cursor: pointer;
}
.hidden-transition {
  visibility: hidden;
  opacity: 0;
  -webkit-transition: visibility 0s 5s, opacity 5s linear;
  -moz-transition: visibility 0s 5s, opacity 5s linear;
  -o-transition: visibility 0s 5s, opacity 5s linear;
  transition: visibility 0s 5s, opacity 5s linear;
}
.neuter-transition {
  -webkit-transition: none;
  -moz-transition: none;
  -o-transition: none;
  transition: none;
}
.visible {
  visibility: visible;
  opacity: 1;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="actionWrapper">
  <div class="content"></div>
</div>
<div class="button">click</div>
&#13;
&#13;
&#13;

如果按钮为c而没有等待,

,则第二次重置clicked
var c = 0;
var $content = $('.content');
$('.button').click(function () {
    var rmc = 'visible',
        adc = 'hidden-transition';
    if (c >= 5) {
            c = 0;
            adc = 'visible';
            rmc = 'hidden-transition';                
    }
    $content.html('new stuff: ' + c)
        .removeClass(rmc)
        .addClass(adc);
    c++;
});

&#13;
&#13;
var c = 0;
var $content = $('.content');
$('.button').click(function() {
  var rmc = 'visible',
      adc = 'hidden-transition';
  if (c >= 5) {
    c = 0;
    adc = 'visible';
    rmc = 'hidden-transition';
  }
  $content.html('new stuff: ' + c)
  .removeClass(rmc)
  .addClass(adc);
  c++;
});
&#13;
.actionWrapper {
  width: 10em;
  background: cadetblue;
  color: white;
}
.content {
  padding: 1em;
  overflow: auto;
  height: 3em;
}
.button {
  background-color: lightsalmon;
  height: 2em;
  width: 5em;
  line-height: 2em;
  text-align: center;
  margin-top: 1em;
}
.button:hover {
  cursor: pointer;
}
.hidden-transition {
  visibility: hidden;
  opacity: 0;
  -webkit-transition: visibility 0s 5s, opacity 5s linear;
  -moz-transition: visibility 0s 5s, opacity 5s linear;
  -o-transition: visibility 0s 5s, opacity 5s linear;
  transition: visibility 0s 5s, opacity 5s linear;
}
.neuter-transition {
  -webkit-transition: none;
  -moz-transition: none;
  -o-transition: none;
  transition: none;
}
.visible {
  visibility: visible;
  opacity: 1;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="actionWrapper">
  <div class="content"></div>
</div>
<div class="button">click</div>
&#13;
&#13;
&#13;

第三(我发现它是2中最好的)从Detecting CSS Animation Completion with JavaScript

引用

&#13;
&#13;
/* From Modernizr */
function whichTransitionEvent(){
    var t;
    var el = document.createElement('fakeelement');
    var transitions = {
      'transition':'transitionend',
      'OTransition':'oTransitionEnd',
      'MozTransition':'transitionend',
      'WebkitTransition':'webkitTransitionEnd'
    }

    for(t in transitions){
        if( el.style[t] !== undefined ){
            return transitions[t];
        }
    }
}
var c = 0;
var $content = $('.content');

/* Listen for a transition! */
var transitionEvent = whichTransitionEvent();
transitionEvent && $content.on(transitionEvent, function() {
    c=0;
	$content.html('new stuff: ' + c)
        .removeClass('hidden-transition')
        .addClass('visible');
});


$('.button').click(function () {
    $content.html('new stuff: ' + c)
        .removeClass('visible')
        .addClass('hidden-transition');
    c++;
});
&#13;
.actionWrapper {
  width: 10em;
  background: cadetblue;
  color: white;
}
.content {
  padding: 1em;
  overflow: auto;
  height: 3em;
}
.button {
  background-color: lightsalmon;
  height: 2em;
  width: 5em;
  line-height: 2em;
  text-align: center;
  margin-top: 1em;
}
.button:hover {
  cursor: pointer;
}
.hidden-transition {
  visibility: hidden;
  opacity: 0;
  -webkit-transition: visibility 0s 5s, opacity 5s linear;
  -moz-transition: visibility 0s 5s, opacity 5s linear;
  -o-transition: visibility 0s 5s, opacity 5s linear;
  transition: visibility 0s 5s, opacity 5s linear;
}
.neuter-transition {
  -webkit-transition: none;
  -moz-transition: none;
  -o-transition: none;
  transition: none;
}
.visible {
  visibility: visible;
  opacity: 1;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="actionWrapper">
    <div class="content"></div>
</div>
<div class="button">click</div>
&#13;
&#13;
&#13;

您可以阅读有关css animation callback here

的更多信息