资讯专栏INFORMATION COLUMN

可以加一些复杂样式的数字动画

huayeluoliuhen / 2910人阅读

摘要:效果图需引入的使用方法

效果图:

需引入的JS

;(function($, window, document, undefined) {
    var leoTextAnimate = function(eles, opts) {
        this.element = $(eles);
        this.string = $(eles).html();
        this.defaults = {
            speed: 1000,
            autorun: true,
            delay: 0,
            fixed: "",
            start: ""
        };
        this.options = $.extend({}, this.defaults, opts);
        this.height = $(eles).height();
    }
    leoTextAnimate.prototype = {
        init: function() {
            if (this.element.find(".TextAnimate").length <= 0) {
                var html = method.getHtml(this.options, this.string);
                this.element.html(html);
            }
            this.reset();
            if (this.options.autorun) {
                if (this.options.delay == 0) {
                    this.run();
                } else {
                    var $this = this;
                    setTimeout(function() {
                        $this.run();
                    },
                    this.options.delay);
                }
            }
        },
        reset: function() {
            var $this = this.element.find(".TextAnimate");
            $this.css({
                "overflow": "hidden",
                "display":"inline-block",
                "vertical-align":"top",
                "height": this.height
            }).find("span").css({
                "display": "inline-block",
                "vertical-align": "top",
                "position": "relative",
                "top": "0px",
                "transform": "translateY(0px)",
                "-ms-transform": "translateY(0px)",
                "-moz-transform": "translateY(0px)",
                "-webkit-transform": "translateY(0px)",
                "-o-transform": "translateY(0px)",
                "-ms-transition": "0s",
                "-moz-transition": "0s",
                "-webkit-transition": "0s",
                "-o-transition": "0s",
                "transition": "0s"
            }).find("i").css({
                "display": "block",
                "font-style": "normal",
                "height": this.height
            });
        },
        run: function() {
            var speed = this.options.speed;
            var height = this.height;
            this.reset();
            this.element.find("span").each(function() {
                var $this = $(this);
                var length = $this.find("i").index($this.find(".on"));
                var to = -length * height + "px";
                if (to != $this.css("top")) {
                    if (!window.applicationCache) {
                        $this.animate({
                            top: to
                        },
                        speed);
                    } else {
                        $this.css({
                            "transform": "translateY(" + to + ")",
                            "-ms-transform": "translateY(" + to + ")",
                            "-moz-transform": "translateY(" + to + ")",
                            "-webkit-transform": "translateY(" + to + ")",
                            "-o-transform": "translateY(" + to + ")",
                            "-ms-transition": speed / 1000 + "s",
                            "-moz-transition": speed / 1000 + "s",
                            "-webkit-transition": speed / 1000 + "s",
                            "-o-transition": speed / 1000 + "s",
                            "transition": speed / 1000 + "s"
                        });
                    }
                }
            });
        }
    }
    var method = {
        getNumber: function(options, string) {
            if(!this.inArr(options.fixed, string)) {
                var text = "";
                if (options.start !== "") {
                    text += "" + options.start + "";
                }
                for (var i = 0; i < 10; i++) {
                    text += "" + i + "";
                }
                return text + "";
            } else {
                return "" + string + "";
            }
        },
        getLowerCase: function(options, string, code) {
            if (!this.inArr(options.fixed, string)) {
                var text = "";
                if (options.start !== "") {
                    text += "" + options.start + "";
                }
                for (var i = 0; i < 26; i++) {
                    text += "" + String.fromCharCode(97 + i) + "";
                }
                return text + "";
            } else {
                return "" + string + "";
            }
        },
        getUpperCase: function(options, string, code) {
            if (!this.inArr(options.fixed, string)) {
                var text = "";
                if (options.start !== "") {
                    text += "" + options.start + "";
                }
                for (var i = 0; i < 26; i++) {
                    text += "" + String.fromCharCode(65 + i) + "";
                }
                return text + "";
            } else {
                return "" + string + "";
            }
        },
        getUnicode: function(options, string, code) {
            if (!this.inArr(options.fixed, string)) {
                var text = "";
                if (options.start !== "") {
                    text += "" + options.start + "";
                }
                for (var i = (code - this.getRand(2, 7)); i < (code + this.getRand(3, 10)); i++) {
                    text += "" + String.fromCharCode(i) + "";
                }
                return text + "";
            } else {
                return "" + string + "";
            }
        },
        getHtml: function(options, string) {
            var html = "
" for (var i = 0; i < string.length; i++) { var text = string.substr(i, 1); var code = text.charCodeAt(); if (code > 47 && code < 58) { html += this.getNumber(options, text); } else if (code > 64 && code < 91) { html += this.getUpperCase(options, text, code); } else if (code > 96 && code < 123) { html += this.getLowerCase(options, text, code); } else { html += this.getUnicode(options, text, code); } } return html + "
"; }, getRand: function(minnum, maxnum) { return Math.floor(minnum + Math.random() * (maxnum - minnum)); }, inArr: function(arr,str){ for(var i=0;i

使用方法:

$("#totalnum").html("2,423");
$("#totalnum").leoTextAnimate({
        fixed: [",", ":", "."],
        start: "0"
    });

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/106549.html

相关文章

  • JavaScript是如何工作: CSS 和 JS 动画底层原理及如何优化它们性能

    摘要:贝塞尔曲线贝塞尔曲线是应用于二维图形应用程序的数学曲线。通过调整控制点,贝塞尔曲线的形状会发生变化。让我们看看贝塞尔曲线的工作原理。贝塞尔曲线需要四个值,或者更准确地说它需要两对数字。每对描述立方贝塞尔曲线控制点的和坐标。 这是专门探索 JavaScript 及其所构建的组件的系列文章的第 13 篇。 如果你错过了前面的章节,可以在这里找到它们: JavaScript 是如何工作的:...

    darcrand 评论0 收藏0
  • 前端面试之路一(HTML+CSS面试整理)

    摘要:或表示红色,表示绿色,表示蓝色,也可取其他数值来指定颜色。针对多字节文字,中文句子也是单词允许在单词内换行。 一、HTML基础 html常见元素和理解 html常见元素分类 head区元素:(不会在页面上留下直接内容) meta title style link script base body区: div/selection/article/aside/header/f...

    hqman 评论0 收藏0
  • 前端面试之路一(HTML+CSS面试整理)

    摘要:或表示红色,表示绿色,表示蓝色,也可取其他数值来指定颜色。针对多字节文字,中文句子也是单词允许在单词内换行。 一、HTML基础 html常见元素和理解 html常见元素分类 head区元素:(不会在页面上留下直接内容) meta title style link script base body区: div/selection/article/aside/header/f...

    YacaToy 评论0 收藏0
  • JavaScript 编程精解 中文第三版 十四、文档对象模型

    摘要:在其沙箱中提供了将文本转换成文档对象模型的功能。浏览器使用与该形状对应的数据结构来表示文档。我们将这种表示方式称为文档对象模型,或简称。树回想一下第章中提到的语法树。语言的语法树有标识符值和应用节点。元素表示标签的节点用于确定文档结构。 来源:ApacheCN『JavaScript 编程精解 中文第三版』翻译项目原文:The Document Object Model 译者:飞龙 协议...

    gggggggbong 评论0 收藏0
  • 可能是最全前端动效库汇总

    摘要:非常的庞大,而且它是完全为设计而生的动效库。它运行于纯粹的之上,是目前最强健的动画资源库之一。可能是创建滚动特效最好用的工具,它支持大量的浏览器,只要它们支持和特性。可以通过安装吊炸天了,接近现实生活中的物理运动碰撞惯性动画库。 收集日期为2019-02-28,★代表当时的该项目在github的star数量 Animate.css 56401 ★ 一个跨浏览器的动效基础库,是许多基础动...

    afishhhhh 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<