> 效果如圖

>1. 制作component組件
在組件中通過play是否為true控制: 圖片是否加`rotate` 旋轉屬性,以及播放圖片的src
```
--music.wxml--
<view class="banner">
<image class="{{play?'rotate':''}}" src="/images/banner.png" />
<image class="play" src="{{play?'/images/play.png':'/images/pause.png'}}"
mode="widthFix" bindtap="onMusic"/>
</view>
```
```
--rotate動畫--
.rotate {
animation: rotate 3s linear infinite;
//linear線性平滑過渡
}
@keyframes rotate {
0% { transform: rotate(0);}
100% { transform: rotate(360deg);}
}
```
>2. 注冊并使用組件
先在index.json中注冊組件
```
"usingComponents":{
"v-music": "/components/music/music"
}
```
再在index.wxml中使用并通過屬性傳遞數據
```
<v-music title="{{music.title}}" url="{{music.url}}"></v-music>
```
>3. 組件接受所需數據
```
--music.js--
properties: {
title: String,
url: String
}
```
>4. 實現點擊功能
```
const audio = wx.getBackgroundAudioManager();
methods: {
onPlay(){
if(this.data.isPlay){
audio.pause();
this.setData({
isPlay: !this.data.isPlay
})
}else{
audio.title = this.properties.mTitle;
audio.src = this.properties.mUrl;
this.setData({
isPlay: !this.data.isPlay
});
}
}
},
attached(){
/* 音樂播放 */
audio.onPlay(()=>{
this.setData({
play: true
});
});
/* 暫停 */
audio.onPause(()=>{
this.setData({
play: false
});
});
/* 停止 */
audio.onStop(()=>{
this.setData({
play: false
});
});
/* 播放完 */
audio.onEnded(()=>{
this.setData({
play: false
});
});
}
```
- 小程序配置
- 1 開始第一個小程序
- 2 navigationBar
- 3 flex彈性布局
- 4 響應式長度單位: rpx
- 5 添加新的頁面
- 6 配置tabBar
- 7 歡迎頁跳轉到有tabBar的頁面
- 小程序語法
- 1. 數據綁定
- 2. 列表渲染
- 3. 條件渲染
- 4. 小程序和vue data讀取方式
- 5. 屬性的數據綁定方式
- 6. bindtap與catchtap
- 7. event.targe和event.currentTarget
- 組件&demo
- 1. scroll-view
- 2. swiper
- 3. 制作一個音樂播放組件
- 4. chooseImage配合緩存創建頭像
- 5. 獲取input表單value(搜索欄實現)
- 6. map
- 7. Form表單提交獲取數據
- 小程序API
- 1. 緩存 wx.setStorageSync
- 2. 選擇圖片 wx.chooseImage
- 3. 加載 wx.showLoading
- 4. 彈出框 wx.showToast
- 5. 分享與獲取用戶信息
- 項目結構類
- 1. 代碼封裝
- 2. wx.request請求數據分離
- 3. 組件
- 1. slot
- 2. 父元素傳遞class到子元素
- 3. 子組件向父組件傳值
- 4. wxml中引用wxs封裝方法