原生JS获取坐标

福尔摩斯对我笑 2019-06-15 5399 次 2218条

JS获取元素的offsetTop,offsetLeft等属性

obj.clientWidth //获取元素的宽度(width+padding)
obj.clientHeight //元素的高度
obj.offsetLeft //元素相对于父元素的left
obj.offsetTop //元素相对于父元素的top
obj.offsetWidth //元素的宽度(width+padding+border)
obj.offsetHeight //元素的高度

获取元素的纵坐标(相对于窗口)

// 获取元素的纵坐标(相对于窗口)
function getTop(e) {
  var offset = e.offsetTop
  if (e.offsetParent != null) offset += getTop(e.offsetParent)
  return offset
}

获取元素的横坐标(相对于窗口)

function getLeft(e) {
  var offset = e.offsetLeft
  if (e.offsetParent != null) offset += getLeft(e.offsetParent)
  return offset
}

上面的方法无疑影响性能,好在浏览器给我提供了相应的接口getBoundingClientRect,这个方法最早出现在IE浏览器中,后来的浏览器也跟着支持了这个方法,而且还更加完善,IE中只能获取到元素的left,top,bottom,right的属性,而后面的现代浏览器还能获取到元素的width和height.

var box = document.getElementById("box");
var pos = box.getBoundingClientRect();
box.innerHTML = "top:"+pos.top +
  "left:"+pos.left +
  "bottom:"+pos.bottom +
  "right:"+pos.right +
  "width:"+pos.width +
  "height:"+pos.height

标签: 无

非特殊说明,本博所有文章均为博主原创。

头像也是LOGO
未来博客 博主大人
到了这个年纪,不应该再彷徨!

发表评论



 


已有 2218 条评论 1


  1. austin texas prostitutes

    You made some good points there. I checked on the net to learn more about the issue and found most individuals will go along with your views on this web site.

    回复 2020年1月22日
  2. Hoa Qua Sach

    I am typically to blogging and i actually recognize your content. The article has really peaks my interest. I’m going to bookmark your website and keep checking for new information.

    回复 2020年1月22日
  3. Trai cay sach Anh Xanh

    Perfectly written subject material , thanks for entropy.

    回复 2020年1月22日
  4. Nonton Bokep

    Aw, this was an extremely good post. Spending some time and actual effort to produce a top notch article… but what can I say… I put things off a whole lot and never seem to get nearly anything done.

    回复 2020年1月22日
  5. Hoa Qua Sach Anh Xanh

    I’m impressed, I need to say. Really not often do I encounter a blog that’s both educative and entertaining, and let me tell you, you may have hit the nail on the head. Your thought is excellent; the difficulty is something that not sufficient individuals are talking intelligently about. I am very completely happy that I stumbled throughout this in my search for one thing regarding this.

    回复 2020年1月22日
  6. video bokep

    Hello there! I just would like to offer you a huge thumbs up for your great info you have got right here on this post. I’ll be returning to your website for more soon.

    回复 2020年1月22日
  7. situs togel terpercaya

    I couldn’t refrain from commenting. Exceptionally well written!

    回复 2020年1月22日
  8. video bokep

    An intriguing discussion is definitely worth comment. I believe that you ought to write more on this subject matter, it may not be a taboo matter but generally people do not discuss these subjects. To the next! All the best!!

    回复 2020年1月22日
  9. karisini escort sitesinde satan pic

    karisini escort sitesinde satan pic

    回复 2020年1月22日
  10. Aconda

    I needed to thank you for this good read!! I definitely loved every little bit of it. I have you saved as a favorite to check out new stuff you post…

    回复 2020年1月22日
  11. Best Massage Gun

    Hey there! I simply would like to offer you a huge thumbs up for the great information you’ve got here on this post. I am coming back to your blog for more soon.

    回复 2020年1月22日
  12. fully opening windows

    I have recently started a website, the info you offer on this website has helped me greatly. Thanks for all of your time & work.

    回复 2020年1月22日
  13. apk vivoslot

    Good post. I learn something totally new and challenging on websites I stumbleupon on a daily basis. It’s always interesting to read through content from other writers and practice a little something from other sites.

    回复 2020年1月22日
  14. situs togel

    Greetings, I think your blog could possibly be having browser compatibility issues. When I take a look at your site in Safari, it looks fine however when opening in I.E., it has some overlapping issues. I just wanted to provide you with a quick heads up! Aside from that, excellent website!

    回复 2020年1月22日
  15. bandar togel terpercaya

    bookmarked!!, I really like your blog!

    回复 2020年1月21日
  16. situs togel terpercaya

    Way cool! Some very valid points! I appreciate you writing this write-up plus the rest of the site is really good.

    回复 2020年1月21日
  17. bandar togel terpercaya

    That is a good tip especially to those fresh to the blogosphere. Brief but very precise information… Many thanks for sharing this one. A must read post!

    回复 2020年1月21日
  18. bandar togel terpercaya

    Way cool! Some very valid points! I appreciate you penning this write-up plus the rest of the website is very good.

    回复 2020年1月21日
  19. togel online

    The next time I read a blog, I hope that it does not fail me as much as this one. I mean, I know it was my choice to read, however I truly thought you would have something interesting to say. All I hear is a bunch of crying about something you could fix if you were not too busy searching for attention.

    回复 2020年1月21日
  20. http://merpati99.info

    I couldn’t refrain from commenting. Very well written!

    回复 2020年1月21日
  21. Perusahaan recruitment

    May I simply say what a relief to uncover someone that really knows what they are talking about on the net. You certainly realize how to bring a problem to light and make it important. A lot more people need to check this out and understand this side of your story. I was surprised you are not more popular since you surely possess the gift.

    回复 2020年1月21日
  22. Hoa qua sach

    This is really interesting, You’re a very skilled blogger. I’ve joined your rss feed and look forward to seeking more of your wonderful post. Also, I’ve shared your website in my social networks!

    回复 2020年1月21日
  23. situs togel

    I would like to thank you for the efforts you have put in penning this site. I really hope to view the same high-grade content by you in the future as well. In truth, your creative writing abilities has motivated me to get my own website now 😉

    回复 2020年1月21日
  24. togel online

    Hello there! This blog post couldn’t be written any better! Reading through this post reminds me of my previous roommate! He always kept preaching about this. I am going to send this information to him. Fairly certain he will have a very good read. Thank you for sharing!

    回复 2020年1月21日
  25. https://www.familyhealthavenue.net/

    This is the perfect web site for everyone who hopes to find out about this topic. You know a whole lot its almost hard to argue with you (not that I actually will need to…HaHa). You definitely put a new spin on a subject that has been discussed for decades. Wonderful stuff, just wonderful!

    回复 2020年1月21日
  26. Jasa Website Murah

    Wonderful article! We will be linking to this particularly great article on our website. Keep up the great writing.

    回复 2020年1月21日
  27. mainbadarq

    Pretty! This was an extremely wonderful article. Many thanks for supplying this information.

    回复 2020年1月21日
  28. howtogetbetterinlife.com

    I really love your website.. Pleasant colors & theme. Did you make this web site yourself? Please reply back as I’m planning to create my very own blog and want to know where you got this from or what the theme is named. Cheers!

    回复 2020年1月21日
  29. bandar togel terpercaya

    Excellent post! We are linking to this particularly great content on our website. Keep up the good writing.

    回复 2020年1月21日
  30. togel online

    Hi, I think your web site could possibly be having internet browser compatibility problems. When I look at your site in Safari, it looks fine however when opening in Internet Explorer, it’s got some overlapping issues. I just wanted to give you a quick heads up! Apart from that, wonderful blog!

    回复 2020年1月21日
  31. bullas

    Oh my goodness! Impressive article dude! Many thanks, However I am experiencing difficulties with your RSS. I don’t know why I am unable to join it. Is there anybody having identical RSS problems? Anyone that knows the answer will you kindly respond? Thanx!!

    回复 2020年1月21日
  32. laotracarboneria.net

    This page really has all of the info I needed concerning this subject and didn’t know who to ask.

    回复 2020年1月21日
  33. serebryaniy-bereg.ru

    Good post. I learn something new and challenging on websites I stumbleupon every day. It’s always exciting to read articles from other writers and practice a little something from other web sites.

    回复 2020年1月21日
  34. agen vivoslot

    After I initially commented I seem to have clicked the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I get 4 emails with the exact same comment. Is there a way you can remove me from that service? Kudos!

    回复 2020年1月21日
  35. q-house.pl

    Way cool! Some extremely valid points! I appreciate you writing this post and the rest of the website is also really good.

    回复 2020年1月21日
  36. http://n3k4.com/

    After exploring a number of the articles on your site, I truly like your technique of writing a blog. I bookmarked it to my bookmark site list and will be checking back soon. Please visit my web site too and let me know your opinion.

    回复 2020年1月21日
  37. singstreet.de

    After looking into a number of the blog articles on your blog, I truly like your way of writing a blog. I book-marked it to my bookmark site list and will be checking back soon. Please check out my website too and let me know your opinion.

    回复 2020年1月21日
  38. idnpoker

    Pretty! This was an incredibly wonderful post. Many thanks for supplying this information.

    回复 2020年1月21日
  39. http://199.192.25.65/

    Aw, this was an exceptionally good post. Spending some time and actual effort to produce a great article… but what can I say… I put things off a whole lot and never seem to get anything done.

    回复 2020年1月21日
  40. video bokep

    Very good information. Lucky me I ran across your site by accident (stumbleupon). I have saved it for later!

    回复 2020年1月21日
  41. judi online

    Everything is very open with a clear description of the issues. It was really informative. Your website is very useful. Thanks for sharing!

    回复 2020年1月21日
  42. dominoqq online terpercaya

    I truly love your site.. Pleasant colors & theme. Did you make this web site yourself? Please reply back as I’m looking to create my own personal website and would love to learn where you got this from or what the theme is called. Many thanks!

    回复 2020年1月21日
  43. http://199.192.19.41/

    Very good post. I absolutely love this site. Stick with it!

    回复 2020年1月21日
  44. video bokep

    This excellent website certainly has all the information I needed about this subject and didn’t know who to ask.

    回复 2020年1月21日
  45. vivoslot login

    I would like to thank you for the efforts you have put in penning this site. I am hoping to check out the same high-grade content by you in the future as well. In truth, your creative writing abilities has inspired me to get my own website now 😉

    回复 2020年1月21日
  46. Pusatfilm 21

    Having read this I thought it was very enlightening. I appreciate you taking the time and effort to put this content together. I once again find myself spending way too much time both reading and leaving comments. But so what, it was still worth it!

    回复 2020年1月21日
  47. Pkv Deposit Pulsa

    Pretty! This has been a really wonderful article. Thank you for supplying this information.

    回复 2020年1月21日
  48. Gonggong.id

    I really love your blog.. Excellent colors & theme. Did you create this website yourself? Please reply back as I’m attempting to create my own personal site and want to find out where you got this from or just what the theme is called. Cheers!

    回复 2020年1月21日
  49. manga tensei

    This is a really good tip especially to those fresh to the blogosphere. Short but very precise information… Thank you for sharing this one. A must read article!

    回复 2020年1月21日
  50. batoto

    Next time I read a blog, Hopefully it does not fail me just as much as this particular one. I mean, I know it was my choice to read, however I genuinely believed you would have something useful to say. All I hear is a bunch of complaining about something you could fix if you weren’t too busy looking for attention.

    回复 2020年1月21日