Media Queries for Java Script - What is the error in this syntax?

Asked

Viewed 205 times

0

This is the first time I try to apply a rule of media queries in java script and, as a beginner, I believe something in this syntax is wrong, since the only function that is working is the second (with Visible: 3) and in all viewports.

var mq = window.matchMedia('@media all and (max-width: 768px)');
if (mq.matches) {
    $("#carrossel").jCarouselLite({
        btnPrev: ".anterior"
        , btnNext: ".proximo"
        , visible: 1
        , auto: 2000
        , speed: 1000
    });

} else {
    $("#carrossel").jCarouselLite({
        btnPrev: ".anterior"
        , btnNext: ".proximo"
        , visible: 3
        , auto: 2000
        , speed: 1000
    });
}

  • Will, did you copy this from jCarousel’s documentation or do it yourself?

2 answers

2


  • Thank you so much, Tiago! Solved my problem!

2

I believe if you remove the @media all and will work.

var mq = window.matchMedia('(max-width: 768px)');
if (mq.matches) {
  console.log('menor');

} else {
 console.log('maior');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Functional example in Jsfiddle.

Another option would be to use the .width() jQuery.

if ($(window).width() > 768) {
  alert('menor');

} else {
  alert('maior');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Example in Jsfiddle.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.