JavaScript控制滚动条滚动到指定位置1.打开⼀个⽹址,F12,定位到我们想到滚动的位置
⽐如定位到翻页的位置,我们可以取到翻页div的class=page-inner
2.在Consle输⼊滚动效果的Js代码:
1function elementPosition(obj) {
2var curleft = 0, curtop = 0;
3if (obj.offsetParent) {
4        curleft = obj.offsetLeft;
5        curtop = obj.offsetTop;
6while (obj = obj.offsetParent) {
7            curleft += obj.offsetLeft;
8            curtop += obj.offsetTop;
9        }
10    }
11return { x: curleft, y: curtop };
12 }
13
14var repeatCount = 0;
15var cTimeout;
16var timeoutIntervals = new Array();
17var timeoutIntervalSpeed;
18function ScrollSmoothly(scrollPos, repeatTimes) {
19if (repeatCount < repeatTimes) {
20        window.scrollBy(0, 50);
21    }
22else {
23        repeatCount = 0;
24        clearTimeout(cTimeout);
25return;
26    }
27    repeatCount++;
28    cTimeout = setTimeout("ScrollSmoothly('" + scrollPos + "','" + repeatTimes + "')", 10);
29 }
30
31function ScrollToControl(classname) {
32var elem = ElementsByClassName(classname)[0];
33var scrollPos = elementPosition(elem).y - 200; //部分页⾯存在固定的Head部分
34    scrollPos = scrollPos - document.documentElement.scrollTop;
35var remainder = scrollPos % 50;
36var repeatTimes = (scrollPos - remainder) / 50;
37    ScrollSmoothly(scrollPos, repeatTimes);
38    window.scrollBy(0, remainder);
39 }
40
如何设置滚动条的位置
41 ScrollToControl("page-inner");
3.执⾏,效果如下:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。