2011-07-07 48 views
0

我正在創建一個拖放沉重頁面,並給每個可拖動元素defaultTopdefaultLeft屬性,以便我可以輕鬆地將其重置爲原始位置。這很好,直到突然,重置函數報告它們的值爲undefined。由於我沒有在這些碎片破碎時進行工作(他們已經工作了一段時間),我不確定我出了什麼問題。謝謝您的幫助。爲什麼我的房產失去了定義? (JS)

創建該元素的作用是:

function makeTool(id, imgURL, defaultL, defaultT,trayname){ 
var a=document.createElement('img'); 
a.setAttribute('src', imgURL); 
a.setAttribute('id',id); 
a.setAttribute('class','drag'); 
a.intray=true; 
a.tray=trayname; 
a.rotation=0; 
a.style.zIndex=document.getElementById(trayname).style.zIndex+1; 
document.getElementById(trayname).appendChild(a); 
a.defaultTop=ExtractNumber(document.getElementById(trayname).style.top)+defaultT; 
a.defaultLeft=ExtractNumber(document.getElementById(trayname).style.left)+defaultL; 
a.style.top=a.defaultTop+'px'; 
a.style.left =a.defaultLeft+'px'; 
a.addEventListener('touchstart', function(e) { return self.touchStart(e) }, false); 
a.addEventListener('gesturestart', function(e) { return self.gestureStart(e) }, false); 
a.addEventListener('touchmove', function(e) { return self.touchMove(e) }, false); 
a.addEventListener('touchend', function(e) { return self.touchEnd(e) }, false); 
tools.push(a); 
return a; 
} 

和復位功能是:

function reposition(t){ 
    console.log(t+' top: '+document.getElementById(t).defaultTop); 
    document.getElementById(t).style.left=document.getElementById(t).defaultLeft+'px'; 
    document.getElementById(t).style.top=document.getElementById(t).defaultTop+'px'; 
} 

這就是我得到的undefined

+0

只是出於興趣:你爲什麼從頭開始寫很多東西?爲什麼不使用像jQuery這樣的庫來完成所有繁重的工作? – mydoghasworms

+0

主要是因爲我正在學習,並且我在一段時間內沒有使用過jQuery,所以它不如其他時間節省太多時間 –

+0

您是否嘗試過'console.log(a.defaultTop)'在哪裏'a.defaultTop'被定義?它從一開始就被賦予了「undefined」嗎?您的makeTool函數中的 – Tony

回答

2

我的日誌不知道泰勒是否有幫助,但我有類似的問題。

我在寫一個對象來計算我的DIV的寬度德據此我innerWidth:

function ScreenGrid() { 
'use strict'; 

this.ColumnIDs = new Array(); 
this.Widths = new Array(); 
this.Dynamic = true; 
this.Set = adjust; 

var ColumnIDsPersist; 
var WidthsPersist; 

function adjust() { 

    //stuff 

    for (var i = 0; i < ColumnIDsPersist.length; i++) { 

的調節功能就是去魔法happends。爲了確保的DIV將保持安裝在屏幕上我用下面的代碼:

if (window.addEventListener) { 
      // Good browsers. 
      window.addEventListener('resize', react, false); 
     } 
     else if (w.attachEvent) { 
      // Legacy IE support. 
      window.attachEvent('onresize', react); 
     } 
     else { 
      // Old-school fallback. 
      window.onresize = react; 
     } 

這是反應函數:

// Slight delay. 
function react() { 

    // Clear the timer as window resize fires, 
    // so that it only calls adapt() when the 
    // user has finished resizing the window. 
    clearTimeout(timer); 

    // Start the timer countdown. 
    timer = setTimeout(adjust, 16); 
    // -----------------------^^ 
    // Note: 15.6 milliseconds is lowest "safe" 
    // duration for setTimeout and setInterval. 
    // 
    // http://www.nczonline.net/blog/2011/12/14/timer-resolution-in-browsers 
} 

的問題是每次REACT火災,鄰有不確定在這個.ColumnIDs中。經過很多調查,我注意到當我從SET()中調用ADJUST時,這個對象是我的SCREENGRID,但是當我從REACT中觸發它時,它變成了WINDOW,所以我在這個.ColumnIDs中得到了undefined。

我解決了這個代碼:

function ScreenGrid() { 
'use strict'; 

this.ColumnIDs = new Array(); 
this.Widths = new Array(); 
this.Dynamic = true; 
this.Set = adjust; 

var ColumnIDsPersist; 
var WidthsPersist; 

function adjust() { 

    // This clearTimeout is for IE. 
    // Really it belongs in react(), 
    // but doesn't do any harm here. 
    clearTimeout(timer); 

    // Parse browser width. 
    var _width = window.innerWidth || window.document.documentElement.clientWidth || window.document.body.clientWidth || 0; 
    var _heigth = window.innerHeight || window.document.documentElement.clientHeight || window.document.body.clientHeight || 0; 
    var _usedWidth = 0; 
    var _fluidColumn = null; 


    //Ao chamar a função adjust novamente no evento on resize, o objeto THIS deixa de ser a função e passa a ser a janela, 
    //Essas vareáveis garantem a persistencia dos dados a serem utilizados. 
    ColumnIDsPersist = (typeof ColumnIDsPersist == 'undefined') ? this.ColumnIDs : ColumnIDsPersist; 
    WidthsPersist = (typeof WidthsPersist == 'undefined') ? this.Widths : WidthsPersist; 

    for (var i = 0; i < ColumnIDsPersist.length; i++) { 

嗯,就是這樣,我希望它能幫助!

相關問題