移除元素
标准移除
ts
import { Leafer, Rect } from 'leafer-ui'
const leafer = new Leafer({ view: window })
const rect = Rect.one({ fill: '#32cd79' }, 100, 100, 200, 200)
leafer.add(rect)
setTimeout(() => {
rect.remove() // 等同于 leafer.remove(rect)
}, 2000)
条件移除
同 find() 方法的参数一致,内部会先 find() 再批量移除。
ts
import { Leafer, Rect } from 'leafer-ui'
const leafer = new Leafer({ view: window })
const rect = Rect.one({ id: 'book', fill: '#32cd79' }, 100, 100, 200, 200)
const rect2 = Rect.one({ fill: 'blue' }, 300, 100, 200, 200)
leafer.addMany(rect, rect2)
setTimeout(() => {
// 移除 id 为 book 的元素
leafer.remove('#book') // 等同于 leafer.find('#book').forEach(item => item.remove())
}, 2000)
销毁移除
ts
import { Leafer, Rect } from 'leafer-ui'
const leafer = new Leafer({ view: window })
const rect = Rect.one({ fill: '#32cd79' }, 100, 100, 200, 200)
leafer.add(rect)
setTimeout(() => {
rect.destroy() // 等同于 rect.remove() => rect.destroy()
}, 2000)
清空元素
ts
import { Leafer, Rect } from 'leafer-ui'
const leafer = new Leafer({ view: window })
const rect = Rect.one({ fill: '#32cd79' }, 100, 100, 200, 200)
const rect2 = Rect.one({ fill: 'blue' }, 300, 100, 200, 200)
leafer.addMany(rect, rect2)
setTimeout(() => {
leafer.clear() // 清空并销毁所有子元素
}, 2000)
销毁应用
ts
import { Leafer, Rect } from 'leafer-ui'
const leafer = new Leafer({ view: window })
const rect = Rect.one({ fill: '#32cd79' }, 100, 100, 200, 200)
leafer.add(rect)
setTimeout(() => {
leafer.destroy() // 应用销毁默认为异步方式
}, 2000)
恭喜 🎉
你已完成基础知识的学习,接下来将学习几个重要的进阶知识。