假设$不是浏览器。现在要实现$,它会接受一个字符串,这是一个查询,它会使用querySelector来选择元素。(参考文献:https:/developer.mozilla.com) https:/developer.mozilla.orgen-USdocsWebAPIDocumentquerySelector)。)
$('text')
现在实现类似jquery的函数addClass和removeClass。(参考文献:https:/developer.mozilla.orgen-USdocsWebAPIElementclassList。https:/developer.mozilla.orgen-USdocsWebAPIElementclassList。)
$('#test').removeClass('blue').addClass('red');
现在实现jquery一样的函数延迟。(参考资料:https:/api.jquery.comdelay) https:/api.jquery.comdelay)在这里我被卡住了,无法实现这种情况下的延迟。
$('#test').removeClass('blue').delay(2000).delay(1000).addClass('red');
代码示例
function $(selector) {
let element = document.querySelector(selector)
Object.prototype.addClass = function (className) {
this.classList.add(className)
return this
}
Object.prototype.removeClass = function (className) {
this.classList.remove(className)
return this
}
Object.prototype.delay = function(ms){
// what to do?
return this
}
return element
}
$('#test').removeClass('blue').delay(2000).delay(1000).addClass('red');
<!DOCTYPE html>
<html>
<head>
<style>
.blue{
background-color: blue;
}
.red{
background-color: red;
}
</style>
</head>
<body>
<div id="test" style="width: 200px; height: 200px;" class="blue"></div>
</body>
</html>
我试着用队列和任务运行器来实现这个问题,有谁能检查一下这是否看起来不错,或者我们可以改进它吗?
function $(selector) {
let element = document.querySelector(selector)
element.queue = []
element.active = false
return element
}
Element.prototype.next = function () {
if (this.queue.length) this.runTask(this.queue.shift())
}
Element.prototype.runTask = function (callBack) {
this.active = true
callBack().then(() => {
this.active = false
this.next()
})
}
Element.prototype.register = function(callBack){
if (this.active) {
this.queue.push(callBack)
} else {
this.runTask(callBack)
}
}
Element.prototype.addClass = function (className) {
that = this
let callBack = () => new Promise(resolve => setTimeout(function () {
that.classList.add(className)
resolve()
}, 0))
this.register(callBack)
return this
}
Element.prototype.removeClass = function (className) {
that = this
let callBack = () => new Promise(resolve => setTimeout(function () {
that.classList.remove(className)
resolve()
}, 0))
this.register(callBack)
return this
}
Element.prototype.delay = function (ms) {
that = this
let callBack = () => new Promise(resolve => setTimeout(function () {
resolve()
}, ms))
this.register(callBack)
return this
}
$('#test')
.removeClass("red").delay(500)
.addClass("blue").delay(500).delay(500).removeClass("blue")
.delay(500).addClass("red").delay(500).removeClass("red")
.delay(500).addClass("blue").delay(500).removeClass("blue")
.delay(500).addClass("red").delay(500).removeClass("red")
<!DOCTYPE html>
<html>
<head>
<style>
.blue{
background-color: blue;
}
.red{
background-color: red;
}
</style>
</head>
<body>
<div id="test" style="width: 150px; height: 150px; margin:10px;" class="red"></div>
</body>
</html>
Object.prototype.delay
不容易实现,因为你必须引入一些像jQuery内部使用的队列。. 这是因为你想在以下情况下执行一些进一步的逻辑。callback
在 setTimeout(callback, ms)
被调用。
就像您的例子.delay(2000).delay(1000).addClass('red');