jQuery .hover not working

时光总嘲笑我的痴心妄想 提交于 2019-12-31 02:42:05

问题


Hi What is wrong with my code here:

When I hover over #open #pull_down_content should be moving down the page from the header and when I move away from #open it should move back up. But when I test the code as soon as the page loads #pull_down_content moves down the screen before I even hover over it.

$(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:


Try fixing your function like below,

$(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);    
    });
});



回答2:


Just need a little fix

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


来源:https://stackoverflow.com/questions/11142153/jquery-hover-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!