-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiframeScrollPosition.js
More file actions
22 lines (22 loc) · 1.02 KB
/
iframeScrollPosition.js
File metadata and controls
22 lines (22 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Use this script in the page that is hosting the iframe.
// It will send the scroll position of the parent frame to the iframe content window.
// Use `iframeScrollPosition.contentWindow.js` in the page contained within your iframe to receive the scroll position.
(function($) {
$(document).ready(function () {
var iframes = $("iframe");
iframes.each(function (index, iframeElem) {
var iframe = $(iframeElem);
var sendScrollPosition = function () {
var data = {
left: $(window).scrollLeft(),
top: $(window).scrollTop(),
offsetLeft: $(window).scrollLeft() - iframe.offset().left,
offsetTop: $(window).scrollTop() - iframe.offset().top
};
iframeElem.contentWindow.postMessage("parentScrollPosition:" + JSON.stringify(data), "*");
};
iframe.on('load', sendScrollPosition);
$(window).scroll(sendScrollPosition);
});
});
})(jQuery);