2012-08-08 74 views
2

我知道这里有一些类似的问题,但我试图获得特定的值。jQuery - 为某个类丢失某个类的元素定义变量和运行函数

我正在研究Flexslider http://www.woothemes.com/FlexSlider/,并试图在幻灯片中添加一些失去“flex-active-slide”类的特殊功能。基于文档,我设法在幻灯片动画之前和之后插入我的函数。

// Callback API 
start: function(){}, 
before: function(){ 
    $('li.image').someFunction(); //this is where I need to trigger this event only to the li.image that's losing the flex-active-slide class 
},       
after: function() { 
    $('li.flex-active-slide').otherFunction(); //this is where the active class 
}, 

我并不需要运行功能适用于所有其他li.image元素你看到的例子以上,只为失去柔性有源滑类之一。

我需要得到这个元素并用变量定义它,这样我才能正确运行li.image的函数,这对于性能优化目的也是有意义的。

我很感激任何帮助。

回答

2

更简单的方法是编辑你的插件文件本身

开放jquery.flexslider.js

找到下面的代码

active: function() { 
      slider.controlNav.removeClass(namespace + "active").eq(slider.animatingTo).addClass(namespace + "active"); 
     } 

您可以在此功能为您的工作中使用slider.controlNavslider.controlNav指的是你找到使用它的子图像li元素jQuery的.find

如果它不工作,你可以找到这个文件active-slide,看到无论这个类被删除,你可以在那些地方添加代码

UPDATE:$('li.image')。eq(slider.currentSlide)是调用失去活动类的幻灯片的方式。

+0

我知道这与此有关,但我不明白如何在失去活动类后立即调用该特定li。 – thednp 2012-08-08 04:53:59

+0

在这个活动函数中,'slider.controlNav'指的是其他地方的'li'' slider.currentSlide'指的是'li'。你可以写'var myLi = slider.currentSlide; // remove class code' then'myLi.your code'粗略地说我可以说''myLi''将会成为移除课后'$('li.flex-active-slide')'的快捷方式 – Imdad 2012-08-08 04:57:17

+0

$('li.image') .eq(slider.currentSlide)这是如何获得该幻灯片,非常感谢。你的回答是最好的。我编辑了你的答案,以方便代码搜索者:) – thednp 2012-08-08 05:40:30

0

我想你可以尝试这样的事情......

var somevar; 

start: function(){}, 
before: function() { 
    somevar = $('li.image.flex-active-slide'); 
}, 
after: function() { 
    $.each(somevar, function(index, elem) { 
     if(!$(elem).hasClass('flex-active-slide'))) { 
      // Obviously it has lost the flex-active-slide class. 
     } 
    }); 
} 

我怀疑这是很容易能够确定该插件实际上已经做了一些之前的元素是否失去了说类,而无需修改插件本身。因此,即使您在那里的.someFunction()方法必须在after回调中执行。

相关问题