uni-app中實(shí)現(xiàn)元素拖動(dòng)效果
uni-app中實(shí)現(xiàn)元素拖動(dòng)
1、代碼示例
<template>
<movable-area class="music-layout">
<movable-view class="img-layout" :x="x" :y="y" direction="all">
<img :src="musicDetail.bgUrl" :class="[ isPlay ? 'rotate-img' : '']" @click="onImgClick">
<view class="small-circle"></view>
</movable-view>
</movable-area>
</template>
<script>
export default {
name: "music-icon",
props: {
musicDetail: {
type: Object,
default: {}
}
},
data() {
return {
innerAudioContext: {},
x: 300,
y: 500,
isPlay: true,
}
},
watch: {
musicDetail: {
handler(newVal, oldVal) {
if (newVal.music) {
this.handlePlay();
}
},
immediate: true
}
},
methods:{
handlePlay() {
this.innerAudioContext = uni.createInnerAudioContext();
this.innerAudioContext.src = this.musicDetail.music;
this.innerAudioContext.startTime = 0;
this.innerAudioContext.play();
this.innerAudioContext.loop = true; // 循環(huán)播放
},
onImgClick() {
this.isPlay = !this.isPlay;
if (this.isPlay) {
this.innerAudioContext.play();
} else {
this.innerAudioContext.pause();
}
}
}
}
</script>
<style scoped lang="scss">
.music-layout {
height: 100vh;
width: 750rpx;
top: 0;
position: fixed;
pointer-events: none; //鼠標(biāo)事件可以滲透
}
.img-layout {
position: relative;
width: 100rpx;
height: 100rpx;
border-radius: 50%;
overflow: hidden;
pointer-events: auto; //恢復(fù)鼠標(biāo)事件
img {
width: 100%;
height: 100%;
border-radius: 50%;
}
.small-circle{
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 20rpx;
height: 20rpx;
background-color: white;
border-radius: 50%;
}
}
.rotate-img {
width: 80rpx;
height: 80rpx;
border-radius: 50%;
transform-origin: center center;
animation: rotate 5s infinite linear;
}
@keyframes rotate {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
</style>利用的是uni-app中的movable-area和movable-view兩個(gè)組件配合實(shí)現(xiàn)
2、效果圖展示

到此這篇關(guān)于uni-app中實(shí)現(xiàn)元素拖動(dòng)的文章就介紹到這了,更多相關(guān)uni-app元素拖動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript this在函數(shù)中的指向及實(shí)例詳解
這篇文章主要介紹了JavaScript this在函數(shù)中的指向及實(shí)例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
JavaScript實(shí)現(xiàn)中秋博餅游戲的示例代碼
中秋博餅習(xí)俗源于福建廈門,盛行于漳州的龍海、泉州的安海和金門縣等地。博餅的游戲規(guī)則簡單公平,既充滿競爭懸念,又富于生活情趣,本文將用JavaScript實(shí)現(xiàn)這一經(jīng)典游戲,感興趣的可以了解一下2022-09-09
JavaScript正則表達(dá)式和級(jí)聯(lián)效果
正則表達(dá)式(regular expression)是一種字符串匹配的模式,用來檢查一個(gè)字符串中是否包含指定模式的字符串。下面通過本文給大家分享JavaScript_正則表達(dá)式和級(jí)聯(lián)效果,感興趣的朋友一起看看吧2017-09-09
前端實(shí)現(xiàn)Word在線預(yù)覽功能詳解
這篇文章主要給大家介紹了關(guān)于前端實(shí)現(xiàn)Word在線預(yù)覽功能的相關(guān)資料,工作中經(jīng)常有時(shí)會(huì)遇到需要給用戶創(chuàng)建word文檔并實(shí)現(xiàn)word文檔在線預(yù)覽的需求,需要的朋友可以參考下2023-09-09
JavaScript數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊(duì)列與循環(huán)隊(duì)列實(shí)例詳解
這篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊(duì)列與循環(huán)隊(duì)列,結(jié)合實(shí)例形式較為詳細(xì)的分析了javascrip數(shù)據(jù)結(jié)構(gòu)中優(yōu)先隊(duì)列與循環(huán)隊(duì)列的原理、定義與使用方法,需要的朋友可以參考下2017-10-10

