【sgAudioDialog】自定义组件:基于audio标签和el-dialog构建弹窗预览音频文件

sgAudioDialog源码

html 复制代码
<template>
  <div :class="$options.name" v-if="visible">
    <!-- 如果不加v-if="visible"弹窗中使用el-tabs组件就会死循环卡死,这个是elementUI的bug -->
    <el-dialog
      :append-to-body="true"
      :close-on-click-modal="true"
      :close-on-press-escape="true"
      :custom-class="`${$options.name}-el-dialog`"
      :destroy-on-close="true"
      :fullscreen="false"
      :show-close="true"
      :title="`${form.name || `音频`}`"
      :top="`calc(100vh / 2 - 70px)`"
      :width="`500px`"
      :visible.sync="visible"
      @keyup.ctrl.enter.native="save"
      style="animation: none"
      v-loading="loading"
      :element-loading-text="elementLoadingText"
    >
      <div class="audio-container">
        <!-- 弹窗内容 -->
        <audio
          :autoplay="typeof form.autoplay === 'undefined' ? true : form.autoplay"
          class="audio"
          controls
          :loop="form.loop"
          :muted="form.muted"
          preload
          ref="audio"
          :src="form.src"
          webkit-playsinline="true"
        />
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "sgAudioDialog",
  components: {},
  data() {
    return {
      loading: false, //加载状态
      elementLoadingText: ``, //加载提示文字

      visible: false,
      form: {}, //表单信息
      disabled: false, //是否只读
      labelWidth: `120px`,
    };
  },
  props: ["value", "data"],
  computed: {},
  watch: {
    loading(newValue, oldValue) {
      newValue || (this.elementLoadingText = ``);
    },
    value: {
      handler(d) {
        this.visible = d;
      },
      deep: true,
      immediate: true,
    },
    visible(d) {
      this.$emit("input", d);
      // 每次显示的时候,重置一些参数值
      if (d) {
      }
    },
    data: {
      handler(newValue, oldValue) {
        //console.log(`深度监听${this.$options.name}:`, newValue, oldValue);
        if (Object.keys(newValue || {}).length) {
          this.form = JSON.parse(JSON.stringify(newValue));
          this.disabled = this.form.disabled;
        } else {
          this.disabled = false; //添加的时候,编辑态
          this.form = {
            // 默认字段名: 缺省值,
          };
        }
      },
      deep: true, //深度监听
      immediate: true, //立即执行
    },
  },
  created() {},
  mounted() {},
  destroyed() {},
  methods: {
    valid(d) {
      if (!this.form.NAME) return this.$message.error(this.$refs.NAME.$attrs.placeholder);
    },
    save() {
      if (this.valid()) return;
      if (this.form.ID) {
        // 修改
      } else {
        // 添加
      }
      let data = {
        ...this.form,
        sgLog: `前端请求来源:${this.$options.name}添加或修改`,
      };
      this.$f.保存数据接口调用方法(
        {
          ...data,
          cb: (d) => {
            this.$emit(`save`, this.form);
            this.cancel();
          },
        },
        this
      );
    },
    cancel() {
      this.visible = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.sgAudioDialog {
}

.el-dialog__wrapper {
  /*遮罩模糊*/
  backdrop-filter: blur(5px);
}
>>> .sgAudioDialog-el-dialog {
  border-radius: 30px;
  box-shadow: 0 10px 50px 0 #00000033;
  //关闭按钮
  .el-dialog__headerbtn {
    .el-dialog__close {
      background-color: #409eff;
      border-radius: 88px;
      box-sizing: border-box;
      padding: 5px;
      color: white;
    }
    &:hover {
      .el-dialog__close {
        background-color: #1f75d5;
        color: white;
      }
    }
  }
  .audio-container {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: -20px 0px -10px;
    border-radius: 20px;
    overflow: hidden;
    height: calc(100vh - 180px);
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 60px;
    overflow: hidden;
    .audio {
      width: 100%;
      height: 100%;
    }
  }
}
</style>

应用

html 复制代码
<sgAudioDialog :data="data_sgAudioDialog" v-model="show_sgAudioDialog" />
 
data_sgAudioDialog: {src:`音频路径`, name:`音频标题`,},
show_sgAudioDialog: false,
相关推荐
ღ_23331 小时前
vue3二次封装element-plus表格,slot透传,动态slot。
前端·javascript·vue.js
xiaohe06011 小时前
🔒 JavaScript 不是单线程吗?怎么还能上“锁”?!
javascript·github
摸着石头过河的石头1 小时前
JavaScript继承的多种实现方式详解
前端·javascript
Ashley的成长之路1 小时前
NativeScript-Vue 开发指南:直接使用 Vue构建原生移动应用
前端·javascript·vue.js
朕的剑还未配妥1 小时前
Vue 2 响应式系统常见问题与解决方案(包含_demo以下划线开头命名的变量导致响应式丢失问题)
前端·vue.js
JNU freshman2 小时前
Element Plus组件
前端·vue.js·vue3
软件技术NINI2 小时前
MATLAB疑难诊疗:从调试到优化的全攻略
javascript·css·python·html
知识分享小能手3 小时前
uni-app 入门学习教程,从入门到精通,uni-app组件 —— 知识点详解与实战案例(4)
前端·javascript·学习·微信小程序·小程序·前端框架·uni-app
长空任鸟飞_阿康3 小时前
在 Vue 3.5 中优雅地集成 wangEditor,并定制“AI 工具”下拉菜单(总结/润色/翻译)
前端·vue.js·人工智能
苏打水com3 小时前
从 HTML/CSS/JS 到 React:前端进阶的平滑过渡指南
前端·javascript·html