简体   繁体   中英

Javascript and media queries

I want to add some kind of media query to the JS below so on mobile or screens <767px the height only animates to 280px. Also would be interested if this can be achieved with CSS

Javascript

   $("#open").click(function(e){
       e.preventDefault();
        $("div#panel").animate({height: "337px"},"medium");
    });

Many thanks

There's a great CSS3 solution that is described in this article: http://webdesignerwall.com/tutorials/adaptive-mobile-design-with-css3-media-queries

This technique uses the CSS media query: @media screen and (max-width: 767px) { ... }

Keep in mind that you might have to get a little creative in your solution if you choose to use this method. Let me know if this is enough information, or if you need me to detail how you could use this to solve your problem.

========== Edit

There's probably a much better way to go about this, but here's what I was thinking...

CSS:

#panel {
  z-index: 0;
}

@media screen and (max-width: 767px) {
  #panel {
    z-index: 1;
  }
}

jQuery:

$("#open").click(function(e){
  e.preventDefault();
  $("div#panel").css("z-index", 0).animate({height: "337px"},"medium");
  $("div#panel").css("z-index", 1).animate({height: "280px"},"medium");
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM