首页 > 其他分享 >uniapp项目实践总结(八)自定义加载组件

uniapp项目实践总结(八)自定义加载组件

时间:2023-09-03 18:22:38浏览次数:32  
标签:uniapp loading 自定义 default height width type 加载

有时候一个页面请求接口需要加载很长时间,这时候就需要一个加载页面来告知用户内容正在请求加载中,下面就写一个简单的自定义加载组件。

目录

  • 准备工作
  • 逻辑思路
  • 实战演练
  • 效果预览

准备工作

在之前的全局组件目录components下新建一个组件文件夹,命名为q-loading,组件为q-loading.vue

再找几个效果不错的 css 加载动画,然后修改一下样式。

逻辑思路

编写模板部分

要求具有扩展性,因此可以使用slot插槽来插入内容,也方便后期修改自定义。

使用classstyle绑定一些父组件传过来的值,更加个性化。

这个页面分为图标和文本提示两部分,各自可以自定义显示、大小、颜色。

编写样式部分

这部分就是图标和文本的样式以及一些加载动画的内容。

编写脚本部分

这部分主要是父组件传递过来的参数,通过props进行制定格式。

实战演练

下面就简单实现一个加载组件。

模板部分

<view
  class="q-loading"
  :style="{'backgroundColor': props.bgColor}"
  v-if="props.show"
>
  <view class="q-loading-inner">
    <slot name="load">
      <!-- 图标部分 -->
      <view
        :class="{'q-loading-icon': true, 'pause': !props.show && !props.showIcon}"
        v-if="props.showIcon"
      >
        <slot name="icon">
          <!-- 圆环 -->
          <view
            class="q-loading-item q-loading-circle"
            :style="{'width': props.borSize +'rpx', 'height': props.borSize +'rpx', 'borderWidth': props.borWin + 'rpx', 'borderColor': props.borColor, 'borderLeftColor': props.bordActiveColor}"
            v-if="props.iconName == 'circle'"
          >
          </view>
          <!-- 呼吸 -->
          <view
            class="q-loading-item q-loading-circle-breath"
            v-if="props.iconName == 'circle-breath'"
          >
          </view>
          <!-- 旋转 -->
          <view
            class="q-loading-item q-loading-circle-round"
            v-if="props.iconName == 'circle-round'"
          >
            <view
              class="loading-round"
              :style="{'backgroundColor': props.bordActiveColor}"
            ></view>
            <view
              class="loading-round"
              :style="{'backgroundColor': props.bordActiveColor}"
            ></view>
            <view
              class="loading-round"
              :style="{'backgroundColor': props.bordActiveColor}"
            ></view>
            <view
              class="loading-round"
              :style="{'backgroundColor': props.bordActiveColor}"
            ></view>
            <view
              class="loading-round"
              :style="{'backgroundColor': props.bordActiveColor}"
            ></view>
            <view
              class="loading-round"
              :style="{'backgroundColor': props.bordActiveColor}"
            ></view>
            <view
              class="loading-round"
              :style="{'backgroundColor': props.bordActiveColor}"
            ></view>
          </view>
        </slot>
      </view>
      <!-- 提示文本 -->
      <view
        class="q-loading-text"
        v-if="props.showText"
        :style="{'color': props.textColor, 'fontSize': props.textSize + 'rpx'}"
      >
        <slot name="text"> {{ props.text }} </slot>
      </view>
    </slot>
  </view>
</view>

样式部分

这部分就是页面的样式以及三个对应的动画。

  • 页面的样式
.q-loading {
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  box-sizing: border-box;
  padding: 10rpx;
  width: 100%;
  height: 100vh;
  .q-loading-inner {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .q-loading-icon {
      display: flex;
      justify-content: center;
      align-items: center;
      margin-bottom: 10rpx;
      width: 100rpx;
      height: 100rpx;
      .q-loading-circle {
        border-radius: 50%;
        border-style: solid;
        animation: loadingCircle 1s linear infinite;
      }
      .q-loading-circle-breath {
        box-shadow: 0 0 0 0 rgb(204, 73, 152);
        height: 36px;
        width: 36px;
        border-radius: 50%;
        animation: loadingCircleBreath 1s linear infinite;
      }
      .q-loading-circle-round {
        position: relative;
        width: 75rpx;
        height: 75rpx;
        .loading-round {
          position: absolute;
          width: 26rpx;
          height: 26rpx;
          border-radius: 50%;
          animation: loadingCircleRound 3s ease infinite;
          transform-origin: 120% 80rpx;
          &.loading-round:nth-child(1) {
            z-index: 7;
          }
          &.loading-round:nth-child(2) {
            height: 12px;
            width: 12px;
            animation-delay: 0.2s;
            z-index: 6;
          }
          &.loading-round:nth-child(3) {
            height: 11px;
            width: 11px;
            animation-delay: 0.4s;
            z-index: 5;
          }
          &.loading-round:nth-child(4) {
            height: 10px;
            width: 10px;
            animation-delay: 0.6s;
            z-index: 4;
          }
          &.loading-round:nth-child(5) {
            height: 9px;
            width: 9px;
            animation-delay: 0.8s;
            z-index: 3;
          }
          &.loading-round:nth-child(6) {
            height: 8px;
            width: 8px;
            animation-delay: 1s;
            z-index: 2;
          }
          &.loading-round:nth-child(7) {
            height: 7px;
            width: 7px;
            animation-delay: 1.2s;
            z-index: 1;
          }
        }
      }
      &.pause {
        .q-loading-item {
          animation-play-state: paused;
        }
      }
    }
  }
}
  • 三个对应的动画
// 圆环
@keyframes loadingCircle {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}

// 呼吸
@keyframes loadingCircleBreath {
  0% {
    transform: scale(0.3);
    box-shadow: 0 0 0 0 rgba(36, 175, 214, 60%);
  }

  60% {
    transform: scale(0.5);
    box-shadow: 0 0 0 56rpx rgba(36, 175, 214, 0%);
  }

  100% {
    transform: scale(0.3);
    box-shadow: 0 0 0 0 rgba(36, 175, 214, 0%);
  }
}

// 转动
@keyframes loadingCircleRound {
  to {
    transform: rotate(1turn);
  }
}

脚本部分

这部分就是传递的数据,包括组件、图标和文本的显示控制,以及各自的颜色,大小等参数。

const props = defineProps({
  // 显示加载
  show: {
    type: Boolean,
    default: true,
  },
  // 背景色
  bgColor: {
    type: String,
    default: "#fff",
  },
  // 显示图标
  showIcon: {
    type: Boolean,
    default: true,
  },
  // 名称
  iconName: {
    type: String,
    default: "circle",
  },
  // 大小
  borSize: {
    type: Number,
    default: 60,
  },
  // 边框粗细
  borWin: {
    type: Number,
    default: 8,
  },
  // 颜色
  borColor: {
    type: String,
    default: "#e3e3e3",
  },
  // 活动颜色
  bordActiveColor: {
    type: String,
    default: "#24afd6",
  },
  // 显示文本
  showText: {
    type: Boolean,
    default: true,
  },
  // 文本内容
  text: {
    type: String,
    default: "加载中...",
  },
  // 文本颜色
  textColor: {
    type: String,
    default: "#555",
  },
  // 文本大小
  textSize: {
    type: Number,
    default: 20,
  },
});

效果预览

下面看一下预览效果吧。

圆环效果

在这里插入图片描述

呼吸效果

在这里插入图片描述

旋转效果

在这里插入图片描述

最后

以上就是自定义加载组件的主要内容,有不足之处,请多多指正。

标签:uniapp,loading,自定义,default,height,width,type,加载
From: https://www.cnblogs.com/guanqiweb/p/17675304.html

相关文章

  • asp.net restful ef core sqlite 自定义包的位置
    MagicVilla_VillaAPI/MagicVilla_VillaAPI.csproj<ProjectSdk="Microsoft.NET.Sdk.Web"><PropertyGroup><TargetFramework>net7.0</TargetFramework><Nullable>enable</Nullable><ImplicitUsings>e......
  • 手把手教你vue3-ts-uniapp-vite创建多端小程序-3 统一ui,uni-ui库
    uni-ui官网地址https://uniapp.dcloud.net.cn/component/uniui/quickstart.html1.安装sass、sass-loadernpminstallsass-Dnpminstallsass-loader-D2.安装uni-uinpminstall@dcloudio/uni-ui3.配置easycom。在pages.json中配置"easycom":{"autoscan&q......
  • vue自定义事件用法及$emit
    子组件<template><button@click="handle">自定义事件</button></template><script>exportdefault{data(){return{message:"我子组件"}},methods:{handle(){......
  • 手把手教你vue3-ts-uniapp-vite创建多端小程序-2 设置底部导航
    1.打开项目,打开pages.json,设置底部导航栏。注意pages中的path和tabBar中list中的path要一致{ "pages":[ { "path":"pages/index/index", "style":{ "navigationBarTitleText":"首页" } },{ "pa......
  • 自定义CUDA实现PyTorch算子的四种简单方法
    背景在探索新的深度学习算法的时候,我们可能会遇到PyTorch提供的算子不能满足需求的情况,这时候就需要自定义PyTorch算子,将我们的算法集成到PyTorch的工作流中。同时,为了提高运算效率,算子往往都需要使用CUDA实现。所幸,PyTorch及很多其他Python库都提供了简化这一过程的方法,完全不需......
  • javaee spring注解设置单例模式和懒加载模式
    @Lazy懒加载@Scope(scopeName=“prototype”)设置多例模式,不加默认单例模式@Lazy@Component@Scope(scopeName="prototype")publicclassDrink{@Value("橙汁")privateStringname;@Value("半糖")privateStringsugar;@Value(&quo......
  • uniapp项目实践总结(七)编写一个简单的应用页面
    之前自定义了顶部和底部导航栏,那接下来就写一个简单的二级页面,这个是出了导航页面之外经常用到的。目录结构模板样式方法示例结构一个普通页面的结构应该是如下所示:<!--html--><template><viewclass="list">列表内容</view></template><!--js--><scrip......
  • 多个fragment切换,而不重新加载数据的实现
     多个fragment切换,而不重新加载数据的实现1、在xml中添加一个framlayout<FrameLayoutandroid:id="@+id/framelayout"android:layout_width="match_parent"android:layout_weight="1"android:layout_height="0dp"......
  • dotnet SemanticKernel 入门 自定义变量和技能
    本文将告诉大家如何在SemanticKernel框架内定义自定义的变量和如何开发自定义的技能本文属于SemanticKernel入门系列博客,更多博客内容请参阅我的博客导航自定义变量是一个非常有用的技能,自定义变量可以让炼丹师和程序员进行并行工作。由炼丹师对AI模型进行训练,从而找到对......
  • uniapp和Flutter我还是选择了Flutter
    APP开发跨平台?uniapp?Flutter?这是一个很纠结的选择!对于uniapp和Flutter,不少人都在使用,给大家总结一下自己使用心得和感悟。uniapp:出自国产、开箱即用、上手简单。支持更多的平台,如果你要支持小程序的话,不用犹豫了就选择它吧。有前端基础可以更好的发挥。在国内社区比较活跃,持续更新(......