简体   繁体   English

@media(最大宽度:837px)!= $(文档).width()<837

[英]@media (max-width: 837px) != $(document).width()<837

Everybody, I try to make a responsive website so I've added media queries So it's like: 大家,我都试图建立一个响应式网站,所以我添加了媒体查询,所以就像:

@media (min-width: 838px) {
    bigscreen code
}
@media (max-width: 837px) {
    littlescreen code
}

But when I do some things with JS like 但是当我用JS做一些事情时

if($(document).width()<837+1){
code
}

I've made a console.log($(document).width()). 我已经做了一个console.log($(document).width())。 And the css littlescreen code happened when $(document).width() = 820 but I don't know why. 当$(document).width()= 820时,发生了css littlescreen代码,但我不知道为什么。 If someone know, thanks you 如果有人知道,谢谢

EDIT: $(window).width() = $(document).width() = $('body').innerWidth() = $(window).innerWidth() = $(document).innerWidth() 编辑:$(window).width()= $(document).width()= $('body')。innerWidth()= $(window).innerWidth()= $(document).innerWidth()

Did you try adding your code inside the event function? 您是否尝试在事件函数中添加代码?

 window.addEventListener('resize', function(event){ console.log($(document).width()); if($(document).width()<837){ alert("Less than 837px"); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

$(window).width();   // returns width of browser
 $(document).width(); // returns width of HTML

I think, you should try $(window).width() 我认为,您应该尝试$(window).width()

if($(window).width()<837+1){
code
}

According to the @media docs it uses the viewport width, not the document width . 根据@media docs,它使用viewport宽度,而不是document width

In the console on this page: 在此页面上的控制台中:

window.innerWidth
1936
$(document).width()
1921

Perhaps we are talking about the difference being the width of the scrollbar (or something else)? 也许我们谈论的是滚动条(或其他)宽度的差异?

This site gives you a good guide to different width outputs: http://ryanve.com/lab/dimensions/ 该站点为您提供了有关不同宽度输出的良好指南: http : //ryanve.com/lab/dimensions/

And this one for dealing with browser viewport differences: http://www.matanich.com/2013/01/07/viewport-size/ 而这个用于处理浏览器视口差异的工具: http : //www.matanich.com/2013/01/07/viewport-size/

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

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