确定元素的offsetTop的,并设置它的其他元素元素、它的、offsetTop

2023-09-13 02:51:12 作者:垂涎自由

有是项目的列表。如果点击某个项目anoter视图显示该项目旁边。我想这个观点有相同的offsetTop的项目。我知道如何在jQuery的做到这一点,但无法找到纯angularjs的解决方案。这是我试过到目前为止

There is a list of items. If an item is clicked anoter view is revealed beside the item. I want this view to have the same offsetTop as the item. I know how to do this in jQuery, however cannot find a solution in pure angularjs. This is what I tried so far

top = angular.element('#user'+user.id).prop('offsetTop');
angular.element('#feed-details').css('margin-top', top);

这没有任何影响。任何想法如何在angularjs实现这一目标?

This has no effect. Any ideas how to achieve this in angularjs?

推荐答案

长度都必须有一个单位。 的offsetTop 是一个无单位数字。所以,只需要添加PX来了。

Lengths must have a unit. offsetTop is a unitless number. So just add "px" to it.

这是说,你可以只是做在香草的JavaScript :

That said, you could just do it in Vanilla JavaScript:

document.getElementById('feed-details').style.marginTop =
            document.getElementById('user'+user.id).offsetTop+"px";