Mootools Javascript 无法推送到数组

发布于 2024-09-04 11:46:22 字数 1889 浏览 4 评论 0原文

我有一个数组,设置了每个隐藏 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;
   }
  }
  });

 } );




});

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

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.


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]);gives undefined, but alert(item.getElement('.moreInfo').offsetHeight); gives the correct values


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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囚我心虐我身 2024-09-11 11:46:22

你的代码没有任何问题。 push 方法按预期工作 - 这是一个示例:http://jsfiddle.net /奥斯卡/D2xps/

There's nothing wrong with your code. The push method works as expected - here's an example: http://jsfiddle.net/oskar/D2xps/

可爱暴击 2024-09-11 11:46:22

您需要 content_height[i - 1]。

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

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

    ....

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


});

You need 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):

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

    ....

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


});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文