简体   繁体   English

jQuery animate()定位问题

[英]jQuery animate() to position issue

I'm just working on my personal website, giving it a bit of a revamp. 我只是在我的个人网站上工作,对此做了一些修改。

I've implemented a sort of 'accordion' menu feature, where if a menu button is clicked, the "non-clicked" buttons disappear, and then the clicked button is moved to the top of the list, where then a panel animates down in which I will be putting text content. 我实现了一种“手风琴”菜单功能,如果单击了菜单按钮,“未单击”按钮就会消失,然后将被单击的按钮移到列表的顶部,然后面板会向下移动我将在其中放置文本内容。

In Firefox this works perfectly, however in IE and Chrome the button jumps to the top of the page and then animates to position, instead of animating from where it started from. 在Firefox中,此功能可以完美运行,但是在IE和Chrome中,按钮跳转到页面顶部,然后进行动画定位,而不是从其起始位置进行动画处理。

Anyone any ideas how to fix this? 任何人有任何想法如何解决此问题?

Offending code: 违规代码:

function Accordion(e)
 {
  var o =
  {
   init: function()
   {
    o.button = e;
    o.addClickHandler();
   },

   addClickHandler: function()
   {
    o.button.click(function(){
     o.button.removeClass('unselected');
     o.button.addClass('selected');
     o.fader();
    });
   },

   fader: function()
   {
    $j('.unselected').animate({opacity:0}, 1000);

    var wait = setInterval(function() {
     if(!$j('.unselected').is(":animated") ) {
      clearInterval(wait);
      o.shifter();
     }
    }, 100);
   },

   shifter: function()
   {
    o.button.css({'position':'absolute'});
    o.button.animate({top:91}, 500, o.createInfoBox);
   },

   createInfoBox: function()
   {
    var buttonParent = o.button.parent();
    buttonParent.append("<div class='infoBox'></div>");
    $j('.infoBox').animate({height:390});
   }
  }

  o.init();
  return o;
 }
}

The issue lies within the shifter function, where I'm setting the position to absolute and then animating so the desired effect can be achieved. 问题出在shifter函数之内,在这里我将位置设置为绝对,然后进行动画处理,从而可以实现所需的效果。 I understand why it's doing this (presume it's just resetting itself to top:0 and then animating to top:91) but does anyone have a quick solution? 我知道为什么要这样做(假设它只是将自身重置为top:0,然后动画为top:91),但是有人能快速解决吗? It's late and it's doing my head in. 已经很晚了,我正在努力。

Much appreciated, 非常感激,

Dave 戴夫

HAve you tried using the current position of the element when you switch it to absolute... for example: 当您将元素切换为绝对值时是否尝试使用元素的当前位置...例如:

function() { 
  var currentp = $(this).offset();
  o.button.css({'position':'absolute', top: currentp.top}); 
  o.button.animate({top:91}, 500, o.createInfoBox); 
}

Note there are two different offset functions and im not sure which one you want use here so you might want to review the docs on that. 请注意,有两种不同的偏移量函数,我不确定在这里要使用哪一种偏移量函数,因此您可能需要查看该文档。

Also you could always just re-theme the jQuery-ui accordian and save yourself the trouble ;-) 另外,您总是可以重新设置jQuery-ui手风琴主题,从而省去麻烦;-)

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

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