jQuery动画scrolltop回调 [英] jquery animate scrolltop callback

查看:317
本文介绍了jQuery动画scrolltop回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下jquery,将页面滚动到顶部,然后执行一个回调函数。

I have the following jquery that scrolls the page to the top, then performs a callback function.

问题是,即使页面已经在顶部,它仍然等待'1000'经过,然后执行回调,我不想它。

The problem is that even if the page is already at the top, it still waits for the '1000' to elapse before performing the callback, which i dont want it to.

$('html').animate({scrollTop:0}, 1000, 'swing', function(){

//do something

});


推荐答案

不等待



它从顶部到顶部动画你的文档...有趣,但这是怎么回事。你有其他的选择。

It doesn't wait

It animates your document from top to the top... Funny but that's how it is. You have other alternatives. Either:


  • 在滚动之前检查您的页面位置(如提供一些代码所建议的)或

  • 使用一些工作不同的jQuery插件

我的 .scrollintoview() jQuery插件以这种方式工作。

My .scrollintoview() jQuery plugin works that way. It scrolls only if it has to and runs a complete handler after scrolling is complete (if there was scrolling in the first place) or immediately if scrolling is not needed.

但是,只有在滚动完成后(如果有第一个滚动)或者不需要滚动时,它需要一些需要滚动的元素。你实际上没有将任何元素滚动到视图,而只是HTML。这当然不是最安全的方式。最好使用:

But it needs some element that needs to be scrolled. You're actually not scrolling any element into view but rather just HTML. This is of course not the safest way. It's better to use either:

$("html,body")

$(window)

支持更多的跨浏览器。因此,您的 $(html)可能无法在所有人中使用。

It's more cross browser supported. So your $("html") may not work in all of them.

这篇关于jQuery动画scrolltop回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆