jQuery获取计算的CSS值

时间:2013-01-16 00:03:46

标签: jquery css attr

我有一个循环,每次迭代都会使margin-top减少182px。我想获得margin-top的值,所以我可以告诉它什么时候停止运行但是当我尝试在控制台中运行它时,它返回“undefined”请告知如何更改它以获得真正的价值。

以下是我正在使用的内容,我使用的是attr(),因为它需要获取的值是内联样式:

$marginTop = $('.emp-wrap').attr("style");

其余代码位于

之下
// if statements to move carousel up
$carouselNum = $('.carousella').length;

$marginTop = $('.emp-wrap').attr("style");

if($carouselNum  > 1){
//     // function empMove, the '-=' allows the -margin-top to run every time. Without this it will set the margin-top to the same value every loop
    function empMove() { $('.emp-wrap').css('margin-top', '-=182')};

    setInterval(empMove, 20000);
}
else if($carousel < 1){
    // do something
}
else{
    // do something
}

3 个答案:

答案 0 :(得分:5)

试试这个:

$marginTop = $('.emp-wrap').css("margin-top");

答案 1 :(得分:1)

$marginTop = $('.emp-wrap').css("margin-top");

答案 2 :(得分:0)

使用以下

$('.emp-wrap').css("margin-top");

此外,您可以使用parseInt仅获取整数值

parseInt($(".testclass").css("margin-top"));

请参阅:http://jsfiddle.net/kEVq7/