2016-08-23 125 views
0

道场有marginBox功能: https://dojotoolkit.org/reference-guide/1.7/dojo/marginBox.html什么是jQuery相当于dojo的marginBox()?

什么是该功能的jQuery的等价物?

+1

我不知道道场,但在本地DOM API,它看起来非常相似[getBoundingClientRect()](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect)方法,除了它是相对于视口,而不是任何非静态父元素。为此,您有'element.offsetLeft'和'element.offsetTop',这与使用jQuery'position()'方法的结果完全相同 –

回答

1

你可以使用几个jQuery的函数来获取你需要的信息。

console.log("Width: " + $('#a1').outerWidth(true)); 
 
console.log("Height: " + $('#a1').outerHeight(true)); 
 
console.log("Top: " + $('#a1').position().top); 
 
console.log("Left: " + $('#a1').position().left);
body { 
 
    margin: 0; 
 
    padding: 0; 
 
} 
 
#a1 { 
 
    width: 250px; 
 
    height: 150px; 
 
    padding: 10px; 
 
    margin: 15px; 
 
    border: 1px solid gray; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<br /><br /> 
 
<div id="a1"> 
 
    This is a div with margin, padding and border 
 
</div>

相关问题