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)
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标志.
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)
使用
$(window).width()
Run Code Online (Sandbox Code Playgroud)
要么
$(document).width()
Run Code Online (Sandbox Code Playgroud)
要么
$('body').width()
Run Code Online (Sandbox Code Playgroud)
// 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)
我知道我迟到了回答这个问题,但我希望对任何有类似问题的人有所帮助.当页面因任何原因刷新时,它也可以工作.
$(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)