javascript - If Statment depending on screen width -


i have created js if-statment, should detect screen width , change css properties depending on screen width. if screen wider 700px, #header_menu should extended. if resolution below 700, #menu should extended (by clicking trigger).

all in function working, first time resize window. not sure mistake ...

thanks ;)

$(document).ready(function(){ $(window).on('load resize', function checkposition(){         if (window.matchmedia('(max-width: 700px)').matches) {             $("#menu_trigger").click(function(){                 $("#menu").css('width', '90%');                 $("#closer, #header").css('margin-left', '90%');                 $("#closer, #header").css('margin-right', '-90%');             });         } else {             $("#menu_trigger").click(function(){                 $("#header_menu").css('width', 'auto');             });     } }); 

});

engages resize event of window function , try this

$(document).load($(window).bind("resize", checkposition));  function checkposition(){     if (window.matchmedia('(max-width: 700px)').matches) {         $("#menu_trigger").click(function(){             $("#menu").css('width', '90%');             $("#closer, #header").css('margin-left', '90%');             $("#closer, #header").css('margin-right', '-90%');         });     } else {         $("#menu_trigger").click(function(){             $("#header_menu").css('width', 'auto');         });     } } 

every time page resized, function called


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -