滚动到页面顶部 [英] Scroll to top of page

查看:162
本文介绍了滚动到页面顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以在单击按钮时强制浏览器滚动到顶部?

Is there a simple way to force the browser to scroll to the top if a button is clicked?

我尝试过

jQuery('html').scrollTop();
jQuery('body').scrollTop();
jQuery(window).scrollTop();

它们似乎都不滚动到页面顶部.

None of them seem to scroll to the top of the page.

推荐答案

由于浏览器异类,某些浏览器将对'html'做出响应,而某些浏览器将对'body'做出响应.也许这只是我的运气,但是.scrollTop(0)从未使我移动页面.试一试:

Due to cross browser oddness, some browsers will respond to 'html' and some to 'body'. And maybe its just my luck, but .scrollTop(0) has never worked for me to move the page. Give this a shot:

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

此版本已通过测试,可用于所有台式机浏览器和移动设备的跨浏览器.

This version is tested and cross browser for all desktop browsers, and mobile devices.

这篇关于滚动到页面顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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