防止jQuery在固定导航上滚动

时间:2015-02-13 15:57:39

标签: javascript jquery

我正在使用顶部有固定导航栏的网站。单击图标时,搜索输入字段使用jQuery在导航栏中滑出。当用户在页面上向下滚动并单击图标时,页面会自动滚动回到顶部。有没有一种简单的方法来防止下面的事件处理程序中的此行为?

$('.search-button').on('click', function(){
    $(this).hide();
    $('.search-field').animate({'width':'toggle'});
});

1 个答案:

答案 0 :(得分:-1)

$('.search-button').on('click', function(e){ 
e.preventDefault(); //If this method is called, the default action of the event will not be triggered.
$(this).hide(); 
$('.search-field').animate({'width':'toggle'}); });




pass the event e as argument and cancel the default behaviour

将起作用

相关问题