Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Iframe from preventing scrolling of parent document?

Tags:

I seem to have the opposite problem of everyone else with iframes and scrolling. I need the iframe (contains a youtube video) to NOT prevent scrolling of the main document. If I hover my mouse over it, the page won't scroll with the scroll wheel, and according to the latest chrome canary simulation of touch devices, I can't put my finger on the frame and scroll the main document either. Any way to stop this? My CSS is below:

.GalleryVideoWrapper { position: relative; padding-bottom: 56.25%; /* 16:9 */ padding-top: 25px; height: 0; width:95%; margin:auto; display:block;  }  .GalleryVideoWrapper iframe { position: absolute; top: 0; left: 0; width: 100%; height: 100%;  } 
like image 699
photocode Avatar asked Nov 23 '14 02:11

photocode


People also ask

How can I disable clicks but still allow scrolling in an iframe?

1 Answer. Show activity on this post. If you don't want the contents of the iframe to be interactable by the user, you can disable pointer-events on it. But as you want it to be scrollable, just put a full sized iframe in a smaller div with overflow: scroll.

How do I turn off scrolling in iframe?

Add scrolling="no" attribute to the iframe.


2 Answers

The question is unclear, so I went into some detail below on various ways to achieve this effect and how it works.

If you don't need to interact with the iframe, the quick and dirty solution is to use pointer-events: none;. This will put the iframe on the page, and not allow it to scroll. However, it also does not allow you to click on it. This obviously won't work for a YouTube video, but it is important to know this is an option.

If you need to interact with the iframe, either to play a video or click a link, all you need to do is make sure the iframe is large enough to display the full contents. I'm unsure what specific problem OP was encountering as we don't have their HTML, but if you scroll and the iframe is not also trying to scroll, it will not prevent the parent from scrolling.

Basically, if you have your cursor over an iframe and you scroll, the iframe will receive the event first. If it does not need to scroll (either it can't or it has already reached the bottom of the iframe) the event will be propagated to the parent.

Finally, if you have an iframe that you need to be scrollable, but you want to scroll the parent while the cursor is on the iframe, you are out of luck. There is no way to inform the iframe that sometimes the user wants to scroll the whole page. This is simply how iframes work. You can either remove the cursor from the iframe to scroll, or scroll to the bottom of the iframe and continue down the page.

Using a YouTube video and the CSS in the question, I've included a demo for you to see. I also included two identical iframes that are scrollable and applied pointer-events: none; to one to demonstrate how it works.

.tall {    height: 1500px;  }    .GalleryVideoWrapper {    position: relative;    padding-bottom: 56.25%;    /* 16:9 */    padding-top: 25px;    height: 0;    width: 95%;    margin: auto;    display: block;  }    .GalleryVideoWrapper iframe {    position: absolute;    top: 0;    left: 0;    width: 100%;    height: 100%;  }    .scrolling-iframe {    margin-top: 35px;    display: inline-block;    height: 500px;  }    .no-scroll {    pointer-events: none;  }
<div class="tall">    <div class="GalleryVideoWrapper">      <iframe width="560" height="315" src="https://www.youtube.com/embed/hzB53YL78rE?rel=0" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>    </div>    <iframe class="scrolling-iframe" src="https://www.wikipedia.org/" frameborder="1"></iframe>    <iframe class="scrolling-iframe no-scroll" src="https://www.wikipedia.org/" frameborder="1"></iframe>  </div>
like image 120
amflare Avatar answered Oct 15 '22 18:10

amflare


There used to be a scrolling attribute, but it is deprecated in html5. try this:

iframe {     overflow: hidden; } 

Don't forget to set your width and height somewhere!

If you wanted to try the iframe scrolling attribute, you could like this:

<iframe src="blah.html" width="200" height="200" scrolling="no"></iframe> 

See working example here: http://jsfiddle.net/4dt4zhwt/1/

like image 32
superUntitled Avatar answered Oct 15 '22 17:10

superUntitled