jquery实现全屏滚动
在很多情况下,我们需要页面的全屏滚动,尤其是移动端。今天简要的介绍一下全屏滚动的知识。
一.全屏滚动的原理
1.js动态获取屏幕的高度。
获取屏幕的高度,设置每一屏幕的高度。
2.监听mousewheel事件。
监听mousewheel事件,并判断滚轮的方向,向上或向下滚动一屏。
二.jQuery插件fullpages介绍
fullPage.js是一个基于jQuery的插件,它能够很方便、很轻松的制作出全屏网站,主要功能有:
- 支持鼠标滚动
- 支持前进后退和键盘控制
- 多个回调函数
- 支持手机、平板触摸事件
- 支持CSS3动画
- 支持窗口缩放
- 窗口缩放时自动调整
- 可设置滚动宽度、背景颜色、滚动速度、循环选项、回调、文本对齐方式等
使用方法
1、引入文件
<linkrel="stylesheet"href="css/jquery.fullPage.css"> <scriptsrc="js/jquery.min.js"></script> <scriptsrc="js/jquery.fullPage.js"></script>
2、HTML
<divid="dowebok"> <divclass="section"> <h3>第一屏</h3> </div> <divclass="section"> <h3>第二屏</h3> </div> <divclass="section"> <h3>第三屏</h3> </div> <divclass="section"> <h3>第四屏</h3> </div> </div>
每个section代表一屏,默认显示“第一屏”,如果要指定加载页面时显示的“屏幕”,可以在对应的section加上class=”active”,如:
<divclass="sectionactive">第三屏</div>
同时,可以在section内加入slide(左右滑动),如:
<divid="fullpages"> <divclass="section">第一屏</div> <divclass="section">第二屏</div> <divclass="section"> <divclass="slide">第三屏的第一屏</div> <divclass="slide">第三屏的第二屏</div> <divclass="slide">第三屏的第三屏</div> <divclass="slide">第三屏的第四屏</div> </div> <divclass="section">第四屏</div> </div>
3、JavaScript
$(function(){
$('#fullpages').fullpage();
});
可以进行跟多的配置:
$(document).ready(function(){
$('#fullpages').fullpage({
//Navigation
menu:'#menu',
lockAnchors:false,
anchors:['firstPage','secondPage'],
navigation:false,
navigationPosition:'right',
navigationTooltips:['firstSlide','secondSlide'],
showActiveTooltip:false,
slidesNavigation:true,
slidesNavPosition:'bottom',
//Scrolling
css3:true,
scrollingSpeed:700,
autoScrolling:true,
fitToSection:true,
fitToSectionDelay:1000,
scrollBar:false,
easing:'easeInOutCubic',
easingcss3:'ease',
loopBottom:false,
loopTop:false,
loopHorizontal:true,
continuousVertical:false,
normalScrollElements:'#element1,.element2',
scrollOverflow:false,
touchSensitivity:15,
normalScrollElementTouchThreshold:5,
//Accessibility
keyboardScrolling:true,
animateAnchor:true,
recordHistory:true,
//Design
controlArrows:true,
verticalCentered:true,
resize:false,
sectionsColor:['#ccc','#fff'],
paddingTop:'3em',
paddingBottom:'10px',
fixedElements:'#header,.footer',
responsiveWidth:0,
responsiveHeight:0,
//Customselectors
sectionSelector:'.section',
slideSelector:'.slide',
//events
onLeave:function(index,nextIndex,direction){},
afterLoad:function(anchorLink,index){},
afterRender:function(){},
afterResize:function(){},
afterSlideLoad:function(anchorLink,index,slideAnchor,slideIndex){},
onSlideLeave:function(anchorLink,index,slideIndex,direction,nextSlideIndex){}
});
});
三.动手写全屏滚动
这里主要介绍监听mousewheel事件及滚动。
由于mousewheel事件的兼容性,引用jquery-mousewheel插件来监听滚轮事件。
通过参数delta可以获取鼠标滚轮的方向和速度(旧版本需要传delta参数,新版本不需要,直接用event取)。如果delta的值是负的,那么滚轮就是向下滚动,正的就是向上。
//usingon
$('#my_elem').on('mousewheel',function(event){
console.log(event.deltaX,event.deltaY,event.deltaFactor);
});
//usingtheeventhelper
$('#my_elem').mousewheel(function(event){
console.log(event.deltaX,event.deltaY,event.deltaFactor);
});
可以根据需求使用fullpages实现全屏滚动(上下,左右),也可以使用jquery-mousewheel定制不同高度的全屏滚动。
以上就是本文的全部内容,希望对大家的学习有所帮助。