[英]jQuery stops working after
I have a page which relies heavily on CSS3 animations. 我有一个页面非常依赖CSS3动画。 I am in the process of creating a script that will be the fallback that will work for those browsers that do not have CSS3 animations (looking at you IE...).
我正在创建一个脚本,该脚本将作为适用于那些没有CSS3动画的浏览器的后备脚本(看着IE ...)。 I created the following script that will do the basic of what I need:
我创建了以下脚本,该脚本可以满足我的基本需求:
var $j = jQuery.noConflict();
$j(document).ready(function() {
//Hide All Elements to Fade Them In
$j(".frame-1, .frame-2, .frame-3, .frame-4, #tile-wrap, #copyright").addClass("hide", function() {
//Change Color of "Frames"
$j(".frame-1, .frame-2, .frame-3, .frame-4").addClass("color", function() {
//Frame 1
$j(".frame-1").fadeIn("slow", function() {
$j('.frame-1').delay(3000).fadeOut('slow', function() {
//Frame 2
$j(".frame-2").fadeIn("slow", function() {
$j('.frame-2').delay(3000).fadeOut('slow', function() {
//Frame 3
$j(".frame-3").fadeIn("slow", function() {
$j('.frame-3').delay(3000).fadeOut('slow', function() {
//Frame 4
$j(".frame-4").fadeIn("slow", function() {
$j('.frame-4').delay(3000).fadeOut('slow', function() {
//Tile
$j('#tile-wrap').fadeIn('slow');
});
});
});
});
});
});
});
});
});
});
});
The first part of the script works without issue (adding the class of .hide
). 该脚本的第一部分可以正常工作(添加
.hide
类)。 But nothing after that fires or works. 但此后没有任何效果或作用。 I am stuck because no errors are seen and I assume I have an error in my script.
我被卡住是因为没有看到错误,并且我认为我的脚本中有错误。
Here is a fiddle of the script with the rest of the code. 这里是一个小提琴与其余代码的脚本。
Note: I am not very knowledgeable of writing JS and welcome any ways to improve the script, please provide examples. 注意:我不太了解JS编写,因此欢迎使用任何改进脚本的方法,请提供示例。
FIDDLE NOTE Firebug shows a couple errors when running the fiddle. 注释注: Firebug在运行小提琴时会显示几个错误。 These errors are only on the Fiddle page and I believe are related to the jsFiddle not my code or page.
这些错误仅在Fiddle页面上,我相信与jsFiddle有关,与我的代码或页面无关。
What I am attempting to Achieve 我正在尝试实现的目标
What I want is for each item (as listed by class or id) is to fade them in then fade them out after a delay then fade in the last div and it stays. 我想要的是每个项目(按类或ID列出)是淡入淡出,然后在延迟后淡出,然后淡入最后一个div,并保持不变。
This will work, http://jsfiddle.net/VNfT2/2/ . 这将起作用, http://jsfiddle.net/VNfT2/2/ 。 There is no callback for addclass.
addclass没有回调。 Having said that.
话说回来。 AHHH!!!!!!
AHHH !!!!!! This is NOT the right way to do it.
这不是正确的方法。 Hint: When you see more than 10 });
提示:当您看到10个以上时}); in a row, stop and take a deep breath.
连续停下来深呼吸。 :)
:)
Edit: There are hundreds of plugins to do this (google for jquery slideshow). 编辑:有数百个插件可以做到这一点(谷歌为jquery幻灯片)。 But, if you want to do it manually...look at this: fiddle: http://jsfiddle.net/VNfT2/5/
但是,如果您想手动进行操作……请看此:小提琴: http : //jsfiddle.net/VNfT2/5/
See http://jsfiddle.net/VNfT2/4/ 参见http://jsfiddle.net/VNfT2/4/
var $j = jQuery.noConflict();
$j(document).ready(function() {
//Hide All Elements
$j(".frame-1, .frame-2, .frame-3, .frame-4, #tile-wrap, #copyright")
.addClass("hide")
//Change Color of "Frames"
.addClass("color");
//Frame 1
$j(".frame-1").fadeIn("slow", function() {
$j(this).delay(3000).fadeOut('slow', function() {
//Frame 2
$j(".frame-2").fadeIn("slow", function() {
$j(this).delay(3000).fadeOut('slow', function() {
//Frame 3
$j(".frame-3").fadeIn("slow", function() {
$j(this).delay(3000).fadeOut('slow', function() {
//Frame 4
$j(".frame-4").fadeIn("slow", function() {
$j(this).delay(3000).fadeOut('slow', function() {
//Tile
$j(this).fadeIn('slow');
});
});
});
});
});
});
});
});
});
As I said im my comment, you can call addClass
with a string (the class) or with a function which return the class. 就像我在评论中说的那样,您可以使用字符串(类)或返回类的函数来调用
addClass
。 But you can't do it with a string and a function... See api.jquery.com/addClass 但是您不能使用字符串和函数来执行此操作...请参阅api.jquery.com/addClass
And in your callback functions you should use $(this)
, it's faster because this way you don't search the element again. 在回调函数中,您应该使用
$(this)
,它的速度更快,因为这样您就不必再次搜索该元素了。
The problem that your callbacks aren't called since they're supplied as the second argument. 您的回调没有被调用的问题,因为它们是作为第二个参数提供的。
Description: Adds the specified class(es) to each of the set of matched elements. 说明:将指定的类添加到每个匹配元素集中。
.addClass( className )
.addClass( function(index, currentClass) )
Here are some tips: 这里有一些提示:
Try to only have 1 nested/callback function inside another function. 尝试在另一个函数中仅包含1个嵌套/回调函数。
Refer to tip 4, then function fadeElementsInThenOut
for an example. 请参考技巧4,然后是函数
fadeElementsInThenOut
。
Don't repeat lookups. 不要重复查找。
Old code: 旧代码:
// Let's forget about the callbacks for now
$j(".frame-1, .frame-2, .frame-3, .frame-4, #tile-wrap, #copyright").addClass("hide");
$j(".frame-1, .frame-2, .frame-3, .frame-4").addClass("color");
New Code: 新代码:
$j(".frame-1, .frame-2, .frame-3, .frame-4").addClass("color hide");
$j("#tile-wrap, #copyright").addClass("color");
Use $(this)
to reference the same element within a callback. 使用
$(this)
引用回调中的相同元素。
Old Code: 旧代码:
$j(".frame-4").fadeIn("slow", function () {
$j('.frame-4').delay(3000).fadeOut('slow', function () {
//...
});
});
New Code: 新代码:
$j(".frame-4").fadeIn("slow", function () {
$j(this).delay(3000).fadeOut('slow', function () {
//...
});
});
Don't use a callback if you don't have to. 不必使用回调。
Old Code: 旧代码:
$j(".frame-4").fadeIn("slow", function () {
$j(this).delay(3000).fadeOut('slow', function () {
//...
});
});
New Code: 新代码:
$j(".frame-4").fadeIn("slow").delay(3000).fadeOut('slow', function () {
//...
});
Here's your code rewritten to fix the problems. 这是您的代码经过重写以解决问题。
var $j = jQuery.noConflict();
$j(function () {
var frames = [ ".frame-4", ".frame-3", ".frame-2", ".frame-1" ];
var fadeElementsInThenOut = function( els, lastCallback ){
var el = els.pop();
if( el ){
$j(el).fadeIn("slow").delay(3000).fadeOut('slow', function(){
fadeElementsInThenOut( els, lastCallback );
});
}else{
lastCallback();
}
};
$j( frames.join( ", " ) ).addClass("color hide");
$j("#tile-wrap, #copyright").addClass("color");
fadeElementsInThenOut( frames, function(){
$j('#tile-wrap').fadeIn('slow');
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.