[英]How to reverse animation on every second click?
I have 6 "blocks" and each contains different texts, for the sake of simplicity let's just consider these as my "blocks" 我有6个“块”,每个包含不同的文本,为了简单起见,我们只考虑这些作为我的“块”
<div id="block1"> <h2> Block1 </h2> </div
I have 3 of them visible and 3 hidden. 我有3个可见,3个隐藏。 I have a button which replace the corresponding blocks
我有一个按钮,可以替换相应的块
$(".showmore").click(function(){
$("#block1").fadeOut("slow", function(){
$(this).replaceWith($("#block4").html());
$(this).fadeIn("slow");
});
$("#block2").delay(400).fadeOut("slow", function(){
$(this).replaceWith($("#block5").html());
$(this).fadeIn("slow");
});
$("#block3").delay(800).fadeOut("slow", function(){
$(this).replaceWith($("#block6").html());
$(this).fadeIn("slow");
});
$(this).text('Show less');
});
It works fine but have no idea how to revert it. 它工作正常,但不知道如何还原它。 I tried to clone the elements to a variable and then load them but it seems like the id is gone because when I try to hide the block1 or the block4 none of them disappear.
我试图将元素克隆到变量然后加载它们但似乎id已经消失,因为当我试图隐藏block1或block4时,它们都没有消失。 Can anyone help?
有人可以帮忙吗?
As i understood, you have 3 div's that you would like to revert them back to the content they had after a fadeout/fadein event on the click of another div. 据我了解,你有3个div,你希望将它们恢复到他们在点击另一个div后的淡出/淡化事件后所拥有的内容。 The issue on your try is the usage of the replaceWith method.
您尝试的问题是使用replaceWith方法。 Is this what you are looking for to achieve?
这是你想要实现的目标吗? See fiddle here .
请看这里的小提琴。
$(".showmore").click(function(){
$("#block1").fadeOut("slow", function(){
//save the content of the hidden block to a variable
var html = $("#block4").html();
//put the content of the current div to the hidden div, to be used on the next click
$("#block4").html($(this).html());
//show the content of the hidden div
$(this).html(html);
$(this).fadeIn("slow");
});
$("#block2").delay(400).fadeOut("slow", function(){
var html = $("#block5").html();
$("#block5").html($(this).html());
$(this).html(html);
$(this).fadeIn("slow");
});
$("#block3").delay(800).fadeOut("slow", function(){
var html = $("#block6").html();
$("#block6").html($(this).html());
$(this).html(html);
$(this).fadeIn("slow");
});
$(this).text('Show less');
});
As you see, i copy the content of a div to the corresponding hidden one. 如您所见,我将div的内容复制到相应的隐藏的内容。 Like this you can basically switch the data on each click.
像这样你基本上可以在每次点击时切换数据。
One idea would be to fadeout
and fadein
instead of replaceWith
. 一个想法是
fadeout
和fadein
而不是replaceWith
。 Then you can check which block is visible and which is invisible. 然后,您可以检查哪个块可见,哪个块不可见。
var visible, invisible;
if ($("#block1").is(":visible")) {
visible = "#block1";
invisible = "#block4";
} else {
visible = "#block4";
invisible = "#block1";
}
$(visible).fadeOut("slow", function(){
$(invisible).fadeIn("slow");
});
Not sure if that gives you the same functionality though. 不确定这是否给你相同的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.