我想在滚动页面时更改用户界面,为此我想获取 div 元素的位置(顶部和底部),我如何在 angularjs 中执行此操作?在下面的代码中,我如何获得 top_position?
var w=angular.element($window);
w.bind('scroll', function(){
var top_div=angular.element($('#top-div'));
// now here what function should i use ?
console.log("the top of the div having id top-div:"+**top_position**);
});
最佳答案
您可以在指令的链接函数中获取 DOM 元素,并在任何需要的地方使用它
scope: {...},
restrict: 'AE',
controller: function() {},
link: function($scope, elem, attrs) {
var el = elem[0]; // elem - jQLite element, el - native DOM element
console.log(el.getBoundingClientRect()); // the bounding rect of the element
}
关于javascript - angularjs获取元素当前位置的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100692/