滚动页面。
使用window.scrollBy
指定页面基于当前位置的滚动偏移量
。
正数坐标会朝页面的右下方滚动,负数坐标会滚向页面的左上方。
// 向下滚动一屏
window.scrollBy(0, window.innerHeight);
// 向上滚动一屏
window.scrollBy(0, -window.innerHeight);
除了接受正负偏移量
,window.scrollBy
还能接受ScrollToOptions
作为配置。
ScrollToOptions
包含如下三个参数:
smooth
(平滑滚动),instant
(瞬间滚动)。默认值auto
,效果等同于instant
ScrollToOptions
可以作为如下方法的配置:
接下来介绍其他几个滚动相关API
:
指定页面需要滚动的绝对位置。
window.scroll(x-coord, y-coord)
window.scroll({
top: 100,
left: 100,
behavior: 'smooth'
});
与window.scroll
相同。
滚动指定的页数。
该API
仅在Firefox
浏览器支持。
// 当前文档向下翻一页
window.scrollByPages(1);
// 当前文档向上翻一页
window.scrollByPages(-1);
滚动指定的行数。
该API
仅在Firefox
浏览器支持。
// 当前页面向下翻五行
window.scrollByLines(5);
// 当前页面向上翻五行
window.scrollByLines(-5);
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有