首页 > 代码库 > js全屏滚动
js全屏滚动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
*{margin:0;padding:0;}
ul{list-style: none;}
.content{width: 100%;height: 100%;}
#nums{position: fixed;top: 100px;right: 100px;}
#nums li{width: 20px;height: 20px;border-radius: 50%;background: #fff;margin-bottom: 20px;}
#nums li.active{background: #333;}
</style>
</head>
<body>
<div class="wrap">
<div class="content" style="background: #f00;"></div>
<div class="content" style="background: #FFA500;"></div>
<div class="content" style="background: #00f;"></div>
<ul id="nums">
</ul>
</div>
<script type="text/javascript">
window.onload=function(){
gun()
tab()
}
function gun(){
//添加滚动监听
document.addEventListener(‘mousewheel‘,wheel,false);
//判断一次滚动是是否完成
var isComplete = true;
//隐藏滚动条
document.body.style.overflow=‘hidden‘;
//获取滚动的元素
var fullList = document.getElementsByClassName("content");
//因为是类数组对象,不是数组对象,所以只能使用call的方式来调用
Array.prototype.forEach.call(fullList,function(value){
//获取一个网页满屏的高
value.style.height = window.innerHeight +‘px‘;
})
//如果窗口大小改变执行的函数
window.onresize = function(){
Array.prototype.forEach.call(fullList,function(value){
value.style.height = window.innerHeight +‘px‘;
});
//改变窗口大小后,应该仍是一个元素占满全屏
if(document.body.scrollTop % window.innerHeight)
{
isComplete = false;
//根据四舍五入判断滚动位置
let tmp = Math.round(document.body.scrollTop / window.innerHeight)* window.innerHeight;
//使用运动框架
move(document.body,{‘scrollTop‘:tmp},function(){
isComplete = true;
});
}
};
//滚动函数
function wheel(e){
//等待上一个滚动完成
if(isComplete){
//滚动进行时
isComplete = false;
//判断是往上滚动还是往下滚动
if(e.wheelDelta < 0){
//要滚动到的点
let arrivePoint = document.body.scrollTop + window.innerHeight;
var x=arrivePoint/window.innerHeight
//最大的滚动点
let maxBottom = document.body.offsetHeight - window.innerHeight;
//如果超出了最大的滚动点,则赋值为最大滚动点
arrivePoint = arrivePoint > maxBottom ? maxBottom : arrivePoint;
move(document.body,{‘scrollTop‘:arrivePoint},function(){
isComplete = true;
});
}else{
let arrivePoint = document.body.scrollTop - window.innerHeight;
var x=arrivePoint/window.innerHeight
//最小滚动点为0
arrivePoint = arrivePoint < 0 ? 0 :arrivePoint;
move(document.body,{‘scrollTop‘:arrivePoint},function(){
isComplete = true;
});
}
var li=document.getElementById(‘nums‘).getElementsByTagName(‘li‘)
if(x>=li.length){
x=li.length-1
}
if(x<0){
x=0
}
for(var i=0;i<li.length;i++){
li[i].className=""
}
li[x].className="active"
}
}
}
/*添加视图*/
function tab(){
var con=document.getElementsByClassName(‘content‘);
var num=document.getElementById(‘nums‘);
for(var i=0;i<con.length;i++){
var li=document.createElement(‘li‘)
num.appendChild(li)
}
num.getElementsByTagName(‘li‘)[0].className="active"
}
/*运动框架*/
//这就不多说了,js动画框架
function move(obj,json,fn){
clearInterval(obj.times);
var fl=true;
obj.times=setInterval(function(){
for(var attr in json){
var le=0;
if(attr=="opacity"){
le=Math.round(parseFloat(getStyle(obj,attr))*100);
}else if(attr=="scrollTop" || attr=="scrollLeft"){
le=obj[attr];
}else{
le=parseInt(getStyle(obj,attr))/*||obj[attr]*/;
}
var speed=(json[attr]-le)/10;
speed>0 ? speed=Math.ceil(speed) : speed=Math.floor(speed);
if(le!=json[attr]){
fl=false;
}else{
fl=true;
}
if(attr=="opacity"){
obj.style.filter=‘alpha(opacity:‘+(le+speed)+‘)‘;
obj.style.opacity=(le+speed)/100;
}else if(obj[attr]||obj[attr] == 0){
obj[attr] = le + speed;
}
else{
obj.style[attr]=le+speed+"px";
}
if(fl){
clearInterval(obj.times);
if(fn){
fn();
}
}
}
},20)
}
function getStyle(obj, attr)
{
if(obj.currentStyle)
{
return obj.currentStyle[attr];
}
else
{
return getComputedStyle(obj,false)[attr];
}
}
</script>
</body>
</html>
js全屏滚动