使用jquery增加css顶部属性 [英] Increment css top property using jquery

查看:148
本文介绍了使用jquery增加css顶部属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下div:

<div id="new" style="position: absolute; z-index: 100; top: 210px; width: 195px..

top 属性当事件发生时?

注意,我试过: $ '#new')。css('top')+ 10 但问题是 .css 返回字符串。 $ b

Note that I've tried: $('#new').css('top')+10 but the problem is that .css returns string.

推荐答案

您可以使用 parseFloat 函数仅获取初始数字部分并将其从字符串转换为数字例如:

You can use the parseFloat function to get only the initial numeric part and convert it from a string to a number. For example:

var n = $('#new');
n.css('top', (parseFloat(n.css('top')) + 10) + 'px');

注意,如果要更改多个元素的位置可以是:

As a side note, if you want to change multiple elements' positions all at once, that could be:

$('.someClass').css('top', function(i, v) {
    return (parseFloat(v) + 10) + 'px';
});

但是如果你只是试图对一个元素进行动画处理,请看看jQuery的 .animate 方法

But if you are just trying to animate an element over time, take a look at jQuery's .animate method.

这篇关于使用jquery增加css顶部属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆