The point of this code is to mask a page where the user is editing the page's 'body' content. This is in a CMS. The rest of the page is the website template (i.e. header, index bar etc...). I've covered resizing the window, and the DIV itself growing larger.
I'd love to know if there is a more efficient way to do this. I've used a DIV position: fixed as a mask under the editable DIV but some of our templates have irregular background colours (or images) and I really want to have a transparent editing area so that the experience is as close as possible to the end result.
If it's not apparent, this code creates a DIV whose borders cover the space that the editable DIV does not take up on the document. As the page resizes or the editable DIV changes size, these borders need to change size.
NOTE: The outer container may have overflow: hidden applied (thus the need to append the mask to the body element). The intention of the mask is to prevent clicking on any other area of the page other than the area that is being edited.
var content_builder_element = $("#content_builder");
var mask = $('<div class="content_builder_mask"></div>');
buildMask(content_builder_element, mask, false);
$('body').append(mask);
$(window).resize(function() {
buildMask(content_builder_element, mask, true);
});
window.setInterval(function() {
buildMask(content_builder_element, mask, true);
}, 100);
function buildMask(reference, mask, resize) {
var offset = reference.offset();
var height = reference.outerHeight(true);
var width = reference.outerWidth(true);
// We don't want the width of the masks to mess with the document width
if (resize) {
mask.css('borderRightWidth', 0);
mask.css('borderBottomWidth', 0);
}
var document_width = $(document).width();
var document_height = $(document).height();
mask.width(width);
mask.height(height);
mask.css('borderTopWidth', offset.top);
mask.css('borderRightWidth', document_width - offset.left - width);
mask.css('borderBottomWidth', document_height - offset.top - height);
mask.css('borderLeftWidth', offset.left);
};
body {
background-color: orange;
}
.content_builder_mask {
border: 0px solid rgba(0, 0, 0, 0.6);
position: absolute;
top: 0;
left: 0;
}
#somecontainer {
margin: 50px;
overflow: hidden;
}
#content_builder {
min-height: 200px;
position: relative;
z-index: 500;
}
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div id="somecontainer">
<div id="content_builder" contenteditable="true">
</div>
</div>
</body>
</html>