How to make IFRAME listen to scroll events as parent when click event within parent iframe is triggered
I have two iframes in an html page
<table border="1" width="100%" height="100%">
<tr>
<th><iframe id="i1" width="100%" height="100%" src="/wordpress"></iframe></th>
<th><iframe id="i2" width="100%" height="100%" src="/wordpress"></iframe></th>
</tr>
</table>
I am giving click event to "a" tag to change the href so that when any link within that iframe is clicked and src of iframe with id "i1" changes the src of second iframe also change subsequently and both iframe have the same page view.
$('a').click(function(e){
var id = $(this).attr('id');
var href = $(this).attr('href');
var ahash={
'id':id,
'href':href
};
if (getFrameElement().id=="i1")
window.parent.document.Aaddevent(e, ahash);
});
The href is changed by this
document.sendevent=function(e, ahash){
if (ahash.id!=undefined) {
$('#i2', window.parent.document).contents().find('#'+ahash.id).trigger(e);
} else {
$('#i2', window.parent.document).attr('src', ahash.href);
}
};
This works fine. Now my question is i have given scroll event to iframe with id "i1" so that when iframe is scrolled the other iframe also gets scrolled automatically. This is working well when the page开发者_如何学编程 is loaded but when click event is triggered and the page view of both iframe changes the scroll event is not working.
function getFrameTargetElement(oI)
{
var lF = oI.contentWindow;
if(window.pageYOffset==undefined)
{
lF= (lF.document.documentElement) ? lF.document.documentElement : lF=document.body;
}
//- return computed value
return lF;
}
//- get frame target elements
var oE1 = getFrameTargetElement( document.getElementById("i1") );
var oE2 = getFrameTargetElement( document.getElementById("i2") );
//- on source scroll
oE1.onscroll = function (e) {
var scrollTopValue;
var scrollLeftValue;
//- evaluate scroll values
if(window.pageYOffset!=undefined)
{
scrollTopValue = oE1.pageYOffset;
scrollLeftValue = oE1.pageXOffset;
}
else
{
scrollTopValue = oE1.scrollTop;
scrollLeftValue = oE1.scrollLeft;
}
//- mimic scroll
oE2.scrollTo( scrollLeftValue, scrollTopValue);
}
I am not getting what i need to change so that when click event is trigged on one iframe and the src of both iframe changes the scroll event should also work alongwith like it worked when page loaded initially.
After the pages load the new url, you have to re-bind your scroll event handler. Listen to the iframe's onload
event, and when that fires, re-bind the scroll handlers.
It looks like you're using jQuery. Here's how to do it:
$("#i1").load(function ()
{
var oE1 = getFrameTargetElement( document.getElementById("i1") );
var oE2 = getFrameTargetElement( document.getElementById("i2") );
//- on source scroll
oE1.onscroll = function (e) {
var scrollTopValue;
var scrollLeftValue;
//- evaluate scroll values
if(window.pageYOffset!=undefined)
{
scrollTopValue = oE1.pageYOffset;
scrollLeftValue = oE1.pageXOffset;
}
else
{
scrollTopValue = oE1.scrollTop;
scrollLeftValue = oE1.scrollLeft;
}
//- mimic scroll
oE2.scrollTo( scrollLeftValue, scrollTopValue);
}
}
精彩评论