HTML5 实现页面滚动

最近项目中需要使用页面滚动,其实页面滚动相对来说挺简单的,但是在实际的使用过程中,尝试了几种不同的方法,总结一下:

一、利用html 标签

<marquee direction="up" behavior="scroll" scrollamount="1080" scrolldelay="10000" loop="-1" width="1920" height="1080" bgcolor="#0099FF" hspace="0" vspace="0">
</marquee>

页面会根据设置按照像素点滚动,使用中效果不太好;

二、使用JavaScript中scrolltoview()和setinterval方法

参考《JavaScript高级程序设计》

HTML5 实现页面滚动

定时将浏览器页面滚动到指定位置

var section1 = document.getElementById("section1");
var section2 = document.getElementById("section2");
var section3 = document.getElementById("section3");

setInterval(function () { /*$('#section1').addClass('active'); $('#section2').removeClass('active'); $('#section3').removeClass('active');console.log("scroll:1");*/ window.scroll(0, 1080); section1.scrollIntoView(); }, 5000);
setInterval(function () { /*$('#section2').addClass('active'); $('#section1').removeClass('active'); $('#section3').removeClass('active');console.log("scroll:2");*/ window.scroll(0, 2160); section2.scrollIntoView(); }, 10000);
setInterval(function () { /*$('#section3').addClass('active'); $('#section1').removeClass('active'); $('#section2').removeClass('active');console.log("scroll:3");*/ window.scroll(0, 3240); section3.scrollIntoView(); }, 15000);

参考文献

1、marquee

2、HTML5 ScrollToView()