9

How can I prevent an HTML page from scrolling when arrow keys are pressed if an iframe inside it is focused?

I'm gettting this error in Chrome

The iframe is focused, I know its focused. The parent scrolls anyway.

Dharman
  • 30,962
  • 25
  • 85
  • 135
John Stimac
  • 5,335
  • 8
  • 40
  • 60
  • Do you have control over the content in the iframe? –  Jul 08 '10 at 21:12
  • yes, and its on the same domain – John Stimac Jul 08 '10 at 23:59
  • IE 6+, FF3+, Chrome: all of them does what you've just said by default. I click on the iframe and press arrow down. The iframe scrolls the page doesn't. What do you want? – gblazex Jul 14 '10 at 01:40
  • `not tested yet solution:` CSS [overscroll-behavior](https://developer.mozilla.org/en-US/docs/Web/CSS/overscroll-behavior) - not supported by IE, Safari(Mac/iOS), but seems that WebKit will implement it in future – rejnok Mar 09 '21 at 10:09

5 Answers5

16

The following code inside the iframe document will prevent it from scrolling:

document.onkeydown = function(evt) {
    evt = evt || window.event;
    var keyCode = evt.keyCode;
    if (keyCode >= 37 && keyCode <= 40) {
        return false;
    }
};
Tim Down
  • 318,141
  • 75
  • 454
  • 536
1

This code does the trick:

JavaScript

<script type="text/javascript">
  function focusOnIframe(iFrameID) {
    if (frames[iFrameID]!=undefined)
      frames[iFrameID].focus(); // Works in all browser, except Firefox
    else
      document.getElementById(iFrameID).focus();  // Works in Firefox
  }
</script>

HTML (example)

<input type="button" id="setfocus" value="Set focus" onclick="focusOnIframe('myiframe')" />

<p>Bla<br />Bla<br />Bla<br />Bla<br />Bla<br /></p>  <!-- Just some filler -->

<iframe id="myiframe" src="yourpage.html"></iframe>

<p>Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br />Bla<br /></p>  <!-- Just some filler -->

I've tested it in Firefox 3.6.6, Iron 5.0.380, Opera 10.60, IE 6 and IE 8.

Gert Grenander
  • 16,866
  • 6
  • 40
  • 43
  • you understand that the question involves scrolling, right? not focusing? – John Stimac Jul 15 '10 at 02:10
  • The problem is that your focus isn't working, correct? I.e. when the iframe is focused and you use the arrow keys, it scrolls the parent. The above code sets the focus and the parent doesn't scroll. – Gert Grenander Jul 15 '10 at 02:39
  • And when you use up and down key, the parent scrolls, correct? – Gert Grenander Jul 15 '10 at 09:50
  • From skimming over the text and having a similar problem: I think the issue is that scrollbars on a page element "spill over" to the body. When you reach the bottom of the stuff you're scrolling, the body scrolls next. (Which is completely counter-intuitive imho). – Mantriur Aug 22 '15 at 15:49
  • This don't solve anything, problem still! – rejnok Mar 05 '21 at 18:11
0

Answer for someone with no access to source of page loaded to the iframe:

I resolved the problem with scroll event propagating to parent element (main html document) by dividing whole page to 2 divs with CSS position:fixed, 1st with whole html page (parent) and 2nd with iframe only, like dividing screen to halfs and seeing 2 browser windows on split screen. Or more flexible upgrade: Make the 1st div's CSS width:100vw;height:100vh; (+ finetune CSS top|left|bottom|right) and 2nd div resizable by CSS resize (resize:vertical|horizontal|both) or any smarter JS code (eg. jQueryUI)

rejnok
  • 343
  • 3
  • 8
-1

This works except IE:

window.top.document.onkeydown = function(evt) {
    evt = evt || window.event;
    var keyCode = evt.keyCode;
    if (keyCode >= 37 && keyCode <= 40) {
        return false;
    }
};
gblazex
  • 49,155
  • 12
  • 98
  • 91
-1

You can prevent scrolling with the arrow keys using the following code:

var objImage= null;

            function getKeyAndMove(e){

            var idtestor = document.getElementById('idPlacer2').value; 
                objImage=  document.getElementById(idtestor);               
                objImage.style.position='relative';


                var key_code=e.which||e.keyCode;
                switch(key_code){
                    case 37: //left arrow key
                        moveLeft();
e.preventDefault(); 
                        break;
                    case 38: //Up arrow key
                        moveUp();
e.preventDefault(); break;
                        break;
                    case 39: //right arrow key
                        moveRight();
e.preventDefault(); 
                        break;
                    case 40: //down arrow key
                        moveDown();
e.preventDefault(); break;
                        break;                      
                }
            }
            function moveLeft(){
                objImage.style.left=parseInt(objImage.style.left)-5 +'px';
window.scrollBy(-5, 0);
            }
            function moveUp(){
                objImage.style.top=parseInt(objImage.style.top)-5 +'px';
window.scrollBy(0, -5);
            }
            function moveRight(){
                objImage.style.left=parseInt(objImage.style.left)+5 +'px';
window.scrollBy(5, 0);
            }k`a`
            function moveDown(){
                objImage.style.top=parseInt(objImage.style.top)+5 +'px';
window.scrollBy(0, 5);
            }
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
nick
  • 1