如何随机翻转前面的Div

时间:2016-06-13 17:30:07

标签: javascript jquery random flip

参考: How to Flip Div's in Randomly和工作小提琴:http://jsfiddle.net/7x75466y/5/

我在我的网站上有上面的随机图像翻转脚本。目前,它会翻转一个随机图像并让它保持在该状态,然后移动到另一个randome图像并将其翻转并使其保持该状态等。

我需要它"解开"在每次翻转之后移动到下一个随机图像。以下是我希望它如何工作的示例:https://learnwithhomer.com/buzz/press/

这是有问题的jQuery:

$(document).ready(function () {
    var $flippers = $(".flip-container"),
    qtFlippers = $flippers.length;

    setInterval(function () {
    $flippers.eq(Math.floor(Math.random()*qtFlippers)).toggleClass('hover'); }, 2000);
});

1 个答案:

答案 0 :(得分:1)



var $flippers = $(".flip-container"),
    qtFlippers = $flippers.length;
var isFlipped = false;
var randomItem = -1
setInterval(function () {
    
    if (isFlipped && randomItem !== -1) {
    	$flippers.eq(Math.floor(randomItem)).toggleClass('hover');
    	isFlipped = false;
      randomItem = -1;
      return;
    }
    
    if (!isFlipped) {
      randomItem = Math.random()*qtFlippers;
    	$flippers.eq(Math.floor(randomItem)).toggleClass('hover');
    	isFlipped = true;
    }
}, 1000);

section {
    -webkit-perspective: 500px;
    perspective: 500px;
    -webkit-perspective-origin: 50% 50%;
    perspective-origin: 50% 50%;
}

.flip-container {
    position: relative;
    display: inline-block;
    margin-left: 50px;
}

.flip-container > div > div {
    background: red;
    backface-visibility: hidden;
    position: absolute;
    top: 0;
    left: 0;
}

.flip-container .flipper {
    transform-origin: 50px 100%;
    transition: 0.6s;
    transform-style: preserve-3d;
    position: relative;
}
.vertical.flip-container .flipper {
    transform-origin: 100% 50px;
}

.flip-container > div > div + div {
    background: green;
    transform: rotateX(180deg) scaleX(-1) scaleY(-1);
}
.vertical.flip-container > div > div + div {
    transform: rotateX(180deg);
}


/* flip the pane when hovered 
.flip-container:hover .flipper,*/
.flip-container.hover .flipper {
    transform: rotateY(180deg);
}

/*.vertical.flip-container:hover .flipper,*/
.vertical.flip-container.hover .flipper{
    transform: rotateX(180deg);
}

.flip-container,
.flip-container > div > div {
    width: 100px;
    height: 100px;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
    <div class="flip-container" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
        
    <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
        
    <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
  <div class="flip-container vertical" ontouchstart="this.classList.toggle('hover');">
		<div class="flipper">
			<div>Rushikesh</div>
			<div>Prudvi</div>
		</div>
	</div>
</section>
&#13;
&#13;
&#13;

这是你的样子吗?