使用jquery检测响应设计中的更改
我需要在支持宽、普通和窄宽度的响应设计中绕过一些javascript代码。我需要在宽模式下绕过代码。我可以这样写:使用jquery检测响应设计中的更改,jquery,responsive-design,Jquery,Responsive Design,我需要在支持宽、普通和窄宽度的响应设计中绕过一些javascript代码。我需要在宽模式下绕过代码。我可以这样写: if ($('#page').width() < 1237) { ... do the animation stuff } 这将允许我将硬编码宽度隔离到单个函数,然后我可以编写: if (!$('#page').hasClass('layout-wide') { ... do the animation stuff } 但是有没有一种方法
if ($('#page').width() < 1237) {
... do the animation stuff
}
这将允许我将硬编码宽度隔离到单个函数,然后我可以编写:
if (!$('#page').hasClass('layout-wide') {
... do the animation stuff
}
但是有没有一种方法可以完全避免硬编码像素宽度
但是有没有一种方法可以完全避免硬编码像素宽度
媒体查询是最好的方法
@media (max-width:767px) {
.class{}
}
这是我想到的。。。我的方法是将特定于每个受支持的布局宽度的类应用于body元素。我只想在存在媒体查询的情况下应用这些类,因此首先检查浏览器是否支持它们。以下是讨论以下内容的线索: 不幸的是,在这个解决方案中,我需要硬编码支持的宽度。如果有更好的方法,请随时发布。就目前而言,这是一个小的折衷快速的结果 以下是CSS:
/* Add to your main CSS file - used to test for media query support */
@media all and (min-width:1px) {
.mediatest {position:absolute}
}
下面是JavaScript:
var $globals = {
mediaQuerySupport: false
};
function initMediaQueries () {
// tests for media query support and if it exists, it applies classes to the body element on window resize
$globals.mediaQuerySupport = false;
/*
test if the browser knows about media queries
create a div, apply the test class, test to see if the computed style is absolute... then delete the div
*/
var d = document.createElement('div');
d.className = "mediatest"; // found in main css file
document.body.appendChild(d);
if( window.getComputedStyle && window.getComputedStyle(d).position == "absolute") {
$globals.mediaQuerySupport = true;
}
document.body.removeChild(d);
if ($globals.mediaQuerySupport) {
// apply a class to the body element now and when window resizes
setBodyLayout ();
$(window).resize(function () {
setBodyLayout();
});
}
} // initMediaQueries ()
function mediaQueriesEnabled () {
return ($globals.mediaQuerySupport);
}
function setBodyLayout () {
// hard-coded widths for the supported media query layouts - change to your needs
var width = $(window).width();
if (width >= 1237) {
$('body').removeClass('layout-normal').removeClass('layout-ipad').addClass('layout-wide');
}
else if (width >= 980) {
$('body').removeClass('layout-wide').removeClass('layout-ipad').addClass('layout-normal');
}
else { // if narrower than 980, its the most narrow width
$('body').removeClass('layout-wide').removeClass('layout-normal').addClass('layout-ipad');
}
}
这篇文章正是你想要的:
如果你可以使用媒体查询,你为什么要这样做?我有一个导航按钮,可以触发一个功能,显示一组指向网站的链接。当窗口处于宽模式时,这些链接已经显示。我需要检测它们是否已经显示,而不是调用显示它们的javascript。我已经在应用媒体查询来实现3种不同的视图模式。我需要知道确定我在JavaScript中处于哪种模式的最佳方法。。。不是CSS。
var $globals = {
mediaQuerySupport: false
};
function initMediaQueries () {
// tests for media query support and if it exists, it applies classes to the body element on window resize
$globals.mediaQuerySupport = false;
/*
test if the browser knows about media queries
create a div, apply the test class, test to see if the computed style is absolute... then delete the div
*/
var d = document.createElement('div');
d.className = "mediatest"; // found in main css file
document.body.appendChild(d);
if( window.getComputedStyle && window.getComputedStyle(d).position == "absolute") {
$globals.mediaQuerySupport = true;
}
document.body.removeChild(d);
if ($globals.mediaQuerySupport) {
// apply a class to the body element now and when window resizes
setBodyLayout ();
$(window).resize(function () {
setBodyLayout();
});
}
} // initMediaQueries ()
function mediaQueriesEnabled () {
return ($globals.mediaQuerySupport);
}
function setBodyLayout () {
// hard-coded widths for the supported media query layouts - change to your needs
var width = $(window).width();
if (width >= 1237) {
$('body').removeClass('layout-normal').removeClass('layout-ipad').addClass('layout-wide');
}
else if (width >= 980) {
$('body').removeClass('layout-wide').removeClass('layout-ipad').addClass('layout-normal');
}
else { // if narrower than 980, its the most narrow width
$('body').removeClass('layout-wide').removeClass('layout-normal').addClass('layout-ipad');
}
}