I am using this script to display and hide a modal view, but I want to disable scrolling on body once the modal view is opened and disable it when it is closed.
I tried to modify the JS code but it works but it breaks the opening animation. here is the code modified:
(function() {
var triggerBttn = document.getElementById( 'trigger-overlay' ),
overlay = document.querySelector( 'div.overlay' ),
bodyTag = document.querySelector( 'body' ),
closeBttn = overlay.querySelector( 'button.overlay-close' );
transEndEventNames = {
'WebkitTransition': 'webkitTransitionEnd',
'MozTransition': 'transitionend',
'OTransition': 'oTransitionEnd',
'msTransition': 'MSTransitionEnd',
'transition': 'transitionend'
},
transEndEventName = transEndEventNames[ Modernizr.prefixed( 'transition' ) ],
support = { transitions : Modernizr.csstransitions };
function toggleOverlay() {
if( classie.has( overlay, 'open' ) ) {
classie.remove( overlay, 'open' );
classie.add( overlay, 'close' );
var onEndTransitionFn = function( ev ) {
if( support.transitions ) {
if( ev.propertyName !== 'visibility' ) return;
this.removeEventListener( transEndEventName, onEndTransitionFn );
}
classie.remove( overlay, 'close' );
classie.remove( bodyTag, 'noscroll' );
};
if( support.transitions ) {
overlay.addEventListener( transEndEventName, onEndTransitionFn );
}
else {
onEndTransitionFn();
}
}
else if( !classie.has( overlay, 'close' ) ) {
classie.add( overlay, 'open' );
classie.add( bodyTag, 'noscroll' );
}
}
triggerBttn.addEventListener( 'click', toggleOverlay );
closeBttn.addEventListener( 'click', toggleOverlay );})();
Original Demo : jsfiddle
First add jquery. Download from https://jquery.com and add it to your document with <script src="jquery.min.js"></script>
.
Then, at the bottom of the document (your document with your animation) add the following snip:
<script>
$(document).ready(function() {
$('#trigger-overlay').click(function() {
$('html').css('overflow', 'hidden');
$('body').bind('touchmove', function(e) {
e.preventDefault()
});
});
$('.overlay-close').click(function() {
$('html').css('overflow', 'scroll');
$('body').unbind('touchmove');
});
});
</script>
I have tried this with code from https://github.com/codrops/FullscreenOverlayStyles and animation Huge inc
. This will prevent scrolling of text when animation is open.
Updated my answer, first answer did allow scrolling on touch devices.