Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Given an HTML DOM ID, how to get an element's position relative to the window in JavaScript/JQuery?(给定HTML DOM ID,如何在JavaScript / JQuery中获取元素相对于窗口的位置?)

This is not the same as relative to the document nor offset parent since the element may be inside an iframe or some other elements.(这与相对于文档和偏移父项不同,因为该元素可能位于iframe或其他元素内。) I need to get the screen location of the element's rectangle (as in position and dimension) as it is currently being displayed.(我需要获取元素矩形的屏幕位置(如位置和尺寸),因为它当前正在显示。) Negative values are acceptable if the element is currently off-screen (have been scrolled off).(如果元素当前处于屏幕外(已滚动关闭),则可以接受负值。)

This is for an iPad (WebKit / WebView) application.(这适用于iPad(WebKit / WebView)应用程序。)

Whenever the user taps on a special link in an UIWebView , I am supposed to open a popover view that displays further information about the link.(每当用户点击UIWebView的特殊链接时,我应该打开一个弹出视图,显示有关该链接的更多信息。) The popover view needs to display an arrow that points back to the part of the screen that invokes it.(弹出视图需要显示一个箭头,该箭头指向调用它的屏幕部分。)   ask by adib translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
524 views
Welcome To Ask or Share your Answers For Others

1 Answer

Initially, Grab the .offset position of the element and calculate its relative position with respect to window(最初,抓住元素的.offset位置并计算其相对于窗口的相对位置)

Refer :(参考 :)


1. offset(抵消)
2. scroll(滚动)
3. scrollTop(3. scrollTop)

You can give it a try at this fiddle(你可以尝试一下这个小提琴)

Following few lines of code explains how this can be solved(以下几行代码解释了如何解决这个问题)

when .scroll event is performed, we calculate the relative position of the element with respect to window object(当执行.scroll事件时,我们计算元素相对于window对象的相对位置)

$(window).scroll(function () {
    console.log(eTop - $(window).scrollTop());
});

when scroll is performed in browser, we call the above event handler function(当在浏览器中执行滚动时,我们调用上面的事件处理函数)

code snippet(代码段)


 function log(txt) { $("#log").html("location : <b>" + txt + "</b> px") } $(function() { var eTop = $('#element').offset().top; //get the offset top of the element log(eTop - $(window).scrollTop()); //position of the ele wrt window $(window).scroll(function() { //when window is scrolled log(eTop - $(window).scrollTop()); }); }); 
 #element { margin: 140px; text-align: center; padding: 5px; width: 200px; height: 200px; border: 1px solid #0099f9; border-radius: 3px; background: #444; color: #0099d9; opacity: 0.6; } #log { position: fixed; top: 40px; left: 40px; color: #333; } #scroll { position: fixed; bottom: 10px; right: 10px; border: 1px solid #000; border-radius: 2px; padding: 5px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="log"></div> <div id="element">Hello <hr>World</div> <div id="scroll">Scroll Down</div> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...