转换字节数

This commit is contained in:
2023-10-01 03:20:16 +08:00
parent ff75509353
commit cbbf007da1
2 changed files with 11 additions and 3 deletions

View File

@ -17,7 +17,6 @@ export default class MusicList {
//this.audio.addEventListener('timeupdate', () => { //this.audio.addEventListener('timeupdate', () => {
// console.log(this.audio.currentTime) // console.log(this.audio.currentTime)
//}) //})
// 本地添加音乐按钮 // 本地添加音乐按钮
const input = document.createElement('input') const input = document.createElement('input')
input.type = 'file' input.type = 'file'
@ -73,10 +72,18 @@ export default class MusicList {
this.EventListeners[name] = callback this.EventListeners[name] = callback
} }
add(item) { add(item) {
// 将字节转换为可读的单位
const bytesToSize = bytes => {
if (bytes === 0) return '0 B'
const k = 1024
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return (bytes / Math.pow(k, i)).toFixed(2) + ' ' + sizes[i]
}
this.list.push(item) this.list.push(item)
this.ul.appendChild(ListItem({ this.ul.appendChild(ListItem({
id: item.id, id: item.id,
innerText: `${item.name} - ${item.size} - ${item.type} - ${item.id}`, innerText: `${item.name} - ${bytesToSize(item.size)}`,
children: [ children: [
Button({ Button({
innerText: '播放', innerText: '播放',

View File

@ -11,11 +11,12 @@ export function List({ children = [] }) {
return ul return ul
} }
export function ListItem({ innerText, onclick, id, children = [] }) { export function ListItem({ innerText, onclick, id, children = [], dataset }) {
const li = document.createElement('li') const li = document.createElement('li')
li.innerText = innerText li.innerText = innerText
li.onclick = onclick li.onclick = onclick
li.id = id li.id = id
dataset && Object.keys(dataset).forEach(key => li.dataset[key] = dataset[key])
children.forEach(child => li.appendChild(child)) children.forEach(child => li.appendChild(child))
return li return li
} }