2013-01-14 65 views
0

点击动作,我有一些功能是这个样子:重新启动功能使用jQuery

var live_search_list = $('.live_search_list ul'), 
    active_search_element = live_search_list.find('li.active'), 
    search_element = live_search_list.find('li'), 
    jsPane = $('.jspPane'); 

$('.bottom_search').click(function(){ 
    if (!search_element.last().hasClass('active')) { 
     active_search_element.removeClass('active'); 
     active_search_element.next('li').addClass('active'); 
     jsPane.animate({top:"-=95px"}); 
    } 
}); 
$('.top_search').click(function(){ 
    if (!search_element.first().hasClass('active')) { 
     active_search_element.removeClass('active'); 
     active_search_element.prev('li').addClass('active'); 
     jsPane.animate({top:"+=95px"}); 
    } 
}); 

所以,问题开始第一次点击之后,我只有一个动作 - 这个动画。第一次点击功能后,不再检查我的情况,并且不会更改,删除类active。每次点击这个按钮后我怎样才能重新启动这个功能?

+0

你想['停止()'](http://api.jquery.com/停止/)动画? – epascarello

+0

我想在每次点击后检查我的情况,现在在第一次点击一切正常后,但在它之后我只有动画动作... – Lukas

+0

您确定'search_element'和'active_search_element'是第二次和以后的有效元素点击? +1对于epascarello所说的 - 除了任何东西 - 在再次开始动画之前停止()总是个好主意,否则,如果用户在短时间内点击几次,你的动画就会排队等候,一切都会变得很诡异。 – WTK

回答

1

后进行下一次li活动类,你需要重新缓存的active_search_element

var live_search_list = $('.live_search_list ul'), 
    active_search_element = live_search_list.find('li.active'), 
    search_element = live_search_list.find('li'), 
    jsPane = $('.jspPane'); 

$('.bottom_search').click(function(){ 
    if (!search_element.last().hasClass('active')) { 
     active_search_element.removeClass('active'); 
     active_search_element.next('li').addClass('active'); 
     active_search_element = live_search_list.find('li.active') 
     jsPane.animate({top:"-=95px"}); 
    } 
}); 
$('.top_search').click(function(){ 
    if (!search_element.first().hasClass('active')) { 
     active_search_element.removeClass('active'); 
     active_search_element.prev('li').addClass('active'); 
     active_search_element = live_search_list.find('li.active') 
     jsPane.animate({top:"+=95px"}); 
    } 
}); 
+0

ofcourse,现在积极的类追加到第二个元素,并永远留在它,很多thx的帮助,BR – Lukas

1

您并未将active_search_element设置为新的活动元素!

线:

active_search_element = live_search_list.find('li.active') 

仅选择在该时刻的元件,它不会奇迹般地保持更新。

$('.bottom_search').click(function(){ 
    if (!search_element.last().hasClass('active')) { 
     active_search_element.removeClass('active'); 
     active_search_element = active_search_element.next('li').addClass('active'); 
     jsPane.animate({top:"-=95px"}); 
    } 
}); 

$('.top_search').click(function(){ 
    if (!search_element.first().hasClass('active')) { 
     active_search_element.removeClass('active'); 
     active_search_element = active_search_element.prev('li').addClass('active'); 
     jsPane.animate({top:"+=95px"}); 
    } 
}); 
+0

它适用于,thx,我认为这也是更灵活的方式来写它 – Lukas