资讯专栏INFORMATION COLUMN

原生 js 实现一个有动画效果的进度条插件 progress

用户84 / 384人阅读

摘要:效果图项目地址效果体验地址原理一个用于装载进度条内容的且叫做。然后在里面动态生成三个元素,一个是做为背景的且叫做,一个是做为显示进度的且叫做,还有一个是显示文字的且叫做。的宽从逐渐增加到的值的过程比如,给这个过程添加一个逐渐加快的动画。

效果图:

项目地址:https://github.com/biaochenxuying/progress

效果体验地址: https://biaochenxuying.github.io/progress/index.html

1. 原理

一个用于装载进度条内容的 div (且叫做 container)。

然后在 container 里面动态生成三个元素,一个是做为背景的 div (且叫做 progress),一个是做为显示进度的 div (且叫做 bar),还有一个是显示文字的 span (且叫做 text)。

progress 的宽为 100%,bar 的宽根据传入数值 target 的值来定( 默认为 0 ,全部占满的值为 100 ),text 展示的文字为 bar 的宽占 progress 宽的百分比。

bar 的宽从 0 逐渐增加到的 target 值的过程( 比如: 0 > 80 ),给这个过程添加一个逐渐加快的动画。

2. 代码实现

具体的过程请看代码:

/*
* author: https://github.com/biaochenxuying
*/

(function() {
  function Progress() {
    this.mountedId = null;
    this.target = 100;
    this.step = 1;
    this.color = "#333";
    this.fontSize = "18px";
    this.borderRadius = 0;
    this.backgroundColor = "#eee";
    this.barBackgroundColor = "#26a2ff";
  }

  Progress.prototype = {
    init: function(config) {
      if (!config.mountedId) {
        alert("请输入挂载节点的 id");
        return;
      }

      this.mountedId = config.mountedId;
      this.target = config.target || this.target;
      this.step = config.step || this.step;
      this.fontSize = config.fontSize || this.fontSize;
      this.color = config.color || this.color;
      this.borderRadius = config.borderRadius || this.borderRadius;
      this.backgroundColor = config.backgroundColor || this.backgroundColor;
      this.barBackgroundColor =
        config.barBackgroundColor || this.barBackgroundColor;

      var box = document.querySelector(this.mountedId);
      var width = box.offsetWidth;
      var height = box.offsetHeight;
      var progress = document.createElement("div");
      progress.style.position = "absolute";
      progress.style.height = height + "px";
      progress.style.width = width + "px";
      progress.style.borderRadius = this.borderRadius;
      progress.style.backgroundColor = this.backgroundColor;

      var bar = document.createElement("div");
      bar.style.float = "left";
      bar.style.height = "100%";
      bar.style.width = "0";
      bar.style.lineHeight = height + "px";
      bar.style.textAlign = "center";
      bar.style.borderRadius = this.borderRadius;
      bar.style.backgroundColor = this.barBackgroundColor;

      var text = document.createElement("span");
      text.style.position = "absolute";
      text.style.top = "0";
      text.style.left = "0";
      text.style.height = height + "px";
      text.style.lineHeight = height + "px";
      text.style.fontSize = this.fontSize;
      text.style.color = this.color;

      progress.appendChild(bar);
      progress.appendChild(text);
      box.appendChild(progress);

      this.run(progress, bar, text, this.target, this.step);
    },
    /**
     * @name 执行动画
     * @param progress 底部的 dom 对象
     * @param bar 占比的 dom 对象
     * @param text 文字的 dom 对象
     * @param target 目标值( Number )
     * @param step 动画步长( Number )
     */
    run: function(progress, bar, text, target, step) {
      var self = this;
      ++step;
      var endRate = parseInt(target) - parseInt(bar.style.width);
      if (endRate <= step) {
        step = endRate;
      }
      var width = parseInt(bar.style.width);
      var endWidth = width + step + "%";
      bar.style.width = endWidth;
      text.innerHTML = endWidth;

      if (width >= 94) {
        text.style.left = "94%";
      } else {
        text.style.left = width + 1 + "%";
      }

      if (width === target) {
        clearTimeout(timeout);
        return;
      }
      var timeout = setTimeout(function() {
        self.run(progress, bar, text, target, step);
      }, 30);
    },
  };

  // 注册到 window 全局
  window.Progress = Progress;
})();
3. 使用方法
  var config = {
    mountedId: "#bar",
    target: 8,
    step: 1,
    color: "green",
    fontSize: "20px",
    borderRadius: "5px",
    backgroundColor: "#eee",
    barBackgroundColor: "red",
  };
  var p = new Progress();
  p.init(config);
4. 最后

笔者的博客后花圆地址如下:

github :https://github.com/biaochenxuying/blog
个人网站 :http://biaochenxuying.cn/main.html

如果您觉得这篇文章不错或者对你有所帮助,请给个赞呗,你的点赞就是对我最大的鼓励,谢谢。

微信公众号:BiaoChenXuYing
分享 前端、后端开发等相关的技术文章,热点资源,随想随感,全栈程序员的成长之路。
关注公众号并回复 福利 便免费送你视频资源,绝对干货。
福利详情请点击: 免费资源分享--Python、Java、Linux、Go、node、vue、react、javaScript

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

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

相关文章

  • 手把手教你实现一个 Vue 进度组件!

    摘要:添加的属性进度条长度显示和隐藏是否是成功的状态有了这些属性,这个进度条就要根据这些属性的变化来自己动。大家可以自己动手实践一下,起一个项目,使用的声明周期钩子,或者写一个定时器模拟异步来测试一下。 showImg(https://segmentfault.com/img/bVbjDzm?w=900&h=383);最近在个人的项目中,想对页面之间跳转的过程进行优化,想到了很多文档或 np...

    zgbgx 评论0 收藏0
  • javascript功能插件大集合 前端常用插件 js常用插件

    摘要:转载来源包管理器管理着库,并提供读取和打包它们的工具。能构建更好应用的客户端包管理器。一个整合和的最佳思想,使开发者能快速方便地组织和编写前端代码的下一代包管理器。很棒的组件集合。隐秘地使用和用户数据。 转载来源:https://github.com/jobbole/aw... 包管理器管理着 javascript 库,并提供读取和打包它们的工具。•npm – npm 是 javasc...

    netmou 评论0 收藏0
  • javascript功能插件大集合 前端常用插件 js常用插件

    摘要:转载来源包管理器管理着库,并提供读取和打包它们的工具。能构建更好应用的客户端包管理器。一个整合和的最佳思想,使开发者能快速方便地组织和编写前端代码的下一代包管理器。很棒的组件集合。隐秘地使用和用户数据。 转载来源:https://github.com/jobbole/aw... 包管理器管理着 javascript 库,并提供读取和打包它们的工具。•npm – npm 是 javasc...

    Hydrogen 评论0 收藏0
  • javascript功能插件大集合,写前端亲们记得收藏

    摘要:一个专注于浏览器端和兼容的包管理器。一个整合和的最佳思想,使开发者能快速方便地组织和编写前端代码的下一代包管理器。完全插件化的工具,能在中识别和记录模式。健壮的优雅且功能丰富的模板引擎。完整的经过充分测试和记录数据结构的库。 【导读】:GitHub 上有一个 Awesome – XXX 系列的资源整理。awesome-javascript 是 sorrycc 发起维护的 JS 资源列表...

    cfanr 评论0 收藏0

发表评论

0条评论

用户84

|高级讲师

TA的文章

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