如何在函数之前使用animate函数

时间:2017-03-01 05:14:49

标签: javascript jquery html css animation

我想同时使用animate和before函数 我有列表项,我想在交换项目时进行动画处理。

交换工作正常,但我想添加动画'慢' 我想用转换交换列表。

交换时是否可以使用动画“慢”功能?
我想用动画向左移动 如果我点击下一个按钮图片交换没有动画。

jQuery(document).ready(function($){
 $('.flex-next').click(function(){
  $('.thumbnailIcon').each(function(index, item){
    var fourthLi = $(item).find("li:nth-child(4)");
    var secondLi = $(item).find("li:nth-child(2)");
    $(secondLi).before(fourthLi); //animate while swapping
    //$(secondLi).animate(before(fourthLi),slow);  I have tried as
  });

 });
$('.flex-prev').click(function(){
  $('.thumbnailIcon').each(function(index, item){
    var fourthLi = $(item).find("li:nth-child(2)");
    var secondLi = $(item).find("li:nth-child(4)");
    $(secondLi).after(fourthLi);
  });

 });

});
.content__teaser {
  text-align: center;
  margin-bottom: 15px;
  height: 110px;
}
.contentContainer ul {
  list-style-type: none;
}
ul .thumbnailIcon {
  width: 100%;
  margin-left: 31%;
  height: 100%;
}
.thumbnailIcon li {
  float: left;
  margin-left: 16px;
  height: 110px;
  align-items: center;
  display: flex;
}
.thumbnailIcon .img-circle {
  border-radius: 50%;
  border: 4px solid #ffffff !important;
}
.thumbnailIcon .img-circle {
  margin: auto;
  border-radius: 50%;
  border: 4px solid #ffffff !important;
}
.content__teaser img {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="parent flex-active-slider">
<div class="node_id"><span>233</span></div>
<ul class="thumbnailIcon">
  <li>left 0</li>  
  <li>1<img class="videoThumbnail img-circle" src="https://www.w3schools.com/css/paris.jpg" draggable="false" style="width: 100px;"></li>
  <li >2<img class="videoThumbnail img-circle" src="https://www.w3schools.com/css/paris.jpg" draggable="false" style="width: 100px;"></li>
  <li >3 <img class="videoThumbnail img-circle" src="https://www.w3schools.com/css/paris.jpg" draggable="false" style="width: 100px;"></li>
  <li>right 4</li>  
</ul> 
</li>

<a  class="flex-prev">prev<a>
<a  class="flex-next">next<a>

1 个答案:

答案 0 :(得分:0)

希望这就是你要找的东西,如果我理解你正在寻找移动元素的效果。

JsFiddler链接 https://jsfiddle.net/rj1405/tcpk6xde/2/

我只是扩展了你的代码并使用了 .fadeIn .fadeOut 动画方法。

jQuery(document).ready(function($){
  $('.flex-next').click(function(){
     $('.thumbnailIcon').each(function(index, item){
     var fourthLi = $(item).find("li:nth-child(4)");
     var secondLi = $(item).find("li:nth-child(2)");
     $(fourthLi).fadeToggle( "slow", function() {
       $(secondLi).before(fourthLi).fadeIn("slow");
     });
     $(fourthLi).fadeIn("slow");
   });
});

您可以在此处查看输出

&#13;
&#13;
jQuery(document).ready(function($){
 $('.flex-next').click(function(){
  $('.thumbnailIcon').each(function(index, item){
    var fourthLi = $(item).find("li:nth-child(4)");
    var secondLi = $(item).find("li:nth-child(2)");
   	$(fourthLi).fadeToggle( "slow", function() {
   		$(secondLi).before(fourthLi).fadeIn("slow");
  	});
    $(fourthLi).fadeIn("slow");
	});
});
$('.flex-prev').click(function(){
  $('.thumbnailIcon').each(function(index, item){
    var fourthLi = $(item).find("li:nth-child(2)");
    var secondLi = $(item).find("li:nth-child(4)");
    $(secondLi).after(fourthLi);
  });

 });

});
&#13;
.content__teaser {
  text-align: center;
  margin-bottom: 15px;
  height: 110px;
}
.contentContainer ul {
  list-style-type: none;
}
ul .thumbnailIcon {
  width: 100%;
  margin-left: 31%;
  height: 100%;
}
.thumbnailIcon li {
  float: left;
  margin-left: 16px;
  height: 110px;
  align-items: center;
  display: flex;
}
.thumbnailIcon .img-circle {
  border-radius: 50%;
  border: 4px solid #ffffff !important;
}
.thumbnailIcon .img-circle {
  margin: auto;
  border-radius: 50%;
  border: 4px solid #ffffff !important;
}
.content__teaser img {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="parent flex-active-slider">
<div class="node_id"><span>233</span></div>
<ul class="thumbnailIcon">
  <li>left 0</li>  
  <li>1<img class="videoThumbnail img-circle" src="https://www.w3schools.com/css/paris.jpg" draggable="false" style="width: 100px;"></li>
  <li >2<img class="videoThumbnail img-circle" src="https://www.w3schools.com/css/paris.jpg" draggable="false" style="width: 100px;"></li>
  <li >3 <img class="videoThumbnail img-circle" src="https://www.w3schools.com/css/paris.jpg" draggable="false" style="width: 100px;"></li>
  <li>right 4</li>  
</ul> 
</li>

<a  class="flex-prev">prev<a>
<a  class="flex-next">next<a>
&#13;
&#13;
&#13;