【问题标题】:Mootools Javascript can't push to arrayMootools Javascript 无法推送到数组
【发布时间】:2010-06-06 15:22:27
【问题描述】:

我有一个数组设置了每个隐藏 div 的高度,但是当我使用它时,div 会立即向下跳,而不是像有文字数字时那样缓慢滑动。


编辑:测试似乎表明这是 push 方法的问题,因为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;
   }
  }
  });

 } );




});

为什么会这样?非常感谢!

【问题讨论】:

  • 您是否检查过“offsetHeight”是否返回一个附加了“px”的值?你确定这不是“自动”之类的东西吗?可以是字符串而不是数字吗?
  • 仅供参考:您不需要手动增加i,只需使用els.each(fn(el, index){});

标签: javascript variables mootools height accordion


【解决方案1】:

您的代码没有问题。 push 方法按预期工作 - 这是一个示例:http://jsfiddle.net/oskar/D2xps/

【讨论】:

    【解决方案2】:

    你需要 content_height[i - 1]。

    我还建议您使用另一个索引(因为您的代码使用全局变量“i”并且它可能由于闭包而失败):

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

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 2011-07-07
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 2017-12-07
      • 2016-12-26
      • 1970-01-01
      • 2014-04-13
      相关资源
      最近更新 更多