Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I apply 'offset' to an element inside an iframe?

I want to navigate to a div inside an iframe using jQuery.animate from a link outside the iframe. This is the Code I use:

function scrollToAnchorIframe(aid){
  var aTag = window.frames['myFrame'].document.getElementById(aid);
  $('html,body').animate({scrollTop: aTag.offset().top - 62},'slow');
}

However, it doesn't work, logging the error "Object [object HTMLElement] has no method 'offset'". Is there a way to get the offset of the id to make it work?

Update (Solved): That's the code I'm using now:

function scrollToAnchorIframe(aid){
  var aTag = window.frames['myFrame'].document.getElementById(aid);
  jQuery('html,body').animate({scrollTop: $(aTag).offset().top + $("#myFrame").offset().top - 62},'slow');
}
like image 675
Georg Avatar asked Oct 04 '22 20:10

Georg


1 Answers

aTag is DOM element , make it jQuery object

$('html,body').animate({scrollTop: $(aTag).offset().top - 62},'slow');
like image 175
Mohammad Adil Avatar answered Oct 11 '22 02:10

Mohammad Adil