我發現了一個名爲Clippy.js的整潔的小型JS庫,可讓您在瀏覽器中實現Microsoft Word的舊虛擬助手。在玩了一段時間之後,我意識到文本氣球有一個setTimeout()
方法和一個時間延遲導致它消失。如何使用Clippy.js單擊而不是超時隱藏元素?
hide:function (fast) {
if (fast) {
this._balloon.hide();
return;
}
this._hiding = window.setTimeout($.proxy(this._finishHideBalloon, this), this.CLOSE_BALLOON_DELAY);
},
_finishHideBalloon:function() {
if (this._active) return;
this._balloon.hide();
this._hidden = true;
this._hiding = null;
},
我不想那樣。我希望氣球在用戶點擊時消失。我試圖用這個代替this._hiding = ...
註冊一個事件偵聽器:
var clickToHide = document.getElementsByClassName('clippy-balloon');
this._hiding = clickToHide.addEventListener('click', function(){$.proxy(this._finishHideBalloon, this)});
......但這一切確實是完全隱藏氣球。爲什麼這不起作用?我如何實現我想要的功能?
基於你在這裏的代碼,我認爲'this'在你定義的點擊函數內部與'this._hiding'中的'this'有不同的上下文;嘗試設置'var'到'this'並在click功能中使用'that' ...可以解決問題 –
像這樣,對吧? 'this._hiding = clickToHide.addEventListener('click',function(){ var that = this._hiding; $ .proxy(that._finishHideBalloon,that) });' – sabaeus
似乎不起作用。 – sabaeus