2011-04-01 54 views
1

我是新来的jQuery,我试图做一个简单的淡入淡出动画,使实践中我所学到的东西,不幸的是我不是得到我想要的结果 我有一个列表元素,每个li元素中都有一个链接: 我想要什么:当我滚动一个li元素时,我想让相对的<a>链接淡入,当我推出I希望它淡出。这是我下面的代码:如何淡入淡出每个列表元素中的相对链接请

$(function(){ 
    $("a.viewAllProductsLink").hide(); 
    $(".scrollable ul>li").each(function(){ 

     $(this).mouseenter(function(){ 
      $("a.viewAllProductsLink").fadeIn("slow");  
     }) 
     .mouseleave(function(){ 
      $("a.viewAllProductsLink").fadeOut("slow"); 
     }); 
    });  
}); 
+0

请张贴您的标记。 – jensgram 2011-04-01 10:31:06

回答

1

当你调用$('a.viewAllProductsLink')你选择所有与类的链接,当你想要做的是只选择你翻身的元素中的链接。要做到这一点,使用$(this).find()

$(function(){ 
    $("a.viewAllProductsLink").hide(); 
    $(".scrollable ul>li").each(function(){ 

     $(this).mouseenter(function(){ 
      $(this).find("a.viewAllProductsLink").fadeIn("slow"); 
     }) 
     .mouseleave(function(){ 
      $(this).find("a.viewAllProductsLink").fadeOut("slow"); 
     }); 
    }); 
}); 

此外,它的innefficient在这里使用.each()因为所有的元素都具有相同的行为,你可能希望动画前使用.stop()保持动画队列从建立当你多次快速停留在元素上时:

$(function(){ 
    $("a.viewAllProductsLink").hide(); 
    $(".scrollable ul>li").mouseenter(function(){ 
      $(this).find("a.viewAllProductsLink").stop(true,true).fadeIn("slow"); 
     }) 
     .mouseleave(function(){ 
      $(this).find("a.viewAllProductsLink").stop(true,true).fadeOut("slow"); 
     }); 
    }); 
}); 
+0

是的,它的工作原理,谢谢你的快速反应。基于你的expreience什么是开始潜入jquery的最佳方式,我正在阅读jquery的行动书,但我觉得有点失落,所以有一些建议plz – user687347 2011-04-01 17:56:55

+0

我通过使用本网站和通过筛选http:/ /api.jquery.com,以及使用http://jsfiddle.net实现和试验所有的例子......除此之外,但这应该有很多事情要做。在实际的Javascript中获得一个良好的基础也会有很长的路要走。 – mVChr 2011-04-01 21:30:24

+0

谢谢,我会尽力坚持这两个网站,jQuery官方网站和jsfiddle。 – user687347 2011-05-16 13:12:44