First of all, I am not a javascript expert. I'm going crazy, trying to figure out how to conditionally execute specific javascript. I use jQuery to completely focus my block on the browser page, but only if the screen size is more than 480 pixels (in another sense, I do not want this script to work on smartphones). I am using a CSS request to request my request. The fact is that this script works fine on all smartphones, Safari 5+, IE10, Firefox 13. BUT THIS DOES NOT WORK IN IE6-9 and Opera 12 (As far as I understand, they do not support transitions) CAN ANY PLEASE HELP ME PICTURE THAT I AM DEFECTED? And if there is a better way to do this? (I tried the @media request in CSS, but the script continues to work no matter what) ... I would really appreciate help.
<script> if (matchMedia('only screen and (max-device-width:800px) and ' + '(orientation: portrait)').matches) { // smartphone/iphone... maybe run some small-screen related dom scripting? event.preventDefault(); } else{ //Absolute Content Center function CenterItem(theItem){ var winWidth=$(window).width(); var winHeight=$(window).height(); var windowCenter=winWidth/2; var itemCenter=$(theItem).width()/2; var theCenter=windowCenter-itemCenter; var windowMiddle=winHeight/2; var itemMiddle=$(theItem).height()/2; var theMiddle=windowMiddle-itemMiddle; if(winWidth>$(theItem).width()){ //horizontal $(theItem).css('left',theCenter); } else { $(theItem).css('left','0'); } if(winHeight>$(theItem).height()){ //vertical $(theItem).css('top',theMiddle); } else { $(theItem).css('top','0'); } } $(document).ready(function() { CenterItem('.content'); }); $(window).resize(function() { CenterItem('.content'); }); } //end of "else" (normal execution) </script>
source share