Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable scrolling on body in overlay modal view

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

like image 748
iOSGeek Avatar asked Feb 27 '14 20:02

iOSGeek


2 Answers

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.

like image 160
Adam Avatar answered Oct 12 '22 02:10

Adam


already discussed here: Prevent BODY from scrolling when a modal is opened

it is using bootstrap, but the same solution can still work. the second answer is a bit better than the accepted one. it suggests toggling a class on the body that makes the body overflow: hidden; when the modal is open.

like image 27
jakealbaugh Avatar answered Oct 12 '22 03:10

jakealbaugh