开发者

Jquery issue in IE, Chrome but not in FF

开发者 https://www.devze.com 2023-04-07 09:58 出处:网络
Having some problems with a piece of Jquery code, well that\'s where I think the problem is开发者_高级运维 - On FF when the page loads after 2 seconds it animates panels from the left of the screen to

Having some problems with a piece of Jquery code, well that's where I think the problem is开发者_高级运维 - On FF when the page loads after 2 seconds it animates panels from the left of the screen to the right. It looks fine without any issues but in Chrome and IE it jumps back and continues moving across.

Here is the code -

<script type="text/javascript" language="javascript">

$(function() {

   // Display div slowly
    $('#one').delay(2000).show('slow');
    $("#one").animate({
        left: '+=159'
    }, 3000);


    $('#two').delay(3000).show('slow');
    $("#two").animate({
        left: '+=159'
    }, 3000);

    $('#three').delay(4000).show('slow');
    $("#three").animate({
        left: '+=159'
    }, 3000);

    $('#four').delay(5000).show('slow');
    $("#four").animate({
        left: '+=159'
    }, 3000);
    });

 </script>

Here is a preview of the page - http://www.visrez.com/preview/och-group/

P.S I'm not ruling out it's a css issue...

Thanks in advance for any help!

Gearóid


The problem stems from the fact that Chrome isn't taking into account the position (relative to the offset parent) that div has on the page before you start moving it. So, once animated, it has a value of left: 159px which is relative to the body. Firefox is taking the original position into account and ends with left: 256px.

I haven't looked into why this happens yet but here are a few things that you could do (workarounds):

  1. Before animating figure out the position relative to the offset parent and set the CSS top and left attributes. Then, hopefully, the final value will be the value that you set plus 159px. Something like:

    var offset = $("#one").offset();
    $("#one").css({"top": offset.top, "left": offset.left});
    // animate
    
  2. Wrap #oneTop and #one and each other pair in a div that is position: relative. This will set the offset parent of the animated div to the position: relative div. This way the +159px is relative to where the div would be in the document.


i prepared a demo with your code and it works.

http://jsfiddle.net/AjDLS/2/

i don't have your problem, so that makes me think you must have some other javascript issue, or css issue, could you post more code?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号