javascriptjqueryfancybox-2matchmedia

JS matchMedia if-statement


I'm trying to trigger my fancybox for viewports >=768px and trigger my swipebox for viewports <768px. Unfortunately neither one opens now... what am I missing? Thanks for your help! I have tons of CSS thus I'm just adding the JS (I'm quite sure it's a syntax error...).

// JavaScript Document

$(document).ready(function() {
if (window.matchMedia("(min-width: 768px)").matches) {
  
	$(".fancybox").fancybox({
		//width: '70%',
		//height: '70%',
      helpers: {
        overlay: {
          locked: false
        }
      },
    'beforeLoad': function(){
      disable_scroll();
        },
     'afterClose': function(){
       enable_scroll();
      }
		
	});
	
});
var keys = [37, 38, 39, 40];

    function preventDefault(e) {
      e = e || window.event;
      if (e.preventDefault) e.preventDefault();
      e.returnValue = false;  
    }

    function keydown(e) {
        for (var i = keys.length; i--;) {
            if (e.keyCode === keys[i]) {
                preventDefault(e);
                return;
            }
        }
    }

    function wheel(e) {
      preventDefault(e);
    }

    function disable_scroll() {
      if (window.addEventListener) {
          window.addEventListener('DOMMouseScroll', wheel, false);
      }
      window.onmousewheel = document.onmousewheel = wheel;
      document.onkeydown = keydown;
    }

    function enable_scroll() {
        if (window.removeEventListener) {
            window.removeEventListener('DOMMouseScroll', wheel, false);
        }
        window.onmousewheel = document.onmousewheel = document.onkeydown = null;  
    }
} else {
  ;( function( $ ) {

	$( '.swipebox' ).swipebox();

} )( jQuery );
}


Solution

  • Try this. I'll update the fiddle also.

    Fiddle here.

    $(document).ready(function() {
    if (window.matchMedia("(min-width: 768px)").matches) {
    
      $(".fancybox").fancybox({
        //width: '70%',
        //height: '70%',
          helpers: {
            overlay: {
              locked: false
            }
          },
        'beforeLoad': function(){
          disable_scroll();
            },
         'afterClose': function(){
           enable_scroll();
          }
    
      });
    
    
    var keys = [37, 38, 39, 40];
    
        function preventDefault(e) {
          e = e || window.event;
          if (e.preventDefault) e.preventDefault();
          e.returnValue = false;  
        }
    
        function keydown(e) {
            for (var i = keys.length; i--;) {
                if (e.keyCode === keys[i]) {
                    preventDefault(e);
                    return;
                }
            }
        }
    
        function wheel(e) {
          preventDefault(e);
        }
    
        function disable_scroll() {
          if (window.addEventListener) {
              window.addEventListener('DOMMouseScroll', wheel, false);
          }
          window.onmousewheel = document.onmousewheel = wheel;
          document.onkeydown = keydown;
        }
    
        function enable_scroll() {
            if (window.removeEventListener) {
                window.removeEventListener('DOMMouseScroll', wheel, false);
            }
            window.onmousewheel = document.onmousewheel = document.onkeydown = null;  
        }
    } else {
      ( function( $ ) {
    
        $( '.swipebox' ).swipebox();
    
      } )( jQuery );
    }
    });