好程序員技術分享jQuery實現類似fullpage插件的全屏滾動效果

好程序員技術分享jQuery實現類似fullpage插件的全屏滾動效果


結合網上的思路,加上我之前自己做的代碼,代碼有這幾種功能:

 

1.頭部和尾部的內容可以不用滾動,只要中間的滾動就行。

 

2.支持上一屏和下一屏的動畫觸發

 

3.支持下一頁和上一頁導航跳轉

 

4.支持小導航顯示和跳轉到某一頁

 

HTML代碼:

 

<div>header</div>

<div>

<div>

<div class="page page1">page1</div>

<div class="page page2">page2</div>

<div class="page page3">page3</div>

<div class="page page4">page4</div>

</div>

<div><i></i><i></i><i></i><i></i></div>

<a href="javascript:void(0)">下一頁</a>

</div>

<div>footer</div>

<script src="http://js.3conline.com/min/temp/v1/lib-jquery1.10.2.js"></script>

<script src="http://js.3conline.com/pcbaby/2017/nianping/pc/jquery-mousewheel.js"></script>

CSS設置時,需要注意有兩個父類元素,最外面的要設置超出高度隱藏。

 

.doc{height:640px;position:relative;overflow:hidden}

.main{position:relative}

.doc .page{position:absolute;height:100%;width:100%;top:100%}

.doc.done .page{position:static;top:0}

JS代碼,要注意滾動的兼容代碼用到了jQuery插件jquery-mousewheel,不要漏掉這個插件

JS:

 

$(function() {

    var onScroll = false,

    curIndex = 0,

    len = $(".doc .page").length;

    var winHeight = $(window).height();

    // var boxHeight = winHeight- 60 >640 ? winHeight - 60 : 640; //當需要顯示所有內容,需要給外層一個固定高度,保證所有內容都能看到

    var boxHeight = winHeight - 60;

    var toPage = function(curIndex) {

        onScroll = true;

        var now = -curIndex * boxHeight;

        $(".page").eq(curIndex).addClass("current").siblings(".page").removeClass("current");

        $(".page-nav i").eq(curIndex).addClass("current").siblings("i").removeClass("current");

        $(".main").animate({

            top: now + "px"

        },1000,function() {

            onScroll = false;

        });

    };

    $(".doc").css("height", boxHeight);

    $(".main").css("height", boxHeight * len);

    $(".page").css("height", boxHeight);

    $(".doc").addClass("done");

    var pageNext = function() {

        if (curIndex == len - 1) return;

        curIndex++;

        toPage(curIndex);

    }

    var pagePrev = function() {

        if (curIndex == 0) return;

        curIndex--;

        toPage(curIndex);

    }

    $(".doc").on("mousewheel",function(e, i) {

        if (onScroll) return;

        if (i < -.2) {

            //向下滾動

            pageNext();

        } else {

            //向上滾動

            pagePrev();

        }

    });

    $('.nextPage').on('click',function() {

        if (onScroll) return;

        pageNext();

    });

    $('.page-nav i').on('click',function() {

        if (onScroll) return;

        var index = $(this).index();

        toPage(index);

    })

});


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章