jQuery .hover不起作用

3

你好,这是我的代码:

当我悬停在 #open 上时,#pull_down_content 应该从头部向下移动,当我移开 #open 时它应该回到原位。但是当我测试代码时,页面加载后 #pull_down_content 就会在我悬停之前就往下移动。

$(function() {

//Open on hover 
$('#open').hover((function(){
    $('#pull_down_content').animate({'top':'-0px'},1000);
})

//Close when not hovered
(function(){
    $('#pull_down_content').animate({'top':'-340px'},1000);

})
});
);

1
https://www.google.com/search?q=jquery+hover 第一个结果。为什么你不用谷歌? - iambriansreed
2个回答

15

请尝试按以下方式修复您的函数:

$(function() {        
    $('#open').hover(function(){ //Open on hover 
        $('#pull_down_content').animate({'top':'-0px'},1000);
    },    
    function(){ //Close when not hovered
        $('#pull_down_content').animate({'top':'-340px'},1000);    
    });
});

1

只需要进行一点小修复

$('yourElement').hover(
   function(){
      // hover code
   }, function(){
      // unhover code 
   }
);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接