您现在的位置是:首页 > web开发 > CSS3实现整屏切换效果

CSS3实现整屏切换效果

web开发作者:dayu日期:9天前点击:5

每一个section就是一页内容。它的大小充满了屏幕(红色区域)。一个container由多个section构成,我们通过改变container的位置,来达到页面切换的效果。

container向下走。页面好像上移了,container向上走,页面就下移了。
html结构例如以下:

<!DOCTYPE html>
<html>
<head lang="ch">
    <meta charset="UTF-8">
    <!--适配移动端-->
    <meta name=”viewport” content="width=device-width, user-scalable=no, initial-scale=1.0">
    <title></title>
    <style>
        body, html{
            padding: 0;
            margin: 0;
        }

        body, html {
            height: 100%;
            /**隐藏滚动栏**/
            overflow: hidden;
        }

        #container, .section {
            height: 100%;
        }

        #section0 {
            background-color: #83af9b;
        }

        #section1 {
            background-color: #764d39;
        }

        #section2 {
            background-color: #ff9000;
        }

        #section3 {
            background-color: #380d31;
        }

    </style>
</head>
<body>
<div id="container">
    <div class="section" id="section0"></div>
    <div class="section" id="section1"></div>
    <div class="section" id="section2"></div>
    <div class="section" id="section3"></div>
</div>
</body>
</html>
<script src="http://code.jquery.com/jquery-latest.js"></script> <script> //当前页面索引 var curIndex = 0; var scrollFunc = function (e) { e = e || window.event; var t = 0; if (e.wheelDelta) {//IE/Opera/Chrome t = e.wheelDelta; if (t > 0 && curIndex > 0) { //上滚动 movePrev(); } else if (t < 0 && curIndex < sumCount - 1) { //下滚动 moveNext(); } } else if (e.detail) {//Firefox t = e.detail; if (t < 0 && curIndex > 0) { //上滚动 movePrev(); } else if (t > 0 && curIndex < sumCount - 1) { //下滚动 moveNext(); } } }; function moveNext(){ } function movePrev(){ } function init(){ /*注冊事件*/ if (document.addEventListener) { document.addEventListener(‘DOMMouseScroll‘, scrollFunc, false); }//W3C window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome } init(); </script>

为了防止在第一个页面用户上滚。最后一个页面用户下滚。所以用curIndex代表当前页面索引在滚动时作了监听。当然假设要使页面循环滚动。仅仅需改动条件限制就可以。

transform: tanslate3D(10px, 30px, 0);

改动后的js代码例如以下:

<script>
    //当前页面索引
    var curIndex = 0;
    //container元素
    var container = $("#container");
    //页面总数
    var sumCount = $(".section").length;
    //窗口元素
    var $window = $(window);
    //动画时间
    var duration = 500;

    var scrollFunc = function (e) {
        e = e || window.event;
        var t = 0;
        if (e.wheelDelta) {//IE/Opera/Chrome
            t = e.wheelDelta;
            if (t > 0 && curIndex > 0) {
                //上滚动
                movePrev();
            } else if (t < 0 && curIndex < sumCount - 1) {
                //下滚动
                moveNext();
            }
        } else if (e.detail) {//Firefox
            t = e.detail;
            if (t < 0 && curIndex > 0) {
                //上滚动
                movePrev();
            } else if (t > 0 && curIndex < sumCount - 1) {
                //下滚动
                moveNext();
            }
        }
    };

    function moveNext(){
        container.css("transform", "translate3D(0, -" + (++curIndex) * $window.height() + "px, 0)");
    }

    function movePrev(){
        container.css("transform", "translate3D(0, -" + (--curIndex) * $window.height() + "px, 0)");
    }

    function init(){
        /*注冊事件*/
        if (document.addEventListener) {
            document.addEventListener(‘DOMMouseScroll‘, scrollFunc, false);
        }//W3C
        window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome

        //设置动画
        container.css({
            "transition": "all 0.5s",
            "-moz-transition": "all 0.5s",
            "-webkit-transition": "all 0.5s"
        });
    }
</script>

为了防止页面在滚动的时候用户继续滚动打乱节奏,能够用时间来强制控制,即在滚动期间不同意调用moveNext和movePrev函数,终于代码例如以下:

<!DOCTYPE html>
<html>
<head lang="ch">
    <meta charset="UTF-8">
    <meta name=”viewport” content="width=device-width, user-scalable=no, initial-scale=1.0">
    <title></title>
    <style>
        body, html{
            padding: 0;
            margin: 0;
        }

        body, html {
            height: 100%;
            overflow: hidden;
        }

        #container, .section {
            height: 100%;
        }

        .section {
            background-color: #000;
            background-size: cover;
            background-position: 50% 50%;
        }

        #section0 {
            background-color: #83af9b;
        }

        #section1 {
            background-color: #764d39;
        }

        #section2 {
            background-color: #ff9000;
        }

        #section3 {
            background-color: #380d31;
        }

    </style>
</head>
<body>
<div id="container">
    <div class="section" id="section0"></div>
    <div class="section" id="section1"></div>
    <div class="section" id="section2"></div>
    <div class="section" id="section3"></div>
</div>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
    var curIndex = 0;
    var container = $("#container");
    var sumCount = $(".section").length;
    var $window = $(window);
    var duration = 500;
    //时间控制
    var aniTime = 0;

    var scrollFunc = function (e) {
        //假设动画还没运行完,则return
        if(new Date().getTime() < aniTime + duration){
            return;
        }
        e = e || window.event;
        var t = 0;
        if (e.wheelDelta) {//IE/Opera/Chrome
            t = e.wheelDelta;
            if (t > 0 && curIndex > 0) {
                //上滚动
                movePrev();
            } else if (t < 0 && curIndex < sumCount - 1) {
                //下滚动
                moveNext();
            }
        } else if (e.detail) {//Firefox
            t = e.detail;
            if (t < 0 && curIndex > 0) {
                //上滚动
                movePrev();
            } else if (t > 0 && curIndex < sumCount - 1) {
                //下滚动
                moveNext();
            }
        }
    };

    function moveNext(){
        //获取动画開始时的时间
        aniTime = new Date().getTime();
        container.css("transform", "translate3D(0, -" + (++curIndex) * $window.height() + "px, 0)");
    }

    function movePrev(){
        //获取动画開始时的时间
        aniTime = new Date().getTime();
        container.css("transform", "translate3D(0, -" + (--curIndex) * $window.height() + "px, 0)");
    }

    function init(){
        /*注冊事件*/
        if (document.addEventListener) {
            document.addEventListener(‘DOMMouseScroll‘, scrollFunc, false);
        }//W3C
        window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome

        container.css({
            "transition": "all 0.5s",
            "-moz-transition": "all 0.5s",
            "-webkit-transition": "all 0.5s"
        });
    }

    init();
</script>
</body>
</html>


CSS3实现整屏切换效果

原文地址:http://www.cnblogs.com/ljbguanli/p/7199157.html


下一篇       上一篇