function _resize() { var width = $("body").width(); if (width === 964) { $("#Equalizer").css("margin", "0px"); } else $("#Equalizer").css("margin", "auto"); } $(document).ready(function () { _resize(); $(window).resize(_resize) }); 

it is necessary to keep track of the size of the body each time you change the window size and set the margin according to the case. But the fact is that the event does not work.

 #Equalizer { margin:auto; width: 60%; font-family:Consola Mono; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link href="Style.css" type="text/css" rel="stylesheet" /> <script type="text/javascript" src="Scripts/jquery-2.1.4.min.js"></script> <script type="text/javascript"> function _resize() { var width = $("body").width(); if (width === 964) { $("#Equalizer").css("margin", "0px"); } else $("#Equalizer").css("margin", "auto"); } $(window).ready(function () { _resize(); $(document).resize(_resize) }); </script> </head> <body> <div id="Equalizer"> <p> С другой стороны постоянное информационно-пропагандистское обеспечение нашей деятельности представляет собой интересный эксперимент проверки дальнейших направлений развития. Значимость этих проблем настолько очевидна, что дальнейшее развитие различных форм деятельности представляет собой интересный эксперимент проверки систем массового участия. Значимость этих проблем настолько очевидна, что дальнейшее развитие различных форм деятельности играет важную роль в формировании систем массового участия. Товарищи! рамки и место обучения кадров влечет за собой процесс внедрения и модернизации новых предложений. Значимость этих проблем настолько очевидна, что начало повседневной работы по формированию позиции представляет собой интересный эксперимент проверки модели развития. </p> </div> </body> </html> 

  • because you call _resize before being transmitted - Grundy
  • @Grundy sorry typo, but still does not work - Alex
  • Than css did not please you? - Invision

1 answer 1

because you call _resize before being passed as a parameter, you need to pass without ()

 $(window).resize(_resize) 

UPDATE

To check whether the handler is running or not, add output to the function in the function, for example

 console.log('resize func'); 

In addition, inside the function you have a tough condition.

  if (width === 964) { 

This means that margin:0 will be set only in case of exact size matching. Perhaps you would like to use it here <

UPDATE2
In the markup that you enclosed you confused the document and window.

 $(document).ready(function () { _resize(); $(window).resize(_resize) }); 

Example - in the console you can see that the function is being called.

  • Vseravno not working - Alex
  • @ Alexey see the updated answer - Grundy
  • I don’t know why, but the event isn’t triggered, there should be at least two entries in the console, and one there - Alex
  • @ Alexey, where and how did you check? - Grundy
  • I didn’t understand why, of course, but I hooked up google'vsky jquery, instead of the proposed microsoft and everything earned - Alex