jquery實現(xiàn)全屏滾動
在很多情況下,我們需要頁面的全屏滾動,尤其是移動端。今天簡要的介紹一下全屏滾動的知識。
一.全屏滾動的原理
1.js動態(tài)獲取屏幕的高度。
獲取屏幕的高度,設(shè)置每一屏幕的高度。
2.監(jiān)聽mousewheel事件。
監(jiān)聽mousewheel事件,并判斷滾輪的方向,向上或向下滾動一屏。
二.jQuery插件fullpages介紹
fullPage.js 是一個基于 jQuery 的插件,它能夠很方便、很輕松的制作出全屏網(wǎng)站,主要功能有:
- 支持鼠標(biāo)滾動
- 支持前進(jìn)后退和鍵盤控制
- 多個回調(diào)函數(shù)
- 支持手機、平板觸摸事件
- 支持 CSS3 動畫
- 支持窗口縮放
- 窗口縮放時自動調(diào)整
- 可設(shè)置滾動寬度、背景顏色、滾動速度、循環(huán)選項、回調(diào)、文本對齊方式等
使用方法
1、引入文件
<link rel="stylesheet" href="css/jquery.fullPage.css"> <script src="js/jquery.min.js"></script> <script src="js/jquery.fullPage.js"></script>
2、HTML
<div id="dowebok">
<div class="section">
<h3>第一屏</h3>
</div>
<div class="section">
<h3>第二屏</h3>
</div>
<div class="section">
<h3>第三屏</h3>
</div>
<div class="section">
<h3>第四屏</h3>
</div>
</div>
每個 section 代表一屏,默認(rèn)顯示“第一屏”,如果要指定加載頁面時顯示的“屏幕”,可以在對應(yīng)的 section 加上class=”active”,如:
<div class="section active">第三屏</div>
同時,可以在 section 內(nèi)加入 slide(左右滑動),如:
<div id="fullpages">
<div class="section">第一屏</div>
<div class="section">第二屏</div>
<div class="section">
<div class="slide">第三屏的第一屏</div>
<div class="slide">第三屏的第二屏</div>
<div class="slide">第三屏的第三屏</div>
<div class="slide">第三屏的第四屏</div>
</div>
<div class="section">第四屏</div>
</div>
3、JavaScript
$(function(){
$('#fullpages').fullpage();
});
可以進(jìn)行跟多的配置:
$(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,
//Custom selectors
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){}
});
});
三.動手寫全屏滾動
這里主要介紹監(jiān)聽mousewheel事件及滾動。
由于mousewheel事件的兼容性,引用jquery-mousewheel插件來監(jiān)聽滾輪事件。
通過參數(shù)delta可以獲取鼠標(biāo)滾輪的方向和速度(舊版本需要傳delta參數(shù),新版本不需要,直接用event?。?。如果delta的值是負(fù)的,那么滾輪就是向下滾動,正的就是向上。
// using on
$('#my_elem').on('mousewheel', function(event) {
console.log(event.deltaX, event.deltaY, event.deltaFactor);
});
// using the event helper
$('#my_elem').mousewheel(function(event) {
console.log(event.deltaX, event.deltaY, event.deltaFactor);
});
可以根據(jù)需求使用fullpages實現(xiàn)全屏滾動(上下,左右),也可以使用jquery-mousewheel定制不同高度的全屏滾動。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。
相關(guān)文章
jQuery Validation Plugin驗證插件手動驗證
jquery.validate是jquery旗下的一個驗證框架,借助jquery的優(yōu)勢,我們可以迅速驗證一些常見的輸入,并且可以自己擴充自己的驗證方法,并且對國際化也有很好的支持,接下來通過本文給大家介紹jQuery Validation Plugin驗證插件手動驗證2016-01-01
Jquery與Bootstrap實現(xiàn)后臺管理頁面增刪改查功能示例
本篇文章主要介紹了Jquery與Bootstrap實現(xiàn)后臺管理頁面增刪改查功能示例,具有一定的參考價值,有興趣的可以了解一下。2017-01-01
jquery點擊實現(xiàn)升序降序圖標(biāo)切換
這篇文章主要為大家詳細(xì)介紹了jquery點擊實現(xiàn)升序降序圖標(biāo)切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
jquery.Jcrop結(jié)合JAVA后臺實現(xiàn)圖片裁剪上傳實例
這篇文章主要介紹了jquery.Jcrop結(jié)合JAVA后臺實現(xiàn)圖片裁剪上傳實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11
jQuery使用siblings獲取某元素所有同輩(兄弟姐妹)元素用法示例
這篇文章主要介紹了jQuery使用siblings獲取某元素所有同輩(兄弟姐妹)元素用法,結(jié)合簡單實例形式分析了siblings()函數(shù)獲取同輩元素的操作技巧,需要的朋友可以參考下2017-01-01

