var timer; var currentpos = 0; function scroll() { window.scrollBy(0,50); var scrollTop = $(document).scrollTop(); currentpos += 50; if (currentpos != scrollTop) { stop(); currentpos = 0; } } function start() { timer = setInterval(scroll, 1000); } function stop() { clearInterval(timer); } document.ondblclick = start; document.onclick = stop; </script>
因为一直得不到scrollTop,滚动条高度,搞的很狼狈啊,最后jquery 这样得到。$(document).scrollTop();
js 是下面的,
function getScrollTop() { var scrollTop = 0; if (document.documentElement && document.documentElement.scrollTop) { scrollTop = document.documentElement.scrollTop; } else if (document.body) { scrollTop = document.body.scrollTop; } return scrollTop; }
基础不好,害死人啊