js全屏滾動

來源:互聯網
上載者:User

標籤:pos   ext   osi   運動   oct   one   round   res   動畫架構   

<!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全屏滾動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.