JQuery animate()与removeClass()结合时不能正常工作
JQuery animate() not working as intended in conjunction with removeClass()
我一直在尝试实现我自己的carousel作为一种自学javascript的方式,并遇到了一个问题,在我的removeClass调用之前的动画没有发生,但引入下一个元素的动画按预期工作。
我意识到这听起来很模糊,所以我用jsfiddle重现了我的问题:http://jsfiddle.net/f57N3/
我还包括了下面的相关代码。任何帮助将非常感激!
<div class="button-container">
<button id="b2">Next</button>
</div>
<div class="container">
<div class="box active-box" id="box1">
thingy
</div>
<div class="box" id="box2">
other thingy
</div>
<div class="box" id="box3">
another thingy
</div>
</div>
CSS: .box{
display: none;
position: relative;
opacity: 0;
width: 350px;
height: 200px;
background-color: blue;
color: white;
border: 5px solid black;
}
.active-box{
display: block;
opacity: 1;
}
.container{
width: 850px;
height: 500px;
margin: 0px auto;
background-color: #003399;
}
Javascript: // Functionality for 'Next' button
$("#b2").click(function(){
var thisBox = $(".active-box");
var nextBox = thisBox.next();
if(nextBox.length == 0){
nextBox = $(".box").first();
}
// Set pre-animation conditions
thisBox.css("margin-left", "0px");
thisBox.css("opacity", "1");
// Animate this box and remove active class
thisBox.animate({marginLeft: "-=300px", opacity: "0"}, 800).removeClass("active-box");
//Set pre-animation conditions
nextBox.css("margin-left", "300px");
nextBox.css("opacity", "0");
// Animate this box and add active class
nextBox.animate({marginLeft: "-=300px", opacity: "1"}, 800).addClass("active-box");
});
任何帮助都将非常感激!
Animate有一个可以传递给它的complete
函数http://api.jquery.com/animate/
发生的是removeClass
直接射击。只有complete
中的removing
似乎能解决这个问题。同时,将下面的内容移到其中,使其运行更流畅。
// Functionality for 'Next' button
$("#b2").click(function () {
var thisBox = $(".active-box");
var nextBox = thisBox.next();
if (nextBox.length == 0) {
nextBox = $(".box").first();
}
// Set pre-animation conditions
thisBox.css("margin-left", "0px");
thisBox.css("opacity", "1");
// Animate this box and remove active class
thisBox.animate({
marginLeft: "-=300px",
opacity: "0"
}, 800, 'swing', function () {
thisBox.removeClass("active-box");
//Set pre-animation conditions
nextBox.css("margin-left", "300px");
nextBox.css("opacity", "0");
// Animate this box and add active class
nextBox.animate({
marginLeft: "-=300px",
opacity: "1"
}, 800).addClass("active-box");
})
});
演示:http://jsfiddle.net/robschmuecker/f57N3/1/
你可以这样做:
Javascript:$("#b2").click(function(){
var thisBox = $(".active-box");
var nextBox = thisBox.next();
if(nextBox.length == 0){
nextBox = $(".box").first();
}
// Set pre-animation conditions
thisBox.css("margin-left", "0px");
thisBox.css("opacity", "1");
// Animate this box and remove active class
thisBox.animate({marginLeft: "-=300px", opacity: "0"}, 800);
thisBox.removeClass("active-box");
//Set pre-animation conditions
nextBox.css("margin-left", "300px");
nextBox.css("opacity", "0");
// Animate this box and add active class
nextBox.animate({marginLeft: "-=300px", opacity: "1"}, 800);
nextBox.addClass("active-box");
});
相关文章:
- AngularJS UI路由器不能像ng路由器那样工作
- HTML5音频加载和播放获胜'我不能在iPad上工作
- JS可以在Chrome中工作,但不能在Firefox中工作
- javascript扫雷器floodfill算法不能正常工作
- JavaScript指令不能像我想象的那样工作
- Facebook登录按钮没有'不能在Firefox上工作
- jQuery Datepicker可以在Safari中工作,但不能在FF或Chrome中工作
- javascript没有´我不能在joomla 3.0中工作
- 用javascript将script元素附加到头部;铬不能工作
- jQuery函数不能只在一个页面上工作
- 为什么这个代码不能正常工作
- 为什么jquery悬停在jsfiddle中可以工作,而在我的html布局中却不能
- 为什么我的JavaScript在Safari上的严格模式下不能正常工作
- 为什么Turbolinks不能正常工作?Rails应用程序
- jQuery dosn'内联函数不能按预期工作
- 为什么这个javascript工作不能正确地分配.cells[] ?
- 谷歌应用引擎云端点:OAuth2不工作.不能加载api
- 输入焦点不工作/不能选择单选按钮-引导
- LocalStorage不能在iPhone上工作-不能在私人浏览
- 工作不能代替生活