拖放事件

This commit is contained in:
2023-10-13 01:07:36 +08:00
parent 2112870a8b
commit 065df5cc9e
1 changed files with 30 additions and 5 deletions

View File

@ -1,4 +1,4 @@
export function createElement({ innerText, textContent, onclick, onchange, onkeydown, onmouseenter, onmouseleave, readOnly, children = [], dataset, style, classList = [], ...attributes }, tagName = 'div') { export function createElement({ innerText, textContent, onclick, onchange, ondrop, ondragover, ondragleave, onkeydown, onmouseenter, onmouseleave, readOnly, children = [], dataset, style, classList = [], ...attributes }, tagName = 'div') {
const element = document.createElement(tagName) const element = document.createElement(tagName)
for (const key in attributes) element.setAttribute(key, attributes[key]) for (const key in attributes) element.setAttribute(key, attributes[key])
if (style) Object.assign(element.style, style) if (style) Object.assign(element.style, style)
@ -13,6 +13,9 @@ export function createElement({ innerText, textContent, onclick, onchange, onkey
if (children) children.forEach(child => element.appendChild(child)) if (children) children.forEach(child => element.appendChild(child))
if (onmouseenter) element.onmouseenter = onmouseenter if (onmouseenter) element.onmouseenter = onmouseenter
if (onmouseleave) element.onmouseleave = onmouseleave if (onmouseleave) element.onmouseleave = onmouseleave
if (ondrop) element.ondrop = ondrop
if (ondragover) element.ondragover = ondragover
if (ondragleave) element.ondragleave = ondragleave
return element return element
} }
@ -43,7 +46,7 @@ export function Avatar(options) {
} }
export function UploadMusic(options) { export function UploadMusic(options) {
const text = createElement({ const drop = createElement({
textContent: '点击或拖拽音乐到此处共享您的音乐', textContent: '点击或拖拽音乐到此处共享您的音乐',
style: { style: {
width: '100%', width: '100%',
@ -87,10 +90,32 @@ export function UploadMusic(options) {
input.click() input.click()
}, },
onmouseenter: event => { onmouseenter: event => {
text.style.display = 'block' drop.style.display = 'block'
}, },
onmouseleave: event => { onmouseleave: event => {
text.style.display = 'none' drop.style.display = 'none'
},
// 如果拖拽到了子元素上, 也要触发父元素的事件
ondragover: event => {
console.log('dragover')
event.preventDefault()
event.stopPropagation()
event.dataTransfer.dropEffect = 'copy'
drop.style.display = 'block'
},
ondrop: event => {
console.log('drop')
event.preventDefault()
event.stopPropagation()
const files = Array.from(event.dataTransfer.files)
if (files.length === 0) return console.log('没有文件')
console.log('files', files)
options.onchange(files)
},
ondragleave: event => {
event.preventDefault()
event.stopPropagation()
drop.style.display = 'none'
}, },
children: [ children: [
// 绘制一个云朵上传图标(别放弃...还有我呢!) // 绘制一个云朵上传图标(别放弃...还有我呢!)
@ -141,7 +166,7 @@ export function UploadMusic(options) {
}) })
] ]
}), }),
text drop
//createElement({ //createElement({
// style: { // style: {
// position: 'absolute', // position: 'absolute',