开发者

jquery - get position problem in Firefox

开发者 https://www.devze.com 2023-01-30 06:09 出处:网络
I have the following code which gets the left and top position of the element you are hovered over. It produces the right r开发者_如何学运维esults in safari and IE but fails to get the position of the

I have the following code which gets the left and top position of the element you are hovered over. It produces the right r开发者_如何学运维esults in safari and IE but fails to get the position of the img that you hover over in Firefox - it returns 0, 0. Can anyone see why this might be??

I think it might be something to do with setting it as the variable as it seems to work if I place it straight in the function. I need to set it as a variable though so it can return to its original state.

$.fn.hoverAnimationTwo = function () {
    return $(this).each(function () {

        var originalLeftTwo = parseInt($(this).css("left"));
        var originalTopTwo  = parseInt($(this).css("top"));

        return $(this).hover(function () {
            $(this).animate({
                width: "17px",
                height: "17px",
                left: originalLeftTwo - 5,
                top: originalTopTwo - 5
            }, 100);
        },function () {

            $(this).animate({
                width: "7px",
                height: "7px",
                left: originalLeftTwo,
                top: originalTopTwo
            }, 100);
        });
    });
}

$(".myImg").hoverAnimationTwo();

The other thing which is very odd is that I can copy all of my code into jsfiddle and it seems to work.

EDIT: OK... so it turns out this isn't a javascript issue as such. It is because elsewhere in the page I had given an element a class beginning with a numeric character which is entirely my bad and I should know better!


Browsers seem to have some inconsistencies when relying on CSS properties. Have you tried using .position() or .offset() (whichever is applicable to your needs)?

http://api.jquery.com/offset/ or http://api.jquery.com/position/

0

精彩评论

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