添加类时停止CSS动画

时间:2017-03-19 15:24:27

标签: javascript jquery css animation

你好我的问题是当汽车进入屏幕时如何停止车轮转动,每当有任何车进入屏幕时请停止车轮转动。

我的代码:

c#
$(document).ready(function () {
    $('.wheelEleContainer .slectWheel').click(function (e) {
        e.preventDefault();
        var getImgWheel = $(this).find('img').attr('src');
        $('.car .wheelInCar').css('background-image',
          'url(' + getImgWheel + ')'
        );
    });
    var running = false;
    $('.car-model').click(function (e) {
        e.preventDefault();
        var index = $(this).data("index");

        var $pre = $('.car-image.current');
        var $dom = $('.car-image[data-car-index="' + index + '"]');
        if ($dom.hasClass("current") || running) {
            return;
        }
        var current_pull = parseInt($('.current').css('transform').split(',')[5]);
        
        running = true;
        $dom.addClass('current');
        $pre.addClass('left').removeClass('current');
        setTimeout(function () {
            if (current_pull == 0) {
                $('.wheelInCar').css('animation-play-state', 'paused');
            }
            else {
                $('.wheelInCar').css('animation-play-state', 'running');
            }
            $pre.addClass('no-transition');
            $pre.removeClass('left');
            setTimeout(function () {
                $pre.removeClass('no-transition');
                running = false;
            }, 100);
        }, 2100);

    });
})
.car { /* My Container */
  height: 600px;
  padding: 40px 0;
  background-color: #efefef;
  overflow: hidden;
}

.car .click { /* Click Button */
  width: 100%;
  padding: 10px;
  text-align: center;
  border: 1px solid #0094ff;
  margin-bottom: 30px;
}

.car-image { /* The Cars*/
  position: absolute;
  top: 100px;
  margin-bottom: 30px;
  transform: translate(calc(50vw + 400px), 0);
  transition: all 2s ease-in-out;
  //width:50vw;
}

.car .car-image.current /* When Click the current car will be animated to translate (0, 0) */ {
  transform: translate(0, 0);
  display: block;
}

.car .car-image.left /*When The Car go out of the screen */ {
  transform: translate(calc(-50vw - 400px), 0);
  display: block;
}

.car .car-image.no-transition {
  transition: none; /* Remove the transition*/
}

.car .car-image .wheelInCar {
  width: 99px;
  height: 100px;
  position: absolute;
  background-repeat: no-repeat;
  background-position: center center;
  /* Rotating the Car Wheel, What I Need to stop the Wheel Animation When Car go into the screen but run Car Wheel when the car started to left the screen */ 
  -webkit-animation: wheelRotating 2s linear infinite;
  -moz-animation: wheelRotating 2s linear infinite;
  -ms-animation: wheelRotating 2s linear infinite;
  -o-animation: wheelRotating 2s linear infinite;
  animation: wheelRotating 2s linear infinite;
}

.car .car-image .wheelInCar.left {
  background-image: url('http://store6.up-00.com/2017-03/148992740531661.png');
  top: 94px;
  left: 98px;
}

.car .car-image .wheelInCar.right {
  background-image: url('http://store6.up-00.com/2017-03/148992740544512.png');
  top: 94px;
  right: 75px;
}

.car .wheelEleContainer {
  cursor: pointer;
}
/*Rotating Car Wheels*/
@keyframes wheelRotating {
  from {
    -moz-transform: rotate(360deg);
    -ms-transform: rotate(360deg);
    -o-transform: rotate(360deg);
    -webkit-transform: rotate(360deg);
    transform: rotate(360deg);
  }
  to {
    -moz-transform: rotate(0);
    -ms-transform: rotate(0);
    -o-transform: rotate(0);
    -webkit-transform: rotate(0);
    transform: rotate(0);
  }
}


请全屏运行代码段

2 个答案:

答案 0 :(得分:0)

试用此代码

public class Fragment2 extends Fragment {


    public Fragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_2, container, false);
    }

    public void fragment2refresh(){
        // TODO: this to be call when refresh on MainActivity and also fragment_2 is visible 
    }

}
$(document).ready(function () {
	run();
    $('.wheelEleContainer .slectWheel').click(function (e) {
        e.preventDefault();
        var getImgWheel = $(this).find('img').attr('src');
        $('.car .wheelInCar').css('background-image',
          'url(' + getImgWheel + ')'
        );
    });

    var running = false;
    $('.car-model').click(function (e) {
        e.preventDefault();
        var index = $(this).data("index");
        var $pre = $('.car-image.current');
        var $dom = $('.car-image[data-car-index="' + index + '"]');
        if ($dom.hasClass("current") || running) {
            return; 
        }
        running = true;
        $dom.addClass('current');
        $pre.addClass('left').removeClass('current');
        setTimeout(function () {
            $pre.addClass('no-transition');
            $pre.removeClass('left');
            setTimeout(function () {
                $pre.removeClass('no-transition');
                running = false;
            }, 100);
        }, 2100);
        run();

    });
    
    function run(){
		$(".car-image.current").removeClass("in");
		$(".car-image.current").removeClass("out");
		setTimeout(function(){
			$(".car-image.current").addClass("in");
		},1000);
		setTimeout(function(){
			$(".car-image.current").addClass("out");
		},5000);
		
	}
})
.car{
    height:600px;
    padding:40px 0;
    background-color:#efefef;
    overflow:hidden;
}
.car .click{
    width:100%;
    padding:10px;
    text-align:center;
    border:1px solid #0094ff;
    margin-bottom:30px;
}
.car-image {
    position:absolute;
    top: 100px;
    margin-bottom: 30px;
    -moz-transform: translate(160%, 0);
    -ms-transform: translate(160%, 0);
    -o-transform: translate(160%, 0);
    -webkit-transform: translate(160%, 0);
    transform: translate(160%, 0);
    -moz-transition: all 2s ease-in-out;
    -o-transition: all 2s ease-in-out;
    -webkit-transition: all 2s ease-in-out;
    transition: all 2s ease-in-out;
}
.car .car-image.current {
    display:block;
    transform: translateX(200%);
    transition:all 1s ease;
}
.car .car-image.left {
    -moz-transform: translate(-160%,0);
    -ms-transform: translate(-160%,0);
    -o-transform: translate(-160%,0);
    -webkit-transform: translate(-160%,0);
    transform: translate(-160%,0);
    display:block;
}
.car .car-image.no-transition {
    transition:none;
}
.car .car-image .wheelInCar{
    width:99px;
    height:100px;
    position:absolute;
    background-repeat:no-repeat;
    background-position:center center;
 }
.car .car-image .wheelInCar.left{
    background-image:url('http://store6.up-00.com/2017-03/148992740531661.png');
    top:94px;
    left:98px;
}
.car .car-image .wheelInCar.right{
    background-image:url('http://store6.up-00.com/2017-03/148992740544512.png');
    top:94px;
    right:75px;
}
.car .wheelEleContainer{
    cursor:pointer;
}
.car .car-image.current.in{
	transform:translateX(100px);
	transition:all 2.6s ease-in;
}
.car .car-image.current.out{
   transform:translateX(-200%);
   transition:all 2.6s ease-out;
}
.car .car-image.current.in .wheelInCar{
    animation: wheelRotatingIn 2s ease-in 1.3;
}
.car .car-image.current.out .wheelInCar{
    animation: wheelRotatingOut 2s ease-out 1.3;
}
@keyframes wheelRotatingIn {
    0% { -webkit-transform: rotate(0deg); }
    50% { -webkit-transform: rotate(800deg); }
    100% { -webkit-transform: rotate(1000deg); }
}
@keyframes wheelRotatingOut {
    0% { -webkit-transform: rotate(0deg); }
    50% { -webkit-transform: rotate(800deg); }
    100% { -webkit-transform: rotate(1600deg); }
}

答案 1 :(得分:-1)

CSS的一个班轮:

.car .car-image .wheelInCar {
    width: 99px;
    height: 100px;
    position: absolute;
    background-repeat: no-repeat;
    background-position: center center;
    -webkit-animation: wheelRotating 2s linear infinite;
    -moz-animation: wheelRotating 2s linear infinite;
    -ms-animation: wheelRotating 2s linear infinite;
    -o-animation: wheelRotating 2s linear infinite;
    animation: wheelRotating 2s linear infinite;
    animation-play-state: paused; /* Not exactly one line if you want to make compatible with prefixes */
}