全屏滾動-jQuery插件實現

jopen 8年前發布 | 19K 次閱讀 jQuery HTML jQuery插件

全屏滾動

<---很久沒寫了,這段忙了點,以后還是每周盡量寫點東西--->

在很多情況下,我們需要頁面的全屏滾動,尤其是移動端。今天簡要的介紹一下全屏滾動的知識。

一.全屏滾動的原理

1.js動態獲取屏幕的高度。

獲取屏幕的高度,設置每一屏幕的高度。

2.監聽mousewheel事件。

監聽mousewheel事件,并判斷滾輪的方向,向上或向下滾動一屏。

二.jQuery插件fullpages介紹

fullPage.js 是一個基于 jQuery 的插件,它能夠很方便、很輕松的制作出全屏網站,主要功能有:

支持鼠標滾動

支持前進后退和鍵盤控制

多個回調函數

支持手機、平板觸摸事件

支持 CSS3 動畫

支持窗口縮放

窗口縮放時自動調整

可設置滾動寬度、背景顏色、滾動速度、循環選項、回調、文本對齊方式等等

使用方法

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 代表一屏,默認顯示“第一屏”,如果要指定加載頁面時顯示的“屏幕”,可以在對應的 section 加上 class=”active”,如:

<div class="section active">第三屏</div>

同時,可以在 section 內加入 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();
});

可以進行跟多的配置:

$(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){}
    });
});

4、配置

1)選項

選項 類型 默認值 說明
verticalCentered 字符串 true 內容是否垂直居中
resize 布爾值 false 字體是否隨著窗口縮放而縮放
slidesColor 函數 設置背景顏色
anchors 數組 定義錨鏈接
scrollingSpeed 整數 700 滾動速度,單位為毫秒
easing 字符串 easeInQuart 滾動動畫方式
menu 布爾值 false 綁定菜單,設定的相關屬性與 anchors 的值對應后,菜單可以控制滾動
navigation 布爾值 false 是否顯示項目導航
navigationPosition 字符串 right 項目導航的位置,可選 left 或 right
navigationColor 字符串 #000 項目導航的顏色
navigationTooltips 數組 項目導航的 tip
slidesNavigation 布爾值 false 是否顯示左右滑塊的項目導航
slidesNavPosition 字符串 bottom 左右滑塊的項目導航的位置,可選 top 或 bottom
controlArrowColor 字符串 #fff 左右滑塊的箭頭的背景顏色
loopBottom 布爾值 false 滾動到最底部后是否滾回頂部
loopTop 布爾值 false 滾動到最頂部后是否滾底部
loopHorizontal 布爾值 true 左右滑塊是否循環滑動
autoScrolling 布爾值 true 是否使用插件的滾動方式,如果選擇 false,則會出現瀏覽器自帶的滾動條
scrollOverflow 布爾值 false 內容超過滿屏后是否顯示滾動條
css3 布爾值 false 是否使用 CSS3 transforms 滾動
paddingTop 字符串 0 與頂部的距離
paddingBottom 字符串 0 與底部距離
fixedElements 字符串
normalScrollElements
keyboardScrolling 布爾值 true 是否使用鍵盤方向鍵導航
touchSensitivity 整數 5
continuousVertical 布爾值 false 是否循環滾動,與 loopTop 及 loopBottom 不兼容
animateAnchor 布爾值 true
normalScrollElementTouchThreshold 整數 5

2)方法

名稱 說明
moveSectionUp() 向上滾動
moveSectionDown() 向下滾動
moveTo(section, slide) 滾動到
moveSlideRight() slide 向右滾動
moveSlideLeft() slide 向左滾動
setAutoScrolling() 設置頁面滾動方式,設置為 true 時自動滾動
setAllowScrolling() 添加或刪除鼠標滾輪/觸控板控制
setKeyboardScrolling() 添加或刪除鍵盤方向鍵控制
setScrollingSpeed() 定義以毫秒為單位的滾動速度

3)回調函數

名稱 說明
afterLoad 滾動到某一屏后的回調函數,接收 anchorLink 和 index 兩個參數,anchorLink 是錨鏈接的名稱,index 是序號,從1開始計算
onLeave

滾動前的回調函數,接收 index、nextIndex 和 direction 3個參數:index 是離開的“頁面”的序號,從1開始計算;

nextIndex 是滾動到的“頁面”的序號,從1開始計算;

direction 判斷往上滾動還是往下滾動,值是 up 或 down。

afterRender 頁面結構生成后的回調函數,或者說頁面初始化完成后的回調函數
afterSlideLoad 滾動到某一水平滑塊后的回調函數,與 afterLoad 類似,接收 anchorLink、index、slideIndex、direction 4個參數
onSlideLeave 某一水平滑塊滾動前的回調函數,與 onLeave 類似,接收 anchorLink、index、slideIndex、direction 4個參數

更多請查看官方文檔 https://github.com/alvarotrigo/fullPage.js/blob/master/README.md

示例: http://www.britishairways.com/en-gb/information/travel-classes/experience-our-cabins

三.動手寫全屏滾動

這里主要介紹監聽mousewheel事件及滾動。

由于mousewheel事件的兼容性,引用 jquery-mousewheel 插件來監聽滾輪事件。

通過參數delta可以獲取鼠標滾輪的方向和速度(舊版本需要傳delta參數,新版本不需要,直接用event取)。如果delta的值是負的,那么滾輪就是向下滾動,正的就是向上。

// 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);
});

總結:

可以根據需求使用fullpages實現全屏滾動(上下,左右),也可以使用jquery-mousewheel定制不同高度的全屏滾動。

-------------------------------------------------------------------------------------------------------------------------------------

來自: http://www.cnblogs.com/jingwhale/p/5079827.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!