簡體   English   中英

調用原型函數-不起作用

[英]Calling a prototype function - not working

嗨,我有以下代碼控制圖片庫的位置(可以在steven.tlvweb.com上看到)。 滾輪當前控制着畫廊的位置,但鍵盤按下事件卻沒有,我希望它們能夠。 可以看到下面代碼(keyLeft和keyRight)中的警報,但是根本沒有調用this.parent.scroll

參數sc只需要是一個正整數或負整數-畢竟是什么event.wheelDelta,所以我不禁要問,調用此原型函數的正確方法是什么?

/* //////////// ==== ImageFlow Constructor ==== //////////// */


function ImageFlow(oCont, xmlfile, horizon, size, zoom, border, start, interval) {
    this.oc = document.getElementById(oCont); 
this.scrollbar  = getElementsByClass(this.oc,   'div', 'scrollbar');
this.text       = getElementsByClass(this.oc,   'div', 'text');
this.bar        = getElementsByClass(this.oc,   'img', 'bar');
this.arL        = getElementsByClass(this.oc,   'img', 'arrow-left');
this.arR        = getElementsByClass(this.oc,   'img', 'arrow-right');
    this.bar.parent = this.oc.parent = this; 
    this.arL.parent = this.arR.parent = this;

    /* === handle mouse scroll wheel === */
    this.oc.onmousewheel = function () {
        this.parent.scroll(event.wheelDelta);
        return false;
    }

    var pleasework = this;

/* ==== add keydown events ==== */
    window.document.onkeydown=function(){  
        pleasework.keypress(event.keyCode);
        return false;
    }

}
/* //////////// ==== ImageFlow prototype ==== //////////// */
ImageFlow.prototype = {

scroll: function (sc) {
        if (sc < 0) {
            if (this.view < this.NF - 1) this.calc(1);
        } else {
            if (this.view > 0) this.calc(-1);
        }
    },


keypress : function (kp) {

    switch (kp) {
        case 39:
            //right Key
            if (this.view < this.NF - 1) this.calc(1);
            break;
        case 37:
            //left Key
            if (this.view > 0) this.calc(-1);
            break;
    }

    },

}

預先感謝Steven(Java新手)

不要在DOM元素上使用該parent屬性。 相反,只需在局部變量上創建一個閉包即可。 因此,更換

this.bar.parent = this.oc.parent = this; 
this.arL.parent = this.arR.parent = this;

/* === handle mouse scroll wheel === */
this.oc.onmousewheel = function () {
    this.parent.scroll(event.wheelDelta);
    return false;
}

/* ==== add keydown events ==== */
window.document.onkeydown=function(){  
    this.parent.keypress(event.keyCode);
    return false;
}

var parent = this;
this.oc.onmousewheel = function(e) {
    parent.scroll(e.wheelDelta);
    e.preventDefault();
};
window.document.onkeydown = function(e) { // notice this overwrites previous listeners
    parent.keypress(e.keyCode);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM