Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add media Queries into Jquery

Is it possible to add media jquery's into your jquery code?

I need to reduce "slideWidth: 83," when my screen hits a size of 800px;

$(document).ready(function(){
  $('.slider4').bxSlider({
    slideWidth: 83,
    minSlides: 2,
    maxSlides: 9,
    moveSlides: 1,
    slideMargin: 15
  });
});
like image 823
user2965875 Avatar asked Nov 12 '13 13:11

user2965875


People also ask

Can we use media query in jQuery?

}); Using this solution, regardless of how the browser treats the scrollbar, the jQuery and CSS media query will fire at exactly the same time. Baring in mind there are various wrappers and solutions that you could use, for something so small this was more than enough.

Can you use media queries in JavaScript?

Using Media Queries With JavaScriptMedia queries are used to determine the width and height of a viewport to make web pages look good on all devices (desktops, laptops, tablets, phones, etc).

How do I add multiple media queries?

You may use as many media queries as you would like in a CSS file. Note that you may use the and operator to require multiple queries to be true, but you have to use the comma (,) as the or operator to separate groups of multiple queries. The not keyword can be used to alter the logic as well.


1 Answers

Media queries are supported in js via window.matchMedia

This allows you to create something like

var isPortrait = window.matchMedia("(orientation: portrait)");
if (isPortrait.matches){
    // Screen is portrait
}

or

var smallScreen = window.matchMedia("(max-width: 480px)");
if (smallScreen.matches){
    // Screen is less than 480px
}

Support is good within modern browsers and there is a polyfill available as well but it doesn't support resizing out of the box.

More info – https://developer.mozilla.org/en-US/docs/Web/API/Window.matchMedia

like image 150
Richard Pullinger Avatar answered Sep 27 '22 21:09

Richard Pullinger