How do I get the offset().top value of an element without using jQuery?

前端 未结 6 979
Happy的楠姐
Happy的楠姐 2020-12-01 06:05

I\'m programming a single-page application using the Angular framework. I\'m new to it. I\'ve read this guide to help me understand the fundamental differences between jQuer

6条回答
  •  温柔的废话
    2020-12-01 06:38

    Here is a function that will do it without jQuery:

    function getElementOffset(element)
    {
        var de = document.documentElement;
        var box = element.getBoundingClientRect();
        var top = box.top + window.pageYOffset - de.clientTop;
        var left = box.left + window.pageXOffset - de.clientLeft;
        return { top: top, left: left };
    }
    

提交回复
热议问题