javascript - 如何使div在部分中循环

标签 javascript jquery html css

我正在做轮盘网站,但我不知道如何让 div 循环。 当 div 离开部分边界时,我需要它们循环。

$("#right").click(function() {
  $("#one").css("left", "300px");
  $("#two").css("left", "300px");
  $("#three").css("left", "300px");
  $("#four").css("left", "300px");
  $("#five").css("left", "300px");
  $("#six").css("left", "300px");

});

$("#left").click(function() {
  $("#one").css("left", "0px");
});
section {
  width: 80%;
  height: 50px;
  border: 5px solid gray;
  margin: auto;
  padding: 7px;
  z-index: 1;
  transition: left 2s ease;
  overflow: hidden;
}

div#one {
  width: 15%;
  height: 50px;
  background: black;
  float: left;
  z-index: -1;
  left: 0px;
  position: relative;
  transition: left 2s ease;
  loop: true;
}

div#six {
  width: 15%;
  height: 50px;
  margin-left: 5px;
  background: red;
  float: left;
  z-index: -1;
  left: 0px;
  position: relative;
  transition: left 2s ease;
  loop: true;
}

div#five {
  width: 15%;
  height: 50px;
  background: black;
  float: left;
  margin-left: 5px;
  z-index: -1;
  left: 0px;
  position: relative;
  transition: left 2s ease;
}

div#two {
  width: 15%;
  height: 50px;
  margin-left: 5px;
  background: red;
  float: left;
  z-index: -1;
  left: 0px;
  position: relative;
  transition: left 2s ease;
}

div#three {
  width: 15%;
  height: 50px;
  margin-left: 5px;
  background: black;
  float: left;
  z-index: -1;
  left: 0px;
  position: relative;
  transition: left 2s ease;
}

div#four {
  width: 15%;
  height: 50px;
  margin-left: 5px;
  background: red;
  float: left;
  z-index: -1;
  left: 0px;
  position: relative;
  transition: left 2s ease;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
  <div id="one"></div>
  <div id="two"></div>
  <div id="three"></div>
  <div id="four"></div>
  <div id="five"></div>
  <div id="six"></div>
</section>

<button id="right">
left: 100px;
</button>

所以当我点击按钮时,它应该移动,然后一旦它们离开部分边界,div 应该绕到左边并再次显示在左边。

提前致谢!

最佳答案

您不必为所有元素制作动画,您可以只为第一个元素制作动画,然后您需要使用 overflow 以便能够将最后一个元素放在开头。

所以你可以尝试这样的事情(当然可以改进):

$("#right").click(function() {
  $('section').prepend($('section div').last().css('margin-left','-30%'));
  $('section >div').eq(1).css('margin-left', '0%');

});

$("#left").click(function() {
  $("#one").css("left", "0px");
});
section {
  width: 80%;
  height: 50px;
  border: 5px solid gray;
  margin: auto;
  padding: 7px;
  z-index: 1;
  transition: left 2s ease;
  overflow: hidden;
  display: flex;
}

section>div {
  flex: 0 0 25%;
  margin: 0 5px;
  position: relative;
  color: #fff;
  text-align: center;
  transition: 1s ease;
}

#one,
#three,
#five {
  background: red;
}

#two,
#four,
#six {
  background: black;
}

section>div:first-child {
  margin-left: -30%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section>
  <div id="one">1</div>
  <div id="two">2</div>
  <div id="three">3</div>
  <div id="four">4</div>
  <div id="five">5</div>
  <div id="six">6</div>
</section>

<button id="right">
left: 100px;
</button>

关于javascript - 如何使div在部分中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648847/

相关文章:

php - Ajax 不从 codeigniter 中的模态形式发送序列化数据

javascript - 动态内容的 JQuery Find() .value()

css - 我网站右侧出现意外的边距/填充

html - 为什么乘号会被转换成奇怪的字符?

javascript - 有一个按钮来控制时间线 GSAP 动态 - 定位增量计数器

javascript - 访问 jQuery .each() 循环之外的变量

javascript - 隔离范围 '=' 未分配

javascript - 文本鼠标悬停时的图像边框

javascript - 从 xml 中提取值时,文本框不占用双空格

php - summernote + PHP 邮件程序