zoukankan      html  css  js  c++  java
  • JS运动

    JS运动 - 无缝滚动和缓动动画

    • 无缝滚动原理:首先先复制两张图片(第一张和第二张)放到最后面;ul绝对定位,如果ul的left值大于等于4张图片的宽度,就应该快速复原为0.

    html

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>无缝滚动</title>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            .box {
                 600px;
                height: 200px;
                background: red;
                margin: 100px auto;
                overflow: hidden;
                position: relative;
            }
            ul {
                list-style: none;
                /*font-size: 0;*/ /* 清除图片之间的间距之方法一 */
                 1000%;
                position: absolute;
                left: 0;
                top: 0;
            }
            li {
                float: left;
            }
            li img {
                vertical-align: bottom; /* 清除图片之间的间距之方法二 */
            }
        </style>
    </head>
    <body>
        <div class="box">
            <ul>
                <li><img src="images/scroll01.jpg" alt=""></li>
                <li><img src="images/scroll02.jpg" alt=""></li>
                <li><img src="images/scroll03.jpg" alt=""></li>
                <li><img src="images/scroll04.jpg" alt=""></li>
                <li><img src="images/scroll01.jpg" alt=""></li>
                <li><img src="images/scroll02.jpg" alt=""></li>
                <li><img src="images/scroll03.jpg" alt=""></li>
                <li><img src="images/scroll04.jpg" alt=""></li>
            </ul>
        </div>
    </body>
    </html>
    

    JS

    <script>
        window.onload = function () {
            var ul = document.getElementsByTagName("ul")[0];
            var offsetX = 0;
            var timer = null;
            timer = setInterval(scrollDidScroll,10);
            function scrollDidScroll() {
                offsetX--;
                offsetX = offsetX < -1200 ? 0 : offsetX;
                ul.style.left = offsetX + "px";
                console.log(offsetX);
            }
            ul.onmouseover = function () {
                clearInterval(timer);
            }
            ul.onmouseout = function () {
                timer = setInterval(scrollDidScroll,10);
            }
        }
    </script>
    

    效果图

    • 缓动动画(减速运动)原理 : 记住公式

    假如:初始值 leader = 0; 目标值 target = 400; speed = 10;公式如下
    leader = leader + (target - leader) / speed;

    HTML

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>滚动图</title>
        <style>
            * {
                padding: 0;
                margin: 0;
                font-size: 10px;
            }
            ul, ol {
                list-style: none;
            }
            .box {
                 490px;
                height: 170px;
                margin: 100px auto;
                position: relative;
                overflow: hidden;
            }
            .box ul {
                position: absolute;
                top: 0;
                left: 0;
                 800%;
                height: 100%;
            }
            .box ul li {
                 490px;
                float: left;
            }
            .box ol {
                position: absolute;
                bottom: 10px;
                left: 50%;
                margin-left: -40px;
                text-align: center;
            }
            .box ol li {
                 15px;
                height: 15px;
                line-height: 15px;
                float: left;
                background-color: #cccccc;
                color: #ffffff;
                margin-left: 10px;
                cursor: pointer;
            }
            .box .current {
                background-color: orange;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <ul id="scrollJS">
                <li><img src="0329images/01.jpg" alt=""></li>
                <li><img src="0329images/02.jpg" alt=""></li>
                <li><img src="0329images/03.jpg" alt=""></li>
                <li><img src="0329images/04.jpg" alt=""></li>
            </ul>
            <ol id="olJS">
                <li class="current">1</li>
                <li>2</li>
                <li>3</li>
                <li>4</li>
            </ol>
        </div>
    </body>
    </html>
    

    JS

    <script>
        window.onload = function () {
            var width = 490;
            var timer = null;
            var leader = 0;
            var scroll = document.getElementById("scrollJS");
            var lis = document.getElementById("olJS").children;
            for (var i=0;i<lis.length;i++) {
                lis[i].index = i;
                lis[i].onmouseover = function () {
                    clearInterval(timer);
                    for (var j=0;j<lis.length;j++) {
                        lis[j].className = "";
                    }
                    this.className = "current";
                    var target = -this.index * width;
                    timer = setInterval(function () {
    
                        leader = leader + (target - leader) / 10;
                        scroll.style.left = leader + "px";
                    },30);
                }
            }
        }
    </script>
    

    效果图

  • 相关阅读:
    SQOOP的安装配置_Linux伊甸园开源社区24小时滚动更新开源资讯,全年无休!
    Cloudera's Hadoop Demo VM for CDH4 Cloudera Support
    海量文档查同或聚类问题 Locality Sensitive Hash 算法
    part 1: resemblance with the jaccard coefficient
    计算机科学中最重要的32个算法zz
    详细的tfidf构建过程实例(转)
    2012 Beijing Google Dev FastDay(11/03/2012) 移动新观察
    百度技术沙龙
    Hive官方手册翻译(Getting Started) 实践检验真理 51CTO技术博客
    《周末休闲吧》:教你如何玩车震——车震全程攻略!_周末休闲吧_百度空间
  • 原文地址:https://www.cnblogs.com/gchlcc/p/6653893.html
Copyright © 2011-2022 走看看