zoukankan      html  css  js  c++  java
  • javascript 回到顶部 动画效果

    上代码:

    <!DOCTYPE html>
    <html>
    <head>
        <meta content="测试demo" name="Keywords">
        <meta content="测试demo" name="Description">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
        <title>js原生回到顶部</title>
    </head>
    <body style="100%;height:2000px">
        <div style="position: fixed;bottom: 50px;right: 10%;" id="$backtotop">按钮</div>
        <script>
            !(function () {
                var Animate = function (dom) {
                    this.startTime = 0;
                    this.startPos = 0;
                    this.endPos = 0;
                    this.duration = null;
                    if (!this.start) {
                        Animate.prototype.start = function (endPos, duration) {
                            this.startTime = +new Date;
                            this.startPos = document.body.scrollTop;
                            this.endPos = 0; 
                            this.duration = duration;
                            var self = this;
                            var timeId =
                                setInterval(function () {
                                    if (self.step() === false) {
                                        clearInterval(timeId);
                                    }
                                }, 19);
                        }; Animate.prototype.step = function () {
                            var t = +new Date;
                            if (t >= this.startTime + this.duration) {
                                this.update(this.endPos); return false;
                            }
                            var pos = function (t, b, c, d) {
                                return c * t / d + b;
                            };
                            this.update(pos(t - this.startTime,
                                this.startPos,
                                this.endPos - this.startPos,
                                this.duration));
                        }; Animate.prototype.update = function (pos) {
                            document.body.scrollTop = pos;
                        };
                    };
                };
                document.getElementById("$backtotop").onclick = function () {
                    var animate = new Animate();
                    animate.start(0, 300);
                };
            })();
        </script>
    </body>
    </html>
  • 相关阅读:
    day 6 敌机
    day 11 绘制轮廓
    day 10 形态学处理 膨胀
    day 5 飞机发射子弹 难点??
    激活Navicat?如何注册Navicat?
    Gradle DSL method found: ‘android()’错误
    腾讯sdk配置
    Android模拟器报"Failed To Allocate memory 8"错误的解决办法
    文件上传工具swfupload[转]
    35个jquery技巧[转]
  • 原文地址:https://www.cnblogs.com/lmyt/p/6806187.html
Copyright © 2011-2022 走看看