vue中实现音乐图标点击播放/暂停的组件

vue子组件(css定位代码自行修改),在componets文件夹下新建bgmusic.vue文件:

<template>
    <div class="music">
        <img v-if="isSpin" :style="{'animation-play-state':animat}" @click="spinSwitch()" src="../assets/img/complate_music@2x.png" alt="音乐开关">
        <img v-else :style="{'animation-play-state':animat}" @click="spinSwitch()" src="../assets/img/complate_stop_music@2x.png" alt="音乐开关">
   </div>
</template>

<script>
export default {
  data () {
    return {
      animat: 'running',
      isSpin: true
    }
  },
  methods: {
    spinSwitch () {
      this.isSpin = !this.isSpin
      this.animat = this.animat === 'running' ? 'paused' : 'running'
    }
  }
}
</script>
<style scoped>
@keyframes spin{ 0% {transform:rotate(0deg);} 100% {transform:rotate(360deg);}}
.music img{
  z-index: 999;
  position: fixed;
  right:0.8rem;
  top: 3.8rem;
  width: 2.5rem;
  height: 2.5rem;
  animation:spin 4s infinite linear;
}
</style>


在同级目录中新建index.js文件,用于导出组件:

export { default as Bgmusic } from './bgmusic.vue'

父组件中使用

<template>
  <div class="home">
    <bg-music></bg-music>
  </div>
</template>

<script>
import { Bgmusic } from '../components'

export default {
  name: 'Home',
  components: {
    'bg-music': Bgmusic,
  }
}
</script>

效果图

在这里插入图片描述

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值