[英]jquery not working with css media queries
I have an element called ".left". 我有一个名为“ .left”的元素。 Its css looks like this: 它的CSS看起来像这样:
.left{
float:right;
background-repeat: no-repeat;
width: 300px;
height: 100%;}
I have a media query for this element that looks like this: 我对此元素有一个媒体查询,如下所示:
@media screen and (max-width: 570px) {
.left{
display: none;}
}
What I'm trying to do is have a button named ".close" change it's function when the media query is activated. 我想做的是激活媒体查询后,有一个名为“ .close”的按钮来更改其功能。 I want one,two,three to fadein if display:none and one,two,three,four to fadein if display does not = none. 如果要显示,我希望一,二,三淡入:如果显示不= none,则一,二,三,四要淡入淡出。 This is what I have so far in jquery: 这是我到目前为止在jQuery中的内容:
<script>
$( document ).ready(function($) {
console.log( "ready!" );
$(".close").on('click',function() {
if ($(".left").css("display") == "none" ){
$("#one,#two,#three").fadeIn();}
else {
$("#one,#two,#three,#four").fadeIn();}
});
});
</script>
However, what I've coded only executes the if condition and not the else condition... no matter what size the browser is. 但是,无论浏览器的大小如何,我编写的代码仅执行if条件,而不执行else条件。
I need some help! 我需要协助! Thanks! 谢谢!
Try 尝试
$(document).ready(function ($) {
console.log("ready!");
$(".close").on('click', function () {
if ($(".left").is(":hidden")) { //check if .left is hidden or not
$("#one,#two,#three").fadeIn();
} else {
$("#one,#two,#three,#four").fadeIn();
}
});
});
You should be using media queries in javascript. 您应该在javascript中使用媒体查询。 See here: 看这里:
https://developer.mozilla.org/en-US/docs/Web/API/Window.matchMedia https://developer.mozilla.org/zh-CN/docs/Web/API/Window.matchMedia
$( document ).ready(function($) {
console.log( "ready!" );
$(".close").on('click',function() {
if(window.matchMedia("(max-width: 570px)").matches) {
$("#one,#two,#three").fadeIn();}
} else {
$("#one,#two,#three,#four").fadeIn();}
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.