zoukankan      html  css  js  c++  java
  • 移动端常见问题(动画演示)

    移动端动画

     红色勾勾代表强烈推荐

    transition实现动画案例:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>移动端动画</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
        <style>
            *{padding:0;margin:0;}
            .box{width:100px;height: 100px;background-color: pink;transition:transform 1s;}
        </style>
    </head>
    <body>
        <button id="btn">start</button>
        <div class="box" id="box"></div>
    
        <script>
            var btn=document.getElementById("btn"),
                box=document.getElementById("box"),
                dest=window.innerWidth-100;//移动的距离
    
                btn.addEventListener("click",function(){
                    box.style.transform="translate3d("+dest+"px,0,0)";
                },false);    
        </script>
    </body>
    </html>

     也可以提取成函数的写法:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>移动端动画</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
        <style>
            *{padding:0;margin:0;}
            .box{width:100px;height: 100px;background-color: pink;transition:transform 1s;}
        </style>
    </head>
    <body>
        <button id="btn">start</button>
        <div class="box" id="box"></div>
    
        <script>
            var btn=document.getElementById("btn"),
                box=document.getElementById("box"),
                dest=window.innerWidth-100;//移动的距离
    
                btn.addEventListener("click",function(){
                    move(box,dest);
                },false);    
    
                function move(el,pos){
                    el.style.transform="translate3d("+pos+"px,0,0)";
                }
        </script>
    </body>
    </html>

    animation动画推荐一个animation库,animation.js  https://daneden.github.io/animate.css/

    可以查看各种动画的样式:

    一般情况下推荐使用css3的transition和animation来完成动画,如果不能满足需求,可以考虑js的requestAnimationFrame

    不做css动画时,记得一定要去掉transition属性

    requestAnimationFrame的特点是:调用一次只执行一帧;如果想要持续执行,就需要递归。
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>移动端动画</title>
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
        <style>
            *{padding:0;margin:0;}
            .box{width:100px;height: 100px;background-color: pink;}
        </style>
    </head>
    <body>
        <button id="btn">start</button>
        <div class="box" id="box"></div>
    
        <script>
            //requestAnimationFrame的兼容性处理    
            var requestAnimationFrame=window.requestAnimationFrame||
            window.webkitRequestAnimationFrame||
            window.mozRequestAnimationFrame||
            window.msRequestAnimationFrame||
            window.oRequestAnimationFrame||
            function(fn){
                setTimeout(fn,16);
            }
    
            var btn=document.getElementById("btn"),
                box=document.getElementById("box"),
                dest=window.innerWidth-100,//移动的距离
                speed=1,
                pos=0;
    
            btn.addEventListener("click",function(){
                requestAnimationFrame(step);
            },false);    
    
            function move(el,pos){
                el.style.transform="translate3d("+pos+"px,0,0)";
            }
    
            function step(){
                if(pos<dest){
                    //递归
                    pos+=speed;
                    move(box,pos);
                    requestAnimationFrame(step);
                }else{
                    pos=dest;
                    move(box,pos);
                }
            }
        </script>
    </body>
    </html>

  • 相关阅读:
    .NET开发者必备的工具箱
    LINQ标准查询操作符详解(转)
    转)SSO单点登录在互联网电商应用中的解决方案(基于CAS的改造)
    构建高并发高可用的电商平台架构实践(转)
    使用UI Automation实现自动化测试 --微软提供的控件Pattern
    使用UI Automation实现自动化测试 --工具使用
    [archlinux][daily] 自建DNS服务器 / 建立本地DNS cache / 使用dnsmasq加速上网
    [skill] 补码
    nfs的时间问题,影响编译
    [daily][CentOS][SELinux]用key免登陆不成功,原来是SElinux在搞事情
  • 原文地址:https://www.cnblogs.com/chenyingying0/p/12510059.html
Copyright © 2011-2022 走看看