0

嗨,我有以下代码可以控制图片库的位置(可以在 steven.tlvweb.com 上看到)。滚轮当前控制画廊位置,但 keydown 事件不控制,我希望他们这样做。可以看到下面代码中的警报(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 新手程序员)

4

1 回答 1

0

不要parent在 DOM 元素上使用该属性。相反,只需在局部变量上创建一个闭包。所以,更换

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);
};
于 2012-12-12T21:43:50.757 回答