jQuery setInterval 与 chrome 中的 fadeIn、fadeOut 错误?
我要疯狂地想弄清楚这个问题。在我网站的主页上,我有一个滑块,当前有四个项目(html 是动态创建的,因此该解决方案必须适用于任意数量的项目)。这些项目绝对位于彼此之上。该脚本可以执行两件事:
1)当单击滑块导航项(这是一个锚链接)时,当前导航项淡出,新导航项淡入(交叉淡入淡出)。
2) 如果用户没有单击任何导航项,滑块将自动前进到下一个项。
当我尝试使用 setInterval 时,我的问题仅出现在 Chrome 中。当我这样做时,交叉淡入淡出不起作用,传出的容器淡出为白色,然后传入的容器突然出现(没有褪色)。
这是我的代码。希望有一些想法,因为我已经尝试了一切!
jQuery(document).ready(function() {
jQuery('.slider-item:first').show();
jQuery('.slider-nav-item').click(function () {
if (!jQuery(this).hasClass("activeSlide")) {
clearInterval(s);
s = setInterval ( "nextSlide()", 6000 );
currentSlider = jQuery(this).attr("rel");
jQuery('#slider .slider-item').fadeOut(1200);
jQuery('#slider .slider-item:nth-child('+ currentSlider+')').fadeIn(1200);
jQuery('.slider-nav-item').removeClass("activeSlide");
jQuery(this).addClass("activeSlide");
}
return false;
});
var s = setInterval ( "nextSlide()", 6000 );
});
function nextSlide (){
var max = jQuery('#slider-nav .slider-nav-item').length;
var currentSlider = jQuery('.activeSlide').attr("rel");
if (currentSlider == max) {
jQuery('#slider .slider-item:nth-child('+currentSlider+')').fadeOut(1200);
jQuery('#slider .slider-item:first').fadeIn(1200);
jQuery('.slider-nav-item').removeClass("activeSlide");
jQuery('.slider-nav-item:first').addClass("activeSlide");
} else {
var temp = parseInt(currentSlider) + 1;
jQuery('#slider .slider-item:nth-child('+currentSlider+')').fadeOut(1200);
jQuery('#slider .slider-item:nth-child('+temp+')').fadeIn(1200);
jQuery('.slider-nav-item').removeClass("activeSlide");
jQuery('#slider-nav .slider-nav-item:nth-child('+temp+')').addClass("activeSlide");
}
}
I'm going crazy trying to figure this out. On the homepage of my site I have a slider that currently has four items (the html is created dynamically, so this solution has to work for any number of items). These items are positioned absolutely on top of each other. The script can do two things:
1) When clicking on a slider-nav-item (which is a anchor link), the current nav item fades out, and the new one fades in (cross-fades).
2) If the user does not click on any nav items, the slider will automatically progress to the next item.
My problem is only occurring in Chrome when I try to use setInterval. When I do this, the cross fading does not work, and the outgoing container fades out to white, and then the container coming in just abruptly appears (no fading).
Here is my code. Hoping for some ideas, because I've tried everything!
jQuery(document).ready(function() {
jQuery('.slider-item:first').show();
jQuery('.slider-nav-item').click(function () {
if (!jQuery(this).hasClass("activeSlide")) {
clearInterval(s);
s = setInterval ( "nextSlide()", 6000 );
currentSlider = jQuery(this).attr("rel");
jQuery('#slider .slider-item').fadeOut(1200);
jQuery('#slider .slider-item:nth-child('+ currentSlider+')').fadeIn(1200);
jQuery('.slider-nav-item').removeClass("activeSlide");
jQuery(this).addClass("activeSlide");
}
return false;
});
var s = setInterval ( "nextSlide()", 6000 );
});
function nextSlide (){
var max = jQuery('#slider-nav .slider-nav-item').length;
var currentSlider = jQuery('.activeSlide').attr("rel");
if (currentSlider == max) {
jQuery('#slider .slider-item:nth-child('+currentSlider+')').fadeOut(1200);
jQuery('#slider .slider-item:first').fadeIn(1200);
jQuery('.slider-nav-item').removeClass("activeSlide");
jQuery('.slider-nav-item:first').addClass("activeSlide");
} else {
var temp = parseInt(currentSlider) + 1;
jQuery('#slider .slider-item:nth-child('+currentSlider+')').fadeOut(1200);
jQuery('#slider .slider-item:nth-child('+temp+')').fadeIn(1200);
jQuery('.slider-nav-item').removeClass("activeSlide");
jQuery('#slider-nav .slider-nav-item:nth-child('+temp+')').addClass("activeSlide");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不会解决问题(也许?)
应该是(最佳实践)
另外为什么不只使用插件呢? http://css-tricks.com/3412-anythingslider-jquery-plugin/
This won't fix the problem (maybe?)
should be (best practice)
Also why not just use a plugin? http://css-tricks.com/3412-anythingslider-jquery-plugin/
我遇到了 jQuery 交叉淡入淡出问题,当 HTML 无效时,元素会突然完全按照您所描述的方式显示。通过验证器运行页面并更正所有语法问题,然后查看问题是否仍然存在。
我遇到过好几个这样的问题。 jQuery 似乎对有效的 HTML 有着强烈的偏好。
I have had issues with jQuery cross-fades where elements would abruptly appear exactly as you describe when the HTML wasn't valid. Run the page through a validator and correct any syntax issues, and see if the issue remains.
I've had several issues like this. jQuery seems to have a strong preference for valid HTML.