jQuery .scrollTop(); +动画

jQuery .scrollTop(); +动画

单击按钮时,我将页面设置为滚动到顶部。但是首先我使用了一个if语句来查看页面顶部是否未设置为0。然后如果它不是0,则使页面动画以滚动到顶部。


var body = $("body");

var top = body.scrollTop() // Get position of the body


if(top!=0)

{

  body.animate({scrollTop:0}, '500');

}

现在,棘手的部分是在页面滚动到顶部之后对某些内容进行动画处理。所以我的下一个想法是,找出页面位置。因此,我使用控制台日志来查找。


console.log(top);  // the result was 365

这给了我365的结果,我想这就是我滚动到顶部之前的位置编号。


我的问题是如何将位置设置为0,以便添加可以在页面为0时运行的另一个动画?


qq_花开花谢_0
浏览 676回答 4
4回答

慕侠2389804

试试这个代码:$('.Classname').click(function(){    $("html, body").animate({ scrollTop: 0 }, 600);    return false;});

交互式爱情

用这个:$('a[href^="#"]').on('click', function(event) {    var target = $( $(this).attr('href') );    if( target.length ) {        event.preventDefault();        $('html, body').animate({            scrollTop: target.offset().top        }, 500);    }});

莫回无

尝试以下方法:var body = $("body, html");var top = body.scrollTop() // Get position of the bodyif(top!=0){       body.animate({scrollTop :0}, 500,function(){         //DO SOMETHING AFTER SCROLL ANIMATION COMPLETED          alert('Hello');      });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JQuery