• 原生js实现简单的全屏滚动


    <!DOCTYPE html>
    <html lang="en">

    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- <link href="./animate.css"> -->
    <style>
    html,
    body {
    height: 100%;
    margin: 0;
    padding: 0;
    }

    div {
    font-size: 30px;
    text-align: center;
    }

    body {
    overflow: hidden;
    }

    .fir {
    background-color: aliceblue;
    transition: all 2s;
    }

    .sec {
    background-color: antiquewhite;
    transition: all 2s;
    }

    .thr {
    background-color: aqua;
    transition: all 2s;
    }

    .fou {
    background-color: aquamarine;
    transition: all 2s;
    }

    .fiv {
    background-color: bisque;
    transition: all 2s;
    }

    .page .active {
    color: green;
    background-color: blueviolet;
    }

    .active .sec {
    background-color: red;
    }

    </style>
    </head>

    <body>
    <div id='wrap'>
    <div class="page fir">
    <div class="animated">page one</div>
    </div>
    <div class="page sec">
    <div>page two</div>
    </div>
    <div class="page thr">
    <div>page three</div>
    </div>
    <div class="page fou">
    <div>page four</div>
    </div>
    <div class="page fiv">
    <div>page five</div>
    </div>


    </div>

    <script>
    window.onload = function () {
    var wrap = document.getElementById('wrap');
    var page = document.getElementsByClassName('page');
    var pagelength = page.length;
    var index = 0;
    // wrap.style.height=window.innerHeight*pagelength+'px';
    for (var i = 0; i < pagelength; i++) {
    page[i].style.height = window.innerHeight + 'px';
    page[i].setAttribute('id', i)
    }
    document.body.scrollTop = 0;
    document.addEventListener('mousewheel', wheel)
    var istrue = true;

    function wheel(e) {
    if (istrue) {
    istrue = false;
    var st=document.body.scrollTop||document.documentElement.scrollTop;
    var dh=document.documentElement||document.body;
    if (e.wheelDelta > 0) {
    console.log(e.wheelDelta)

    var pageh = st - window.innerHeight;
    pageh = pageh < 0 ? 0 : pageh;

    index = pageh / window.innerHeight;

    //
    for (var i = 0; i < pagelength; i++) {
    page[i].classList.remove('active')
    }
    page[index].classList.add('active')

    animation(dh, {
    'scrollTop': pageh
    }, function () {
    istrue = true;
    })

    } else {
    console.log(st)

    var pageh = st + window.innerHeight;
    var maxh = wrap.offsetHeight - window.innerHeight;
    pageh = pageh > maxh ? maxh : pageh;

    index = pageh / window.innerHeight;
    for (var i = 0; i < pagelength; i++) {
    page[i].classList.remove('active')
    }
    page[index].classList.add('active')
    animation(dh, {
    'scrollTop': pageh
    }, function () {
    istrue = true;
    })

    }
    }
    }

    function animation(obj, json, fn) {
    clearInterval(obj.timer);
    //表示运动是否都已经停止
    var flag = true;
    obj.timer = setInterval(function () {
    //循环json
    for (var i in json) {
    if (i == 'opacity') {
    //获取透明度值,round四舍五入去除小数点
    var icur = Math.round(parseFloat(getStyle(obj, i)) * 100);
    } else {
    //获取属性值
    var icur = parseInt(getStyle(obj, i)) || obj[i];
    }
    //缓冲运动,speed随时变换
    var speed = (json[i] - icur) / 10; //千万要写在定时器里面,写在外面会有意想不到的后果
    speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed); //速度向上或者下取整,防止到不了over位置
    //如果有一个没到达终点就是false
    if (json[i] !== icur) {
    flag = false;
    } else {
    flag = true;
    }
    if (i == 'opacity') {
    obj.style.filter = 'alpha(opacity:' + (icur + speed) + ')'; //IE兼容
    obj.style.opacity = (icur + speed) / 100;
    } else if (obj[i] || obj[i] == 0) {
    obj[i] = icur + speed;
    } else {
    obj.style[i] = icur + speed + 'px';
    }
    // console.log(icur + ' ' + json[i]);
    }
    //检查是否所有的运动都已经停止
    if (flag) {
    clearInterval(obj.timer);
    if (fn) {
    fn();
    }
    }
    }, 13);
    }
    /**
    *函数作用:返回样式属性值
    *接受参数:obj(需要获取属性的DOM元素)
    * attr(需要获取的属性名称)
    */
    function getStyle(obj, attr) {
    if (obj.currentStyle) {
    return obj.currentStyle[attr]; //IE兼容
    } else {
    return getComputedStyle(obj, false)[attr];
    }
    }
    }

    </script>
    </body>

    </html>
  • 相关阅读:
    CSS 换行
    CSS div仿table样式
    CSS 选择器优先级
    ajax 请求前后处理
    JQuery 数组按指定长度分组
    JQuery .width()/.css("width")方法 比较
    JS 生成唯一值UUID
    JS/jQuery点击某元素之外触发事件
    JS/Jquery关系
    利用Nginxcp为cPanel/WHM服务器开启nginx支持
  • 原文地址:https://www.cnblogs.com/zzh965390267/p/8249749.html
Copyright © 2020-2023  润新知