简体   繁体   English

Javascript:在元素的“ top”属性上添加/减去?

[英]Javascript: Adding/Subtracting to 'top' attribute to elements?

I am trying to add some pixels from an element on my page. 我正在尝试从页面上的元素添加一些像素。 This is so I can create a new element and place it above it. 这样一来,我可以创建一个新元素并将其放置在其上方。

 posy = document.getElementById('user').style.top + 20;

But it is returning "50px20", I haven't tried subtracting it yet, but what should I do to prevent it from returning 'px' in it? 但是它正在返回“ 50px20”,我还没有尝试过减去它,但是我应该怎么做才能防止它在其中返回“ px”?

Is there a way to do this in jQuery? 有没有办法在jQuery中做到这一点?

Thanks, Alex. 谢谢,亚历克斯。

What you are asking is a little confusing. 您要问的是有点混乱。 You say you want the top attribute, but this could be the position of the element on the page or the top value of its style. 您说您想要top属性,但这可能是元素在页面上的位置或其样式的最高值。 There is a distinct difference. 有明显的区别。 It sounds like you want the position of the element because you are storing the results in posy . 听起来好像您想要元素的位置,因为您将结果存储在posy This is different than getting the top value of the CSS which will only exist if it has been defined in your stylesheet. 这与获得CSS的最高值不同,后者只有在样式表中已定义时才存在。

To get the top CSS value in jQuery you would use 要在jQuery中获得top CSS值,您可以使用

$('selector').css('top');

To get the position of the element use jQuery's offset() or position() methods. 要获取元素的位置,请使用jQuery的offset()position()方法。 You probably would want offset. 您可能需要补偿。

$('selector').offset().top; //or .left

To fix your returned value for px or em try parseInt. 要固定返回的pxem值,请尝试parseInt。

parseInt(value);

JavaScript's + operator performs concatenation of strings as well as adding of Numbers. JavaScript的+运算符执行字符串的连接以及数字的添加。 This can be pretty confusing, but once you know, it's easy to solve using parseInt : 这可能非常令人困惑,但是一旦您知道了,就可以使用parseInt轻松解决:

var posy = parseInt(document.getElementById('user').style.top, 10) + 20;

One other quirk: parseInt(number) doesn't always work as expected. 另一个怪癖: parseInt(number)并不总是按预期工作。 If number is a string of a number start starts with a leading zero (like 0123 ), parseInt will assume it is in base 8. 如果number是一个number字符串,则以前导零开头(例如0123 ),parseInt将假定其以8为底。

To make sure it works as expected, use radix : parseInt(number, 10); 为了确保它按预期工作,请使用radixparseInt(number, 10);

PS: mrtsherman has a good point about your underlying intent. 附言mrtsherman对您的潜在意图有很好的看法 And while I agree with his advise to use jQuery, it is possible with plain JavaScript as well: 虽然我同意他的建议使用jQuery,但可以使用普通JavaScript:

function findOffsetTop(element) {
    var offset = 0;
    do {
        offset += element.offsetTop;
    } while (element = element.offsetParent);
    return offset;
}

var user = document.getElementById('user');
var posy = findOffsetTop(user) + 20;

(Based on http://www.quirksmode.org/js/findpos.html ) (基于http://www.quirksmode.org/js/findpos.html

posy = parseInt($('#user').css('top').replace('px', ''), 10) + 20;

Plain Javascript method: 普通的Javascript方法:

posy = document.getElementById('user').style.top.replace('px','') + 20;

jQuery method: jQuery方法:

posy = $('user').css('top').replace('px','')+20;

尝试这个:

posy = parseInt(document.getElementById('user').style.top) + 20;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM