简体   繁体   English

Mootools Javascript无法推送到数组

[英]Mootools Javascript can't push to array

I have an array set with the heights of each hidden div, but when I use it, the div instantly jumps down, rather than slowly sliding as when there is a literal number. 我有一个包含每个隐藏div高度的数组集,但是当我使用它时,div会立即跳下来,而不是像有文字数字时那样缓慢滑动。


EDIT: testing seems to reveal that it's a problem with the push method, as content_height.push(item.getElement('.moreInfo').offsetHeight);alert(content_height[i]); 编辑:测试似乎显示出push方法存在问题,如content_height.push(item.getElement('.moreInfo').offsetHeight);alert(content_height[i]); gives undefined, but alert(item.getElement('.moreInfo').offsetHeight); 给出未定义的,但是alert(item.getElement('.moreInfo').offsetHeight); gives the correct values 给出正确的值


Javascript: 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;
   }
  }
  });

 } );




});

Why could this be? 为什么会这样呢? Thanks so much! 非常感谢!

There's nothing wrong with your code. 您的代码没有错。 The push method works as expected - here's an example: http://jsfiddle.net/oskar/D2xps/ push方法按预期方式工作-这是一个示例: http : //jsfiddle.net/oskar/D2xps/

You need content_height[i - 1]. 您需要content_height [i-1]。

Also I'd recomend you use another indexing (since your code using global variable 'i' and it could fail because of closures): 我也建议您使用另一个索引(因为您的代码使用全局变量'i',并且由于关闭而可能失败):

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

    ....

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


});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM