您好,欢迎来到安汇情感。
搜索
您的当前位置:首页页面加载完毕后滚动条自动滚动一定位置_javascript技巧

页面加载完毕后滚动条自动滚动一定位置_javascript技巧

来源:安汇情感


昨天有一需求,是希望页面加载完毕后向左自动滚动一定位置。

一直以为只要给页面的 document.documentElement.scrollLeft 设置一个数值就生效,结果失望了~
今天抽空一查,才发现:
使用document.documentElement.scrollLeft 设置值,必须在人为事件触发下才生效;
想要页面加载完毕时自动滚动一定距离,则使用jquery的animate,如下面例子:

$("html,body").animate({"scrollLeft": "300px"}, 1000);
$("html,body").animate({"scrollTop": "300px"}, 1000);

demo:
代码如下:




自动滚动





点击


/*window.onload = function(){
window.scroll(0,300);

$(".btn").on("click", function(){

document.documentElement.scrollLeft = "500";
var oTop = document.body.scrollTop || document.documentElement.scrollTop;
var oLeft = document.body.scrollLeft || document.documentElement.scrollLeft;

alert(oLeft);
});
}*/

$(function(){
$("html,body").animate({"scrollLeft": "300px"}, 1000);
});




Copyright © 2019- ahftz.com 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务