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

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

1、效果预览


网络异常,图片无法展示
|


2、用到的知识


主要利用SVGstroke-dasharraystroke-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>



相关文章
|
24天前
sd.js 2.0封装:更加简化请求传参内容(逐步废弃、逐渐日落2024.01.02)
sd.js 2.0封装:更加简化请求传参内容(逐步废弃、逐渐日落2024.01.02)
|
25天前
|
存储 开发框架 JavaScript
uniapp、vue、小程序、js图片转base64 示例代码
uniapp、vue、小程序、js图片转base64 示例代码
40 0
|
28天前
|
缓存 JavaScript 前端开发
vue如何优化首页加载速度
vue如何优化首页加载速度
26 7
|
4天前
|
SQL 存储 前端开发
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情
React&Nest.js全栈社区平台(五)——👋封装通用分页Service实现文章流与详情
|
8天前
|
存储 JavaScript
报错permission.js:41 [Vue warn]: Property “showClose“ must be accessed with “$data.showClose“ because
报错permission.js:41 [Vue warn]: Property “showClose“ must be accessed with “$data.showClose“ because
|
10天前
|
JavaScript
Vue与原生JS中方法调用
Vue与原生JS中方法调用
5 0
|
14天前
|
JavaScript 前端开发 UED
Vue工具和生态系统: Vue.js和服务器端渲染(SSR)有关系吗?请解释。
Vue.js是一个渐进式JavaScript框架,常用于开发单页面应用,但其首屏加载较慢影响用户体验和SEO。为解决此问题,Vue.js支持服务器端渲染(SSR),在服务器预生成HTML,加快首屏速度。Vue.js的SSR可手动实现或借助如Nuxt.js的第三方库简化流程。Nuxt.js是基于Vue.js的服务器端渲染框架,整合核心库并提供额外功能,帮助构建高效的应用,改善用户体验。
15 0
|
14天前
|
JavaScript 前端开发 开发者
Vue工具和生态系统: Vue.js和TypeScript可以一起使用吗?
【4月更文挑战第18天】Vue.js与TypeScript兼容,官方文档支持在Vue项目中集成TypeScript。TypeScript作为JavaScript超集,提供静态类型检查和面向对象编程,增强代码准确性和健壮性。使用TypeScript能提前发现潜在错误,提升代码可读性,支持接口和泛型,使数据结构和函数更灵活。然而,不是所有Vue插件都兼容TypeScript,可能需额外配置。推荐尝试在Vue项目中使用TypeScript以提升项目质量。
14 0
|
14天前
|
资源调度 JavaScript 前端开发
Vue工具和生态系统: 如何使用Vue.js实现服务端渲染(SSR)?不少于500字
Vue.js框架用于构建用户界面,而服务端渲染(SSR)能提升首屏加载速度和SEO。以下是使用Vue.js实现SSR的简要步骤:1) 安装vue、vue-server-renderer和express依赖;2) 创建Vue应用如`vue create my-ssr-app`;3) 使用express创建服务器;4) 在Express应用中设定路由处理所有请求;5) 创建渲染器将Vue应用转为HTML;6) 运行服务器如`node my-ssr-app/server.js`。实际应用可能涉及动态内容、状态管理和错误处理等复杂情况。
18 1
|
20天前
|
JavaScript 搜索推荐 测试技术
深入了解 Vue CLI:现代化 Vue.js 项目开发工具
深入了解 Vue CLI:现代化 Vue.js 项目开发工具