admin管理员组文章数量:1328785
I need to make this parison:
if (x < siteA.style.left || x > siteA.style.left + land.width() ) {
however siteA.style.left returns 20px (e.g) so the condition doesn't work. How do I remove the px and transform it into an integer so I can work with it?
I tried alert(parseInt(siteA.style.left))
and it returned NaN however alert(siteA.style.left)
returned 30px, 60px etc.
I need to make this parison:
if (x < siteA.style.left || x > siteA.style.left + land.width() ) {
however siteA.style.left returns 20px (e.g) so the condition doesn't work. How do I remove the px and transform it into an integer so I can work with it?
I tried alert(parseInt(siteA.style.left))
and it returned NaN however alert(siteA.style.left)
returned 30px, 60px etc.
4 Answers
Reset to default 3Use parseInt
:
var leftPos = parseInt(siteA.style.left, 10);
where 10
is the base, good practice to always specify it.
Just use parseInt().
Have you tried this?
var left = parseInt(siteA.style.left.replace('px', ''), 10)
When using jQuery you can use $('#siteA').offset().left
which returns only the integer value.
本文标签: Javascripttransform elemstyleleft into integerStack Overflow
版权声明:本文标题:Javascript - transform `elem.style.left` into integer? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742259147a2442186.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论