From 5915c717a06661dd20fde016853a97291826dc1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A7=89?= Date: Wed, 1 Nov 2023 16:42:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B6=E8=B5=B7=E9=9F=B3=E4=B9=90=E5=88=97?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/music.js | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/src/music.js b/src/music.js index 3d97902..6b95aed 100644 --- a/src/music.js +++ b/src/music.js @@ -80,15 +80,30 @@ export default class MusicList { textContent: '', }) + const upload = UploadMusic({ + style: { height: '5rem', margin: '1rem 0' }, + onchange: files => { + for (const file of files) { + const id = 'music' + Date.now() + const { name, size, type } = file + const reader = new FileReader() + reader.onload = async event => { + const arrayBuffer = event.target.result + this.add({ id, name, size, type, arrayBuffer }) // 添加到列表(默认并不存储) + this.like({ id, name, size, type, arrayBuffer }) // 本地缓存的必要条件是喜欢 + } + reader.readAsArrayBuffer(file) + } + } + }) + // 收起到右上角, 音乐播放器基于浮窗定位, 不再占用页面空间 const element = createElement({ style: { position: 'fixed', top: '5rem', right: '1rem', backgroundColor: '#fff', padding: '.5rem', borderRadius: '1rem', cursor: 'pointer', - width: '20rem', Height: '70vh', - minWidth: '20rem', minHeight: '13rem', - maxWidth: '80vw', maxHeight: '80vh', + width: '20rem', Height: '70vh', minWidth: '20rem', maxWidth: '80vw', maxHeight: '80vh', overflow: 'hidden', boxShadow: '0 0 1rem #eee', display: 'flex', flexDirection: 'column', fontSize: '12px', userSelect: 'none', @@ -131,6 +146,18 @@ export default class MusicList { justifyContent: 'space-between', flexShrink: 0, marginBottom: '.5rem', }, + onclick: event => { + // 点击隐藏列表和播放器 + if (this.ul.style.display === 'none') { + this.ul.style.display = 'block' + this.audio.style.display = 'block' + upload.style.display = 'block' + } else { + this.ul.style.display = 'none' + this.audio.style.display = 'none' + upload.style.display = 'none' + } + }, children: [ this.封面, createElement({ @@ -182,22 +209,7 @@ export default class MusicList { }), this.audio, this.ul, - UploadMusic({ - style: { height: '5rem', margin: '1rem 0' }, - onchange: files => { - for (const file of files) { - const id = 'music' + Date.now() - const { name, size, type } = file - const reader = new FileReader() - reader.onload = async event => { - const arrayBuffer = event.target.result - this.add({ id, name, size, type, arrayBuffer }) // 添加到列表(默认并不存储) - this.like({ id, name, size, type, arrayBuffer }) // 本地缓存的必要条件是喜欢 - } - reader.readAsArrayBuffer(file) - } - } - }) + upload, ] }) document.body.appendChild(element)