jQuery整屏滾動
1.jQuery.mousewheel外掛使用
jQuery中沒有滑鼠滾輪事件,原生js中的滑鼠滾輪事件不相容,可以使用jQuery的滾輪事件外掛
jQuery.mousewheel.js
2.函式節流
JavaScript中有些事件的觸發頻率非常高,比如onresize事件(jq中是resize),onmousemove事件(jq中是mousemove)以及上面說的滑鼠滾輪事件,在短事件內多處觸發執行繫結的函式,可以巧妙地使用定時器來減少觸發的次數,實現函式節流。
例子:整屏滾動
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>滾輪事件</title>
<script type="text/javascript" src="../jQuery庫/jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="../jQuery庫/jquery.mousewheel.js"></script>
<link rel="stylesheet" type="text/css" href="../CSS3/wheelstyle.css">
<script type="text/javascript">
/*$(window).mousewheel(function(event,data){ 滑鼠滾輪向上1,向下-1 alert(data); console.log(data);*/ /*})*/ $(function(){ var $screen = $('.pages_con'); /*獲取整個父級div*/ var $pages = $('.pages'); /*獲取頁面div*/ var $h = $(window).height(); /*視窗的高度*/ //alert($h); var $nowscreen = 0; /*重新整理次數變數*/ var $len = $pages.length; /*有多少個頁面div*/ var $points = $('.points li'); /*獲取li*/ var timer = null; /*定時變數為空值*/ $pages.css({'height':$h}); /*修改頁面樣式,高度設為可視視窗大小*/ $points.click(function(){ /*點選li即小圓點,執行*/ $nowscreen = $(this).index(); /*重新整理次數變數=點選的數值*/ /*第幾個li,增加active類,同輩的li刪除active類*/ $points.eq($nowscreen).addClass('active').siblings().removeClass('active'); /*父級div樣式top值,等於負的可視螢幕大小*重新整理數*/ $screen.animate({'top':-$h*$nowscreen}); //document.title=$nowscreen; /*頁面div的第幾個,增加moving類,同輩的div刪除moving類*/ $pages.eq($nowscreen).addClass('moving').siblings().removeClass('moving'); }) /*滑鼠滾輪函式,事件變數event,滾輪次數dat*/ $(window).mousewheel(function(event,dat){ /*沒執行一次清空timer變數,重新計算*/ clearTimeout(timer); /*定時器函式*/ timer = setTimeout(function(){ /*向下滾*/ if(dat==-1){ $nowscreen++; } /*向上滾*/ else{ $nowscreen--; } /*最頂部限定0*/ if($nowscreen<0){ $nowscreen=0; } /*最底部限定4,0-4*/ if($nowscreen>$len-1){ $nowscreen = $len-1 } $screen.animate({'top':-$h*$nowscreen}); //document.title=$nowscreen; $pages.eq($nowscreen).addClass('moving').siblings().removeClass('moving'); $points.eq($nowscreen).addClass('active').siblings().removeClass('active'); },200) }) }) </script>
</head>
<body>
<div class="pages_con"> <div class="pages"> <div class="main_con"> <div class="left_img"> <img src="../images/1.png" alt="boy&girl"> </div> <div class="right_info">日常裡,與你相抱</div> </div> </div> <div class="pages page2"> <div class="main_con"> <div class="left_img"> <img src="../images/2.png" alt="櫻花美"> </div> <div class="right_info">櫻花下,是你的小提琴</div> </div> </div> <div class="pages page3"> <div class="main_con"> <div class="left_img"> <img src="../images/3.png" alt="星空"> </div> <div class="right_info">星空下,你我偶遇</div> </div> </div> <div class="pages page4"> <div class="main_con"> <div class="left_img"> <img src="../images/4.png" alt="黃昏美"> </div> <div class="right_info">黃昏時,一人獨奏</div> </div> </div> <div class="pages page5"> <div class="main_con"> <div class="left_img"> <img src="../images/5.png" alt="櫻花帥"> </div> <div class="right_info">琴聲中,櫻花飄揚</div> </div> </div> <ul class="points"> <li class="active"></li> <li></li> <li></li> <li></li> <li></li> </ul> </div>
</body>
</html>
CSS檔案:
/ CSS Document /
body,ul{
margin: 0; /取消列表和系統自帶縮排/
padding: 0;
}
ul{
list-style: none;
}
/父級div/
.pages_con{
position: fixed;
left: 0;
top: 0;
width: 100%;
overflow: hidden;
}
/每一頁面/
.pages{
height: 768px;
position: relative;
}
/小圓點ul/
.points{
width: 16px;
height: 176px;
position: fixed;
right: 20px;
top: 50%;
margin-top: -88px;
}
/每一個小圓點/
.points li{
width: 13px;
height: 13px;
border-radius: 50%;
margin: 16px 0;
border: 1px solid #666;
cursor: pointer;
}
/jQuery操作類/
.points .active{
background-color: #666666;
}
/頁面大小/
.main_con{
width: 1366px;
height: 768px;
position: relative;
}
/頁面的圖片/
.main_con .left_img{
position: relative;
left: -40px;
opacity: 0;
filter: alpha(opacity=0);
transition: all 1000ms ease 300ms;
}
/頁面的文字/
.main_con .right_info{
width: 40px;
height: 300px;
position: absolute;
left: -50px;
top: 50%;
margin-top: -150px;
font-size: 30px;
color: #666666;
text-align: justify;
opacity: 0;
filter: alpha(opacity=0);
transition: all 1000ms ease 300ms;
}
/jQuery操作動畫/
.moving .main_con .left_img{
left: 0;
opacity: 1;
filter: alpha(opacity=100);}
.moving .main_con .right_info{
left: 30px;
opacity: 1;
filter: alpha(opacity=100);}