环形加载进度条封装(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获取height和width的方法说明
Jan 06 Javascript
微信分享的标题、缩略图、连接及描述设置方法
Oct 14 Javascript
js 加密压缩出现bug解决方案
Nov 25 Javascript
浅谈javascript的调试
Jan 28 Javascript
react开发中如何使用require.ensure加载es6风格的组件
May 09 Javascript
vue2.0 子组件改变props值,并向父组件传值的方法
Mar 01 Javascript
解决淘宝cnpm 安装后cnpm不是内部或外部命令的问题
May 17 Javascript
利用JS实现一个同Excel表现的智能填充算法
Aug 13 Javascript
使用jquery Ajax实现上传附件功能
Oct 23 jQuery
你了解vue3.0响应式数据怎么实现吗
Jun 07 Javascript
VUE 实现element upload上传图片到阿里云
Aug 12 Javascript
vue-router 按需加载 component: () =&gt; import() 报错的解决
Sep 22 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 cookie 作用范围?不要在当前页面使用你的cookie
2009/03/24 PHP
PHP 模拟$_PUT实现代码
2010/03/15 PHP
php模块memcache和memcached区别分析
2011/06/14 PHP
php学习笔记 面向对象中[接口]与[多态性]的应用
2011/06/16 PHP
PHP实现数据四舍五入的方法小结【4种方法】
2019/03/27 PHP
推荐:极酷右键菜单
2006/11/29 Javascript
如何在标题栏显示框架内页面的标题
2007/02/03 Javascript
基于jQuery试卷自动排版系统
2010/07/18 Javascript
struts2+jquery+json实现异步加载数据(自写)
2013/06/24 Javascript
jQuery数据缓存用法分析
2015/02/20 Javascript
jQuery表单验证功能实例
2015/08/28 Javascript
jquery对所有input type=text的控件赋值实现方法
2016/12/02 Javascript
jQuery Validate插件自定义验证规则的方法
2016/12/27 Javascript
JS实现线性表的链式表示方法示例【经典数据结构】
2017/04/11 Javascript
vue中使用腾讯云Im的示例
2020/10/23 Javascript
vue-cli 3如何使用vue-bootstrap-datetimepicker日期插件
2021/02/20 Vue.js
详解Python中的strftime()方法的使用
2015/05/22 Python
python下调用pytesseract识别某网站验证码的实现方法
2016/06/06 Python
根据DataFrame某一列的值来选择具体的某一行方法
2018/07/03 Python
cProfile Python性能分析工具使用详解
2019/07/22 Python
Python尾递归优化实现代码及原理详解
2020/10/09 Python
python 带时区的日期格式化操作
2020/10/23 Python
HTML5 placeholder属性详解
2016/06/22 HTML / CSS
Blank NYC官网:夹克、牛仔裤等
2020/12/16 全球购物
法务专员岗位职责
2014/01/02 职场文书
入党积极分子介绍信
2014/01/17 职场文书
高一学生评语大全
2014/04/25 职场文书
活动总结报告怎么写
2014/07/03 职场文书
幸福中国演讲稿
2014/09/12 职场文书
师德师风个人整改措施
2014/10/27 职场文书
2014年就业工作总结
2014/11/26 职场文书
2014年电话客服工作总结
2014/12/09 职场文书
 Python 中 logging 模块使用详情
2022/03/03 Python
【海涛教你打DOTA】死灵飞龙第一视角解说
2022/04/01 DOTA
如何让你的Nginx支持分布式追踪详解
2022/07/07 Servers
nginx sticky实现基于cookie负载均衡示例详解
2022/12/24 Servers