方法一:
使用obj.style.left和obj.style.top
该方法获取到的样式可读可写
但是这种方法有局限性,仅能获取行内样式中的left,right。
如果写在了style标签里,或者link引入的css文件中,则获取不到
方法二:
使用window.getComputedStyle
该方法接收两个参数
第一个参数是,要获取样式的元素
第二个是可选参数,要匹配的伪元素的字符串
用法
var box = document.querySelector('.box')
var boxStyle = window.getComputedStyle(box, null)
console.log(boxStyle.left + ' ' + boxStyle.top)
该方法获取到的样式是可读不可写的
方法三:
使用obj.offsetLeft和obj.offsetTop
var box = document.querySelector('.box')
console.log(box.offsetLeft + ' ' + box.offsetTop)
该方法可读不可写