2010-06-05 39 views
0

我有一個數組設置每個隱藏div的高度,但是當我使用它時,div會立即跳下來,而不是像文字數字那樣緩慢滑動。Mootools Javascript無法推送到陣列


編輯:測試似乎表明它與推送方法的問題,因爲content_height.push(item.getElement('.moreInfo').offsetHeight);alert(content_height[i]);給出定義,但alert(item.getElement('.moreInfo').offsetHeight);給出了正確的價值觀


的Javascript:

window.addEvent('domready', function(){ 

var content_height = [];i=0; 

$$('.bio_accordion').each(function(item){ 
    i++; 
    content_height.push(item.getElement('.moreInfo').offsetHeight); 
    var thisSlider = new Fx.Slide(item.getElement('.moreInfo'), { mode: 'horizontal' }); 
    thisSlider.hide(); 


    item.getElement('.moreInfo').set('tween').tween('height', '0px'); 

    var morph = new Fx.Morph(item.getElement('.divToggle')); 
    var selected = 0; 
    item.getElement('.divToggle').addEvents({ 
    'mouseenter': function(){ 
    if(!selected) this.morph('.div_highlight'); 
    }, 

    'mouseleave': function(){ 
    if(!selected) { 
    this.morph('.divToggle'); 
    } 
    }, 

    'click': function(){ 
    if (!selected){ 
    if (this.getElement('.symbol').innerHTML == '+') 
    this.getElement('.symbol').innerHTML = '-'; 
    else 
    this.getElement('.symbol').innerHTML = '+'; 
    item.getElement('.moreInfo').set('tween', { 
    duration: 1500, 
    transition: Fx.Transitions.Bounce.easeOut 
    }).tween('height', content_height[i]); //replacing this with '650' keeps it smooth 
    selected = 1; 
    thisSlider.slideIn(); 
    } 
    else{ 
    if (this.getElement('.symbol').innerHTML == '+') 
    this.getElement('.symbol').innerHTML = '-'; 
    else 
    this.getElement('.symbol').innerHTML = '+'; 
    thisSlider.slideOut(); 
    item.getElement('.moreInfo').set('tween', { 
    duration: 1000, 
    transition: Fx.Transitions.Bounce.easeOut 
    }).tween('height', '0px'); 
    selected = 0; 
    } 
    } 
    }); 

}); 




}); 

這是爲什麼呢?非常感謝!

+0

你檢查,看看是否「的offsetHeight」正在恢復你附有「px」的值?你確定它不是像「汽車」?它可能是一個字符串,而不是一個數字? – Pointy 2010-06-05 03:13:38

+1

僅供參考:您不需要手動增加'i',只需使用'els.each(fn(el,index){});'。 – 2010-06-05 06:32:47

回答

0

您需要content_height [i - 1]。

而且我會建議更換使用另一個索引(使用全局變量「我」,因爲你的代碼,這可能是因爲封鎖的失敗):

$$('.bio_accordion').each(function(item, index) { 
    content_height[index] = item.getElement('.moreInfo').offsetHeight; 

    .... 

    tween('height', content_height[index]); 


});