开发者

Any way to take a css value from another element, and add it with another?

I am using this right now...

if($('.current').hasClass('odd') && !$('.current').hasClass('even')) {
                        var oddMarL = $('.current img').css('margin-left'); 
                        var oddMarL2 = $('.touch').css('margin-left'); 
                        var oddMarT = $('.touch').css('margin-top'); 

                        oddMarL.replace('px', '');
                        oddMarL2.replace('px', '');
                        oddMarT.replace('px', '');

                        Math.abs(oddMarL,oddMarL2,oddMarT);

                        oddMarL = oddMarL + oddMarL2;

                        $('#zoom').css('margin-left',oddMarL);
                        $('#zoom').css('margin-top',oddMarT);
                    }

开发者_Go百科Is there something wrong with my code or is it just not possible?

It works until I add the 2 values together or when I use math.abs. In the error console it says margin-left cannot be parsed.

I have reasons for needing it this way so please don't recommend using a class! :) thanks.


The replace and Math.max functions don't work the way your code seems to assume they would. They return values:

oddMarL = oddMarL.replace('px', '');

and

oddMarL = Math.abs(parseInt(oddMarL, 10));

are necessary to actually change the values. You'd have to do the same thing with the other variables.


try this also:

$('#zoom').css('margin-left',oddMarL+'px');
$('#zoom').css('margin-top',oddMarT+'px');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜