Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - If screen is less than specified width (responsive)

How can I make an alert popup if the width of the page is less than 1200px, and made responsive?

Thanks!

like image 503
Harry Avatar asked Dec 12 '22 16:12

Harry


1 Answers

You can use something like the breakpoints module. Then you setup a breakpoint to trigger at 1200px and show a dialog and either add a css class that changes the layout, or use straight javascript to make the changes.

breakpoints(1200, function(oldPoint, newPoint) {
  alert('The screen width just changed');
});

if you just wanted native jQuery:

$(window).resize(function() {
  var width = $(window).width();
  if (width < 1200){
    alert('Your screen is too small');
  }
});

For completeness, heres the CSS media query (still doesn't take care of the alert, but can help with making the website "responsive").

/* some normal style */
.myclass {
  font-size: 22pt;
}

/* alter the style when the screen's smaller */
@media screen and (max-width: 1200px) {
  .myclass {
    font-size: 18pt;
  }
}
like image 87
Brad Christie Avatar answered Jan 11 '23 13:01

Brad Christie