简体   繁体   English

CSS3过渡效果的问题

[英]Issue with CSS3 transition effect

With the reference from this Rotate image with onclick , I am trying to apply a css3 transition to a div, when the div element is clicked. 通过使用onclick旋转图像的引用,当单击div元素时,我试图将css3过渡应用于div。 The demo is here Everything is working perfect. 演示在这里一切运行正常。

HTML 的HTML

<div class="testRotate">Test rotate</div>

CSS 的CSS

.testRotate{
 width: 300px;
  height: 50px;
  border: 1px solid black;
  padding: 20px;
  margin: 0px auto;
  margin-top: 50px;
  -moz-transition: transform 1s;
  -webkit-transition: transform 1s;
  transition: transform 1s;
}

.testRotate.rotate{
  transform: rotate(360deg);
  -moz-transform: rotate(360deg);
  -webkit-transform: rotate(360deg);
}

JS JS

$(function(){
    $("div").click( function(){
        $(this).addClass("rotate");
        setTimeout(function(){$("div").removeClass("rotate");}, 1500);
    });
});

In this example, when onclicking the div, rotate class will be applied to it, so it will rotate for 360 degree, as defined in css. 在此示例中,当单击div时,将对其应用rotate类,因此它将旋转360度(如CSS中所定义)。 After sometimes we are removing the rotate class, so again the div is rotating back to its original position. 有时,我们删除了旋转类,因此div再次旋转回到其原始位置。

Now what i need is, when it clicked the element is has to rotate for 360 degree, but it should not suppose to rotate back once the rotate class got removed from it. 现在我需要的是,单击该元素时必须将其旋转360度,但是一旦旋转类从中移除,它就不应假定向后旋转

You can add a new class for transition and remove rotate as well as the class for transition. 您可以添加新的过渡类,并删除rotate和过渡类。

  $(function(){ $("div").click( function(){ $(this).addClass("testRotate rotate"); setTimeout(function(){$("div").removeClass("testRotate rotate");}, 1500); }); }); 
  .test { width: 300px; height: 50px; border: 1px solid black; padding: 20px; margin: 0px auto; margin-top: 50px; } .testRotate{ -moz-transition: transform 1s; -webkit-transition: transform 1s; transition: transform 1s; } .testRotate.rotate{ transform: rotate(360deg); -moz-transform: rotate(360deg); -webkit-transform: rotate(360deg); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="test testRotate">Test rotate</div> 

Fiddle Demo 小提琴演示

Fiddle 小提琴

$("div").click(function() {
    if (  $(this).css( "transform" ) == 'none' ){
        $(this).css("transform","rotate(360deg)");
    } else {
        $(this).css("transform","");
    }
});

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

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