2011-03-16 99 views
0

功能:http://www.awaismuzaffar.com/examples/content_1.htmlJQuery的 - 创建一个隐藏如果你去/显示内容

您可以查看我的jQuery功能的演示。

基本上,它假设隐藏/显示内容取决于哪个鼠标结束了哪些div

但是,此时您会意识到,当鼠标悬停在所有div元素上时,所有文本都会相互重叠。

我需要找出一种方法来隐藏前一个div的内容,当鼠标在另一个div上时。不知道如何做到这一点。

这里是jQuery的代码片段:

$(document).ready(function(){ 
    var current_id; 
    $('div.video_image').hover(
     function(){ 
      current_id = $(this).attr('id').split('_')[1]; 
      $('div#text_' + current_id).stop().animate({'opacity': '1'}, 'slow');     
     } 
    ); 
}); 

感谢。

+1

'jQuery'有一个'切换()'方法。你应该使用这个。 – 2011-03-16 21:24:32

+0

我会去与尼尔斯的建议和使用切换,如果您寻找“效果”,然后使用.fadeIn()或.fadeOut() – 2011-03-16 21:28:04

回答

0

喜欢的东西可能会奏效。下面是这个脚本的动作的一例:http://jsbin.com/apiya3

$(document).ready(function(){ 
    var current_id, previous_id; 
    $('div.video_image').hover(
     function(){ 
      previous_id = current_id; 
      if (previous_id) 
       $('div#text_' + previous_id).stop().animate({'opacity': '0'}, 'slow'); 

      current_id = $(this).attr('id').split('_')[1]; 
      $('div#text_' + current_id).stop().animate({'opacity': '1'}, 'slow');     
    }); 
}); 
+0

感谢您的所有答复。这对我来说是一个很好的解决方案。你介意给我一个简单的解释,说明这是如何工作的?谢谢。 – sidewinder 2011-03-16 22:20:00

0

您是否尝试过element.hide()element.show()的功能?他们将速度作为参数(例如'慢','快'等)。

0

一个解决办法是清除大格(.empty功能),并把新的文本内(.append()函数或.html()函数)

0

您应该使用代替悬停鼠标悬停及移出功能,因为当你将鼠标移开元素悬停不复位。此外,show()和hide()函数比动画不透明度更容易。

$(document).ready(function(){ 
var current_id; 
$('div.video_image').mouseover(
    function(){ 
     current_id = $(this).attr('id').split('_')[1]; 
     $('div#text_' + current_id).stop().show('slow');     
    }); 
$('div.video_image').mouseout(
    function(){ 
     $('div#textcontainer).children().hide('slow'); 

}); 

或者类似的东西,至少...