2012-12-12 69 views
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; 
    } 

    }, 

} 

在此先感謝 史蒂芬(新手Java程序員)

+1

更換

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; } 

我猜你想做的事'ImageFlow.prototype.scroll =功能(SC){'。現在您正在覆蓋ImageFlow對象的原型,而不是爲其添加功能。 –

+0

你從哪裏得到'parent'? – mVChr

+0

那些'oc'和'parent'屬性是什麼? – Bergi

回答

0

不要使用了DOM元素parent財產。相反,只需在局部變量上創建一個閉包。所以,用

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); 
}; 
+0

完成,非常感謝! –