【发布时间】:2011-07-21 11:37:26
【问题描述】:
我在一个网站上工作,其中有一系列元素,并且在任何时候都只有一个元素必须处于活动状态。当该元素处于活动状态时,会显示一个“详细信息”div。
当您将鼠标悬停在这些元素上时,它们会变得活跃,但正如我所说,它们中只有一个可以同时处于活跃状态。我目前正在使用活动类设置活动元素。
在我当前的代码中,当用户将鼠标悬停在任何元素上时会发生这种情况:
- 前一个活动元素移除了“活动”类,并且淡出
- 在fadeOut 回调中,悬停的元素变为活动状态(获取“活动”类),并且是fadeIn
- 如果当前没有活动元素,则悬停元素变为活动元素(“活动”类)并淡入
这没问题,但是当您在元素之间快速悬停时,会有一个短暂的时刻没有元素处于活动状态,因此不仅仅是元素获得活动类并显示出来。
你会如何解决这个问题?
这是我的代码:
function setActive(selected) {
//stores the active element in a variable
active = selected;
//checks if there are currently elements with the 'active' class in the DOM
if ( $('#info article.active').length > 0) {
//if there is any currently active element, and its element_id attribute is not the one stored in the active variable
//it gets the 'active' class removed, its hidden, and in the callback of the animation
//the newly selected element gets the class 'active' and is shown with fadeIn
$('#info article.active[element_id!="' + selected + '"]').removeClass('active').fadeOut('fast', function(){
$('#info article[element_id="' + selected +'"]').addClass('active').fadeIn('normal');
});
} else {
//if there is no current active element, the newly selected one is applied the class active, and shown with fadeIn
$('#info article[element_id="' + selected +'"]').addClass('active').fadeIn('normal');
}
}
【问题讨论】:
标签: javascript jquery