| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <template>
- <view class="main" id="bgMusic">
- <view class="icon" :class="{ ran: status }" @click="musicChange">
- <image :src="$util.img('upload/uniapp/audio-icon-new.png')" mode="widthFix" :style="icon_style"></image>
- </view>
- <view class="musicPlay" @touchstart.stop="playTo" v-if="musicPlay"></view>
- </view>
- </template>
- <script>
- export default {
- props: {
- sizi: {
- default: '0.4',
- type: [Number, String]
- },
- autoplay: {
- default: true,
- type: Boolean
- },
- musicSrc: {
- default: '',
- type: String
- }
- },
- data() {
- return {
- icon_style: {},
- audioMusic: {},
- status: false,
- musicPlay: true
- };
- },
- beforeDestroy() {
- this.pauseTo();
- },
- watch: {
- '$route.path'(newVal, oldVal) {
- if (newVal != '/promotion/pintuanfission/detail') {
- this.pauseTo();
- }
- }
- },
- mounted() {
- this.icon_style = {
- width: this.sizi + 'rem'
- // height: this.sizi + 'rem'
- };
- this.$nextTick().then(res => {
- const audioMusic = uni.createInnerAudioContext();
- audioMusic.loop = true;
- audioMusic.onPlay(() => {
- this.status = true;
- this.musicPlay = false;
- });
- audioMusic.onPause(() => {
- this.status = false;
- });
- audioMusic.onError(err => {
- this.musicPlay = false;
- console.log(err);
- });
- audioMusic.src = this.musicSrc;
- if (this.autoplay) audioMusic.autoplay = true;
- this.audioMusic = audioMusic;
- if (!this.autoplay) return;
- setTimeout(() => {
- if (typeof WeixinJSBridge == 'undefined') {
- this.playTo();
- } else {
- WeixinJSBridge.invoke('getNetworkType', {}, e => {
- this.playTo();
- });
- }
- }, 500);
- });
- },
- methods: {
- playTo() {
- this.audioMusic.play();
- },
- pauseTo() {
- this.audioMusic.pause();
- },
- musicChange() {
- const status = this.status;
- if (status) {
- this.audioMusic.pause();
- } else {
- this.audioMusic.play();
- }
- }
- },
- destroyed() {}
- };
- </script>
- <style lang="scss" scoped>
- .icon {
- position: fixed;
- top: 0.6rem;
- right: 0.2rem;
- z-index: 999;
- image {
- width: 0.4rem;
- height: 0.4rem;
- }
- }
- .ran {
- image {
- animation: turn 1s linear infinite;
- }
- }
- @keyframes turn {
- 0% {
- -webkit-transform: rotate(0deg);
- }
- 25% {
- -webkit-transform: rotate(90deg);
- }
- 50% {
- -webkit-transform: rotate(180deg);
- }
- 75% {
- -webkit-transform: rotate(270deg);
- }
- 100% {
- -webkit-transform: rotate(360deg);
- }
- }
- .musicPlay {
- position: fixed;
- top: 0;
- bottom: 0;
- right: 0;
- left: 0;
- background-color: rgba($color: #000000, $alpha: 0);
- z-index: 9999;
- }
- </style>
|