简体   繁体   English

在jQuery中匹配CSS媒体查询

[英]Matching css media queries in jQuery

this is a continuation of my question yesterday regarding an automatic jQuery image slider which you can find here - Jquery automatic image slider . 这是我昨天关于自动jQuery图像滑块的问题的延续,您可以在这里找到-jQuery自动图像滑块

I've added a couple of media queries into my css and am trying to mirror them with an 'if / if else' statement in the javascript, however at the moment it's only working for the second section of the statement. 我在CSS中添加了一些媒体查询,并尝试在javascript中使用“ if / if else”语句来镜像它们,但是目前,该查询仅适用于该语句的第二部分。 For both others the slide appears to move 620px to the left, leaving a section of another picture in the frame, but still resets to 'margin: 0' after the last slide. 对于其他两个幻灯片,幻灯片似乎向左移动620像素,在帧中保留了另一张图片的一部分,但在最后一张幻灯片之后仍重置为'margin:0'。

There is also a lot of repetition in the code which ideally I would like to get rid of but when I tried including only the width variable in the 'if' statement the code didn't run. 在代码中也有很多重复,理想情况下,我想摆脱这些重复,但是当我尝试在'if'语句中仅包含width变量时,代码没有运行。

I'm stumped! 我很沮丧! Any help would be greatly appreciated. 任何帮助将不胜感激。

 $(document).ready(function() { //INDEX IMAGES SLIDER $(function() { if($('#slider').width() > 760) { //configuration var width = 720; var speed = 1000; var pause = 2000; var current = 1; //cache DOM var $slider = $('#slider'); var $slides = $slider.find('#slides'); var $slide = $slides.find('.slide'); setInterval(function() { $slides.animate({'margin-left': '-='+width}, speed, function() { current++; if (current === $slide.length) { current = 1; $slides.css('margin-left', 0); } }); }, pause); } else if($('#slider').width() <= 760) { var width = 620; var speed = 1000; var pause = 2000; var current = 1; //cache DOM var $slider = $('#slider'); var $slides = $slider.find('#slides'); var $slide = $slides.find('.slide'); setInterval(function() { $slides.animate({'margin-left': '-='+width}, speed, function() { current++; if (current === $slide.length) { current = 1; $slides.css('margin-left', 0); } }); }, pause); } else { var width = 520; var speed = 1000; var pause = 2000; var current = 1; //cache DOM var $slider = $('#slider'); var $slides = $slider.find('#slides'); var $slide = $slides.find('.slide'); setInterval(function() { $slides.animate({'margin-left': '-='+width}, speed, function() { current++; if (current === $slide.length) { current = 1; $slides.css('margin-left', 0); } }); }, pause); }; }); }); 
 #slider { width: 720px; height: 400px; overflow: hidden; margin: 100px auto; } #slider #slides { display: block; width: 2880px; height: 400px; margin: 0; padding: 0; } #slider .slide { float: left; list-style: none; height: 400px; width: 720px; } #slider .slide img { width: 100%; } @media only screen and (max-width: 760px) { #slider { width: 620px; } #slider .slide { width: 620px; } #slider .slide img { width: 620px; } } @media only screen and (max-width: 650px) { #slider { width: 520px; } #slider .slide { width: 520px; } #slider .slide img { width: 520px; } } 
 <div class="page-container"> <div id="slider"> <ul id="slides"> <li class="slide"><img src="images/sp_1.png"></li> <li class="slide"><img src="images/ss_1.jpg"></li> <li class="slide"><img src="images/sd_1.jpg"></li> <li class="slide"><img src="images/sp_1.png"></li> </ul> </div> </div> 

Your code is fundementally flawed; 您的代码存在根本性缺陷; the width of the slider will never exceed 720px and that is why only the second branch of your if statement runs. 滑块的宽度永远不会超过720px,这就是为什么if语句的第二个分支运行的原因。 Your final else will never run. 您的else决赛将永远不会进行。

In terms of refactoring your code, you could do something like this: 在重构代码方面,您可以执行以下操作:

 $(document).ready(function() { //INDEX IMAGES SLIDER $(function() { var width; var speed = 1000; var pause = 2000; var current = 1; if ($('#slider').width() > 760) { //configuration width = 720; } else if ($('#slider').width() <= 760) { width = 620; } else { width = 520; }; //cache DOM var $slider = $('#slider'); var $slides = $slider.find('#slides'); var $slide = $slides.find('.slide'); setInterval(function() { $slides.animate({ 'margin-left': '-=' + width }, speed, function() { current++; if (current === $slide.length) { current = 1; $slides.css('margin-left', 0); } }); }, pause); }); }); 

Hope this helps. 希望这可以帮助。

Thanks Andrew for your solutions, really helpful. 感谢Andrew提供的解决方案,非常有帮助。 I've now fixed the code completely so that the slider works when the page loads, whatever the initial width, and then also responds to the window being resized. 现在,我已经完全修复了代码,以便无论页面的初始宽度如何,加载页面时滑块都可以工作,然后还可以响应窗口调整大小。

I've posted the new code below. 我在下面发布了新代码。 Hope it's useful to someone! 希望对某人有用!

 $(document).ready(function() { //INDEX IMAGES SLIDER $(function() { //configuration var width = $('#slider').width(); //detect width of slider on page load var speed = 1000; var pause = 7000; var current = 1; //change value of 'width' to match image widths when media queries are triggered $(window).resize(function() { if ($('#slider').width() === 720) { //configuration width = 720; } else if ($('#slider').width() === 620) { width = 620; } else { width = 520; }; }); //cache DOM var $slider = $('#slider'); var $slides = $slider.find('#slides'); var $slide = $slides.find('.slide'); //move the images the defined width and speed to the left setInterval(function() { $slides.animate({'margin-left': '-='+width}, speed, function() { current++; if (current === $slide.length) { current = 1; $slides.css('margin-left', 0); } }); }, pause); }); }); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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