How to scroll the horizontal scrollbar in an iFrame from the parent frame?

Posted by Mohammad on Stack Overflow See other posts from Stack Overflow or by Mohammad
Published on 2010-05-25T09:40:54Z Indexed on 2010/05/25 9:51 UTC
Read the original article Hit count: 312

Is there any cross browser way to scroll the horizontal scroll bar of an IFrame with super wide content with Javascript inside the parent frame. Also I need it to be attached to the mouse wheel event.

This is what I have so far, it's a bit copy and paste at the moment and doesn't work unfortunately.

//var myIframe = document.getElementById('iframeWithWideContent');
//myIframe.onload = function (myIframe) {
  var mouseWheelEvt = function (e){
    var event = e || window.event;
    if (document.body.doScroll){
        document.body.doScroll(event.wheelDelta>0?"left":"right");
    }else if ((event.wheelDelta || event.detail) > 0){
        document.body.scrollLeft -= 10;
    }else{
        document.body.scrollLeft += 10;
    }
    return false;
  }
  if ("onmousewheel" in document.body){
    document.body.onmousewheel = mouseWheelEvt;
  }else{
    document.body.addEventListener("DOMMouseScroll", mouseWheelEvt);
  }
//}?

I probably should uncomment that code, replace document.body with myIframe though I wouldn't know what I'm doing wrong.

Demo on JSBIN
link fixed

Any help from you JavaScript Lords would be very appreciated. Thank you!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about iframe