如何扭转这种简单的jQuery淡出效果?
How can I reverse this simple jQuery fadeIn effect?
因此,当您单击按钮(id="enter")时,此脚本创建了一个舒缓的快速效果,我想要的是,当您再次单击它时,它将逆转效果并返回到开始。另外,另一个解决方案是使用两个按钮,这样一个按钮可以执行第一部分,另一个按钮可以反向操作。下面是代码:
jQuery('#enter').click(function() {
$('#slide2').fadeIn(1000, 'linear', function(){
$('#slide3').fadeIn(1000, 'linear');
});
});
有三个图像:slide1, slide2和slide3。我实际上在这里找到了这段代码:http://jsfiddle.net/e6hUr/1/,这就是它应该看起来的样子,但我现在只需要反向
这里你有多个幻灯片,所以fadeToggle不能给你确切的反转下面的代码可以给你完全相反的fadeIn效果
var forward = true;
$('#enter').click(function() {
if(forward == true){
$('#slide2').fadeIn(1000, 'linear', function(){
$('#slide3').fadeIn(1000, 'linear');
});
forward = false;
}else{
$('#slide3').fadeOut(1000, 'linear', function(){
$('#slide2').fadeOut(1000, 'linear');
});
forward = true;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="slides">
<li id="slide1" style="width: 100%; float: left; margin-right: -100%; display: list-item; ">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/drawing.jpg">
</li>
<li id="slide2" style="width: 100%; float: left; margin-right: -100%; display: none; ">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/cgi-day-time.jpg">
</li>
<li id="slide3" style="width: 100%; float: left; margin-right: -100%; display: none; ">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/cgi-night-time.jpg">
</li>
</ul>
<button id="enter" type="button">Click Me!</button>
有两种方法可以使用fadIn,fadeOut和fadeToogle来实现在jquery中改变幻灯片顺序的反向效果,见下面的代码。为了识别渐变效果,我们在按钮中管理data-fade属性。
$('#enter').click(function() {
var fade_type = $(this).data('fade');
if (fade_type == "in") {
$('#slide2').fadeToggle(1000, 'linear', function() {
$('#slide3').fadeToggle(1000, 'linear');
});
$(this).data('fade', 'out');
} else {
$('#slide3').fadeToggle(1000, 'linear', function() {
$('#slide2').fadeToggle(1000, 'linear');
});
$(this).data('fade', 'in');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="slides">
<li id="slide1" style="width: 100%; float: left; margin-right: -100%; display: list-item; ">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/drawing.jpg">
</li>
<li id="slide2" style="width: 100%; float: left; margin-right: -100%; display: none; ">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/cgi-day-time.jpg">
</li>
<li id="slide3" style="width: 100%; float: left; margin-right: -100%; display: none; ">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/cgi-night-time.jpg">
</li>
</ul>
<button id="enter" type="button" data-fade="in">Click Me!</button>
你应该使用CSS过渡来处理它,在jQuery中,你只需要切换一个类:
-jsFiddle -
$('#enter').click(function() {
$('#slide2, #slide3').toggleClass('fadeIn');
});
#enter {
position: relative;
z-index: 9;
}
#slide2, #slide3 {
opacity: 0;
transition: opacity 1s linear;
}
#slide2.fadeIn, #slide3.fadeIn {
opacity: 1;
}
#slide2:not(.fadeIn), #slide3.fadeIn
{
transition-delay: 1s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="slides">
<li id="slide1" style="width: 100%; float: left; margin-right: -100%; display: list-item; ">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/drawing.jpg">
</li>
<li id="slide2" style="width: 100%; float: left; margin-right: -100%;">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/cgi-day-time.jpg">
</li>
<li id="slide3" style="width: 100%; float: left; margin-right: -100%;">
<img alt="Panoramic view from the window of one of the penthouses" src="http://tritonpenthouses.uwpistol.net/images/bg/cgi-night-time.jpg">
</li>
</ul>
<button id="enter" type="button">Click Me!</button>
使用
$('#enter').click(function() {
$('#slide2').fadeToggle(1000, 'linear', function(){
$('#slide3').fadeToggle(1000, 'linear');
});
});
相关文章:
- css(或jQuery)悬停时淡入淡出
- 带有计数器、缩略图、进度条和淡入淡出效果的简单jQuery幻灯片
- jQuery图像幻灯片,在停止后淡出到第一张图像
- jQuery淡出后停止自动滚动
- 使用jquery淡入淡出
- jQuery滑块淡入淡出
- jQuery idTabs-使用鼠标悬停自动更改和淡入淡出
- 文本淡入淡出jquery
- jQuery UI模式对话框覆盖淡出
- jQuery在用新文本替换HTML文本时淡出HTML文本,而不是仅显示
- 如何让li项目在这个jQuery列表中滑动和/或淡出视图
- JQuery刷新列表效果-淡入淡出问题
- 使用 JQuery 淡出动画
- 如何使用jquery淡出多个图像
- jQuery淡入淡出动画在两者之间显示白色
- jquery自动淡入淡出一系列图像
- 我可以在淡入/淡出 JQuery 函数中使用 标记按钮类吗?
- 动画淡入淡出|jQuery与纯js|setInterval与setTimeout
- Img没有淡出jQuery
- 淡入和淡出 jQuery