为了账号安全,请及时绑定邮箱和手机立即绑定

jQuery .scrollTop(); +动画

jQuery .scrollTop(); +动画

qq_花开花谢_0 2019-12-16 10:52:37
jQuery .scrollTop(); +动画单击按钮时,我将页面设置为滚动到顶部。但是首先我使用了一个if语句来查看页面顶部是否未设置为0。然后如果它不是0,则使页面动画以滚动到顶部。var body = $("body");var top = body.scrollTop() // Get position of the bodyif(top!=0){  body.animate({scrollTop:0}, '500');}现在,棘手的部分是在页面滚动到顶部之后对某些内容进行动画处理。所以我的下一个想法是,找出页面位置。因此,我使用控制台日志来查找。console.log(top);  // the result was 365这给了我365的结果,我想这就是我滚动到顶部之前的位置编号。我的问题是如何将位置设置为0,以便添加可以在页面为0时运行的另一个动画?
查看完整描述

4 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

试试这个代码:


$('.Classname').click(function(){

    $("html, body").animate({ scrollTop: 0 }, 600);

    return false;

});


查看完整回答
反对 回复 2019-12-16
?
交互式爱情

TA贡献1712条经验 获得超3个赞

用这个:


$('a[href^="#"]').on('click', function(event) {


    var target = $( $(this).attr('href') );


    if( target.length ) {

        event.preventDefault();

        $('html, body').animate({

            scrollTop: target.offset().top

        }, 500);

    }


});


查看完整回答
反对 回复 2019-12-16
?
莫回无

TA贡献1865条经验 获得超7个赞

尝试以下方法:


var body = $("body, html");

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

if(top!=0)

{

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

         //DO SOMETHING AFTER SCROLL ANIMATION COMPLETED

          alert('Hello');

      });

}


查看完整回答
反对 回复 2019-12-16
  • 4 回答
  • 0 关注
  • 609 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信