环形加载进度条封装(Vue插件版和原生js版)


Posted in Javascript onDecember 04, 2019

本文实例为大家分享了环形加载进度条封装代码,供大家参考,具体内容如下

1、效果预览

环形加载进度条封装(Vue插件版和原生js版)

2、用到的知识

主要利用SVG的stroke-dasharray和stroke-dashoffset这两个属性。
在看下面文章之前,你需要了解

<!DOCTYPE html>
<html>
<head>
 <title>svg demo</title>
 <style type="text/css">
 #line{
 transition: all 2s;
 stroke-dasharray:300,320;
 stroke-dashoffset:300;
 }
 svg:hover #line{
 stroke-dashoffset: 0;
 }
 
 #circle{
 transition: all 2s;
 stroke-dasharray:314,314;
 stroke-dashoffset:314;
 }
 svg:hover #circle{
 stroke-dashoffset:0;
 }
 </style>
</head>
<body>

<h3>线段区域</h3>
<svg width="100%" height="40" >
 <line id="line" x1="30" y1="30" x2="300" y2="30" stroke-width="20" stroke="red" />
</svg> 
<h3>圆区域</h3>

<svg width="200" height="200" viewBox="0 0 200 200">
 <circle id="circle" cx="100" cy="80" r="50" fill="gray" stroke-width="5" stroke="green" />
</svg>

</body>
</html>

3、使用

有两种方式,一种是直接安装即可使用,一种需要封装。选一种适合自己的即可。

(1)、安装插件

安装Vue插件

npm install loading-vue-component

使用

// main.js
import loading from 'loading-vue-component'
Vue.use(loading)
// app.vue
<template>
 <div id="app">
  <loading :radius="20" :progress="progress" :stroke="2" :color='color'></loading>
 </div>
</template>
 
<script>
export default {
 name: 'app',
 data() {
 return { progress: 0,color:'#1989fa'}
 }
}
</script>

(2)、封装插件

Vue版

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>loading</title>
 <style>
 html,
 body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  position: relative;
  margin: 0;
  padding: 0;
 }
 circle {
  transition: stroke-dashoffset 0.15s;
  transform: rotate(-90deg);
  transform-origin: 50% 50%;
 }
 .txt{
 font-size: 14px;
 text-align: center;
 }
 .loading{
  padding:40vh;
 }
 </style>
</head>

<body>
 <div id="example"></div>
</body>
<script src="https://unpkg.com/vue@2.6.10/dist/vue.js"></script>
<script>
// 子组件
const Loading = Vue.component('Loading', {
 props: {
  radius: Number,
  progress: Number,
  stroke: Number,
  color:String
 },
 data() {
  const progressed = this.progress;
  const colored = this.color
  const normalizedRadius = this.radius - this.stroke * 2; // 净半径,总半径-2*路径宽
  const circumference = normalizedRadius * 2 * Math.PI; // 周长,2πd
  return {
   normalizedRadius,
   circumference,
   progressed,
   colored
  };
 },
 mounted() {
  // emulating progress
  const interval = setInterval(() => {
   this.progressed += 25;
   if (this.progressed > 101) {
    this.colored='white'
   }
   this.colored='#1989fa'
  }, 150);
 },
 computed: {
  strokeDashoffset() {
   return this.circumference - this.progressed / 100 * this.circumference;
  }
 },
 template: `
 <div class='loading'>
 <svg
  :height="radius * 2"
  :width="radius * 2"
  >
  <circle
   :stroke="color"
   :stroke-dasharray="circumference + ' ' + circumference"
   :style="{ strokeDashoffset: strokeDashoffset }"
   :stroke-width="stroke"
   fill="transparent"
   :r="normalizedRadius"
   :cx="radius"
   :cy="radius"
  />
 </svg>
  <p class='txt'>加载中</p>
 </div>
 `
});
// 父组件
new Vue({
 el: '#example',
 components: {
  'Loading': Loading
 },
 data() {
  return { progress: 0,color:'#1989fa',show:true}
 },
 mounted () {
  setTimeout(() => {
  this.show = false
  },3000)
 },
 template: `
 <div>
  <Loading :radius="20" :progress="progress" :stroke="2" :color='color' v-show='show'></Loading>
 </div>
 `
});
</script>

</html>

原生js版

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>progress</title>
 <style>
  html, body {
 background-color: #333;
 display: flex;
 align-items: center;
 justify-content: center;
 height: 100%;
 position: relative;
}
.progress-ring__circle {
 transition: 0.35s stroke-dashoffset;
 transform: rotate(-90deg);
 transform-origin: 50% 50%;
}

input {
 position: fixed;
 top: 10px;
 left: 10px;
 width: 80px;
}
 </style>
</head>

<body>
 <svg class="progress-ring" width="120" height="120">
  <circle class="progress-ring__circle" stroke="white" stroke-width="4" fill="transparent" r="52" cx="60" cy="60" />
 </svg>
 <input value="35" type="number" step="5" min="0" max="100" placeholder="progress">
</body>
<script type="text/javascript">
var circle = document.querySelector('circle');
var radius = circle.r.baseVal.value;
var circumference = radius * 2 * Math.PI;

circle.style.strokeDasharray = `${circumference} ${circumference}`;
circle.style.strokeDashoffset = `${circumference}`;

function setProgress(percent) {
 const offset = circumference - percent / 100 * circumference;
 circle.style.strokeDashoffset = offset;
}

const input = document.querySelector('input');
setProgress(input.value);

input.addEventListener('change', function(e) {
 if (input.value < 101 && input.value > -1) {
  setProgress(input.value);
 }
})
</script>

</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
js实现浏览器的各种菜单命令比如打印、查看源文件等等
Oct 24 Javascript
jQuery学习笔记之toArray()
Jun 09 Javascript
jquery实现图片按比例缩放示例
Jul 01 Javascript
javascript模拟map输出与去除重复项的方法
Feb 09 Javascript
jQuery实现360°全景拖动展示
Mar 18 Javascript
JavaScript中的splice()方法使用详解
Jun 09 Javascript
Augularjs-起步详解
Jul 08 Javascript
原生javascript 学习之js变量全面了解
Jul 14 Javascript
AngularJS 实现弹性盒子布局的方法
Aug 30 Javascript
js模仿微信朋友圈计算时间显示几天/几小时/几分钟/几秒之前
Apr 27 Javascript
浅谈Node.js之异步流控制
Oct 25 Javascript
ES6中的迭代器、Generator函数及Generator函数的异步操作方法
May 12 Javascript
element-ui 远程搜索组件el-select在项目中组件化的实现代码
Dec 04 #Javascript
在vue和element-ui的table中实现分页复选功能
Dec 04 #Javascript
vue项目中极验验证的使用代码示例
Dec 03 #Javascript
jQuery轮播图功能制作方法详解
Dec 03 #jQuery
JS实现的雪花飘落特效示例
Dec 03 #Javascript
vue中实现高德定位功能
Dec 03 #Javascript
node静态服务器实现静态读取文件或文件夹
Dec 03 #Javascript
You might like
解析php中eclipse 用空格替换 tab键
2013/06/24 PHP
PHP的pcntl多进程用法实例
2015/03/19 PHP
PHP框架自动加载类文件原理详解
2017/06/06 PHP
tp5.1 框架数据库-数据集操作实例分析
2020/05/26 PHP
TopList标签和JavaScript结合两例
2007/08/12 Javascript
ExtJs3.0中Store添加 baseParams 的Bug
2010/03/10 Javascript
基于jQuery实现左右div自适应高度完全相同的代码
2012/08/09 Javascript
js的alert弹出框出现乱码解决方案
2013/09/02 Javascript
JavaScript改变HTML元素的样式改变CSS及元素属性
2013/11/12 Javascript
JavaScript希尔排序、快速排序、归并排序算法
2016/05/08 Javascript
sencha ext js 6 快速入门(必看)
2016/06/01 Javascript
关于angularJs指令的Scope(作用域)介绍
2016/10/25 Javascript
JSON字符串和JSON对象相互转化实例详解
2017/01/05 Javascript
vue.js中Vue-router 2.0基础实践教程
2017/05/08 Javascript
微信小程序调用PHP后台接口 解析纯html文本
2017/06/13 Javascript
JS查找数组中重复元素的方法详解
2017/06/14 Javascript
JS+canvas实现的五子棋游戏【人机大战版】
2017/07/19 Javascript
Nuxt升级2.0.0时出现的问题(小结)
2018/10/08 Javascript
JavaScript遍历DOM元素的常见方式示例
2019/02/16 Javascript
原生js实现trigger方法示例代码
2019/05/22 Javascript
Flutter实现仿微信底部菜单栏功能
2019/09/18 Javascript
17个Python小技巧分享
2015/01/23 Python
在java中如何定义一个抽象属性示例详解
2017/08/18 Python
python 通过麦克风录音 生成wav文件的方法
2019/01/09 Python
django的分页器Paginator 从django中导入类
2019/07/25 Python
python 利用turtle库绘制笑脸和哭脸的例子
2019/11/23 Python
HTML5本地存储之IndexedDB
2017/06/16 HTML / CSS
墨尔本复古时尚品牌:Dangerfield
2018/12/12 全球购物
解释一下ArrayList Vector和LinkedList的实现和区别
2013/04/26 面试题
成人大专自我鉴定范文
2013/10/19 职场文书
财产公证书格式
2014/04/10 职场文书
机电一体化专业求职信
2014/07/22 职场文书
安全检查汇报材料
2014/12/26 职场文书
如何撰写创业策划书
2019/06/27 职场文书
Mysql Online DDL的使用详解
2021/05/20 MySQL
微信小程序中使用vant框架的具体步骤
2022/02/18 Javascript