如果屏幕宽度小于960像素,请执行某些操作

Eva*_*nss 207 javascript jquery

如果我的屏幕宽度小于960像素,我如何让jQuery做一些事情?无论窗口大小如何,下面的代码始终会触发第二个警报:

if (screen.width < 960) {
    alert('Less than 960');
}
else {

    alert('More than 960');
}
Run Code Online (Sandbox Code Playgroud)

azi*_*ani 424

使用jQuery获取窗口的宽度.

if ($(window).width() < 960) {
   alert('Less than 960');
}
else {
   alert('More than 960');
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意,如果滚动条可见,$(window).width()在浏览器中不一致.我发现使用[javascript media queries](https://github.com/eddiemachado/bones/issues/468#issuecomment-23626238)更加可靠. (11认同)
  • @forsvunnet这个链接已经死了. (9认同)
  • javascript 媒体查询链接:https://www.w3schools.com/howto/howto_js_media_queries.asp (2认同)

jk.*_*jk. 133

您可能希望将其与resize事件结合使用:

 $(window).resize(function() {
  if ($(window).width() < 960) {
     alert('Less than 960');
  }
 else {
    alert('More than 960');
 }
});
Run Code Online (Sandbox Code Playgroud)

对于RJ:

var eventFired = 0;

if ($(window).width() < 960) {
    alert('Less than 960');

}
else {
    alert('More than 960');
    eventFired = 1;
}

$(window).on('resize', function() {
    if (!eventFired) {
        if ($(window).width() < 960) {
            alert('Less than 960 resize');
        } else {
            alert('More than 960 resize');
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

我试过http://api.jquery.com/off/没有成功,所以我选择了eventFired标志.

  • @Ted这就是为什么我说'结合它'. (5认同)
  • 包装在调整大小事件中只会使其在调整大小时触发,而不是在加载时触发。 (2认同)

moa*_*abi 10

我建议(需要jQuery):

/*
 * windowSize
 * call this function to get windowSize any time
 */
function windowSize() {
  windowHeight = window.innerHeight ? window.innerHeight : $(window).height();
  windowWidth = window.innerWidth ? window.innerWidth : $(window).width();

}

//Init Function of init it wherever you like...
windowSize();

// For example, get window size on window resize
$(window).resize(function() {
  windowSize();
  console.log('width is :', windowWidth, 'Height is :', windowHeight);
  if (windowWidth < 768) {
    console.log('width is under 768px !');
  }
});
Run Code Online (Sandbox Code Playgroud)

在CodePen中添加:http://codepen.io/moabi/pen/QNRqpY?编辑= 0011

然后你可以使用var:windowWidth和Height轻松获得窗口的宽度:windowHeight

否则,获取一个js库:http: //wicky.nillia.ms/enquire.js/


小智 10

您还可以使用带有javascript的媒体查询。

const mq = window.matchMedia( "(min-width: 960px)" );

if (mq.matches) {
       alert("window width >= 960px");
} else {
     alert("window width < 960px");
}
Run Code Online (Sandbox Code Playgroud)


Dan*_*mak 10

我建议不要将jQuery用于此类事情,然后继续window.innerWidth

if (window.innerWidth < 960) {
    doSomething();
}
Run Code Online (Sandbox Code Playgroud)

  • 为什么最好不要使用 jQuery? (2认同)
  • @raison我发现 $(window).width() 不会包含滚动条 - 所以如果你有一个 960px 的屏幕,它将返回 944px 并且你的js不会按预期触发。该解决方案将返回 960px (2认同)
  • @raison 如果不需要的话为什么要使用 jQuerry?它带来了额外的问题...... (2认同)
  • 这可能是这里最好的解决方案 (2认同)

ley*_*nnx 9

// Adds and removes body class depending on screen width.
function screenClass() {
    if($(window).innerWidth() > 960) {
        $('body').addClass('big-screen').removeClass('small-screen');
    } else {
        $('body').addClass('small-screen').removeClass('big-screen');
    }
}

// Fire.
screenClass();

// And recheck when window gets resized.
$(window).bind('resize',function(){
    screenClass();
});
Run Code Online (Sandbox Code Playgroud)


Vel*_*vic 8

我知道我迟到了回答这个问题,但我希望对任何有类似问题的人有所帮助.当页面因任何原因刷新时,它也可以工作.

$(document).ready(function(){

if ($(window).width() < 960 && $(window).load()) {
        $("#up").hide();
    }

    if($(window).load()){
        if ($(window).width() < 960) {
        $("#up").hide();
        }
    }

$(window).resize(function() {
    if ($(window).width() < 960 && $(window).load()) {
        $("#up").hide();
    }
    else{
        $("#up").show();
    }

    if($(window).load()){
        if ($(window).width() < 960) {
        $("#up").hide();
        }
    }
    else{
        $("#up").show();
    }

});});
Run Code Online (Sandbox Code Playgroud)