当使用 css3 动画进行动画处理时,jQuery 获取元素位置
如果您使用 CSS3 动画在屏幕上为相对位置的元素定位内容,
-moz-transform: translate(125px, 5px);
当您使用 javascript 询问项目位置时,它会报告该项目仍然是
top:0
left:0
例如,看看此示例中使用的元素的位置jQuery 同位素插件:
http://isotope.metafizzy.co/demos/basic.html
将要我必须像这样使用 jQuery css() 方法吗?我什至不知道这是否有效!
$(element).css('-moz-transform')
更新:
我已经在 JsFiddle http://jsfiddle.net/uQtur/3/
JS
var elements = $('.element').each(function(i, e){
var el = $(this);
var position = el.position();
var offset = el.offset();
console.log('position', position);
console.log('offset', offset);
var positionLeft = window.getComputedStyle(el.get(0), null).getPropertyValue('left');
var positionTop = window.getComputedStyle(el.get(0), null).getPropertyValue('top');
console.log('computed position left', positionLeft);
console.log('computed position top', positionTop);
var transform = el.css('-moz-transform');
console.log(transform);
// > matrix(1, 0, 0, 1, 340px, 10px)
});
HTML
<div id='container'>
<div class='element' style='left: 0px; top: 0px; -moz-transform: translate(10px, 10px);'>
H
</div>
<div class='element' style='left: 0px; top: 0px; -moz-transform: translate(120px, 10px);'>
He
</div>
<div class='element' style='left: 0px; top: 0px; -moz-transform: translate(230px, 10px);'>
He
</div>
<div class='element' style='left: 0px; top: 0px; -moz-transform: translate(340px, 10px);'>
He
</div>
</div>
If you use CSS3 animations to position stuff on the screen for an element that is position relative
-moz-transform: translate(125px, 5px);
When you ask for the items position using javascript it will report that the item is still
top:0
left:0
For example have a look at the position of elements in this example that uses jQuery Isotope plugin:
http://isotope.metafizzy.co/demos/basic.html
Will I have to use jQuery css() method like this? I don't even know if that will work!
$(element).css('-moz-transform')
UPDATE:
I have posted an example of this on JsFiddle http://jsfiddle.net/uQtur/3/
JS
var elements = $('.element').each(function(i, e){
var el = $(this);
var position = el.position();
var offset = el.offset();
console.log('position', position);
console.log('offset', offset);
var positionLeft = window.getComputedStyle(el.get(0), null).getPropertyValue('left');
var positionTop = window.getComputedStyle(el.get(0), null).getPropertyValue('top');
console.log('computed position left', positionLeft);
console.log('computed position top', positionTop);
var transform = el.css('-moz-transform');
console.log(transform);
// > matrix(1, 0, 0, 1, 340px, 10px)
});
HTML
<div id='container'>
<div class='element' style='left: 0px; top: 0px; -moz-transform: translate(10px, 10px);'>
H
</div>
<div class='element' style='left: 0px; top: 0px; -moz-transform: translate(120px, 10px);'>
He
</div>
<div class='element' style='left: 0px; top: 0px; -moz-transform: translate(230px, 10px);'>
He
</div>
<div class='element' style='left: 0px; top: 0px; -moz-transform: translate(340px, 10px);'>
He
</div>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在选项中设置
itemPositionDataEnabled:true
。然后,您可以使用.data('isotope-item-position')
获取值。请参阅http://isotope.metafizzy.co/docs/options.html#itempositiondataenabled.offset()
和getCompulatedStyle
将不起作用,因为 Isotope 利用 CSS3 转换进行定位。另一个解决方案是使用transformsEnabled: false
禁用转换。In the options set
itemPositionDataEnabled: true
. Then you get the values with.data('isotope-item-position')
. See http://isotope.metafizzy.co/docs/options.html#itempositiondataenabled.offset()
andgetComputedStyle
will not work since Isotope utilizes CSS3 transforms for positioning. Another solution is to disable transforms, withtransformsEnabled: false
.您可以使用 jQuery 的 offset 函数,它会给您相对位置。如果需要准确的位置,则可以获取容器元素的准确位置。因此,您的代码可能如下所示:
You can use jQuery's offset function, which will give you the relative position. If you need the exact position, you can then take the exact position of the container element. So, your code might look like: