I have a video player in a div element. I want to disable everything except the DIV. One way to do it is using lightbox, but I was wondering if I could do it using plain HTML/Javascript.
Nope! Divs can't be disabled. Only form and form elems.
If you have a div, and you want to support click or a key event on that div, then you have to do two things: 1) When you want to disable the div, set its disabled attribute as usual (just to comply with the convention) 2) In your div's click and/or key handlers, check if disabled attribute is set on the div.
Using jQuery The idea is to disable click events inside div with jQuery and CSS. This can be done by setting pointer-events CSS property with the help of jQuery's . addClass() function.
I did simple example for you,
jQuery ;
$(".disable").on('click', function(){
// * = select All, find Div, Not (#video) and edit css opacity
$("*").find('div').not("#video").css('opacity', '0.1');
});
HTML ;
<button class="disable">Disable</button>
<div class="header">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>
<div id="video">
<img src="http://fandomania.com/wp-content/uploads/2012/04/06/anarchy01.jpg">
</div>
<div class="footer">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Css ;
.header{border:1px solid #000;background:#cc0000;color:#fff;}
.footer{border:1px solid #000;background:#cc0000;color:#fff;}
Check FIDDLE
To do this really thoroughly cross-browser, you need an iframe
, which you can create dynamically. Give the iframe
a z-index
higher than anything else on the page except the video div
, make the iframe
the full size of the viewport/page, and then make the video div
a higher z-index
. Now, all clicks except those on the video div
go to the iframe
, which presumably ignores them. You can also use opacity on the iframe if you want to "dim out" the rest of the page.
Very roughly:
function maskAllExcept(div) {
var iframe = document.createElement('iframe');
iframe.style.position = "absolute";
iframe.style.left = iframe.style.right = iframe.style.top = iframe.style.bottom = "0";
iframe.style.zIndex = 1000;
div.style.zIndex = 1001;
document.body.appendChild(iframe);
}
Disable everything except an element and its descendants with Pure CSS. Let's do it with a common HTML dialog
where this behavior could be necessary (you can use a div
if you like)
We just need to add a class to avoid pointer events in all the body
except our dialog
and its descendants.
body.only-dialog *:not(dialog *) { /* not supported yet */
pointer-events: none;
}
Because, :not
only supports a single simple selector, we have to do it like this:
body.only-dialog * {
pointer-events: none;
}
body.only-dialog dialog * {
pointer-events: all;
}
https://jsfiddle.net/bmntvLfs/
Hope this help to future generations :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With