window.matchMedia正在工作,但我认为它会像使用“@media only screen”一样工作,并且当屏幕变窄时会发生这种情况。例如,使用window.matchMedia时,如果您使用的是桌面浏览器,则必须加载或刷新屏幕。有没有办法让window.matchMedia像@media CSS一样工作,当你增加或减少屏幕宽度时,它就会自动发生,而不需要刷新或(重新)加载屏幕?
下面的示例使用在:Americastributetoparis.com上
jQuery(document).ready(function($) {
if (window.matchMedia("(max-width: 800px)").matches) {
// phone
$(".front-page-1").backstretch(["/wp-content/themes/digital-pro/images/americas-tribute-to-paris-mobile.png"]);
} else {
//tab or desktop
$(".front-page-1").backstretch([BackStretchImg.src]);
}});
发布于 2016-03-22 04:02:56
在搜索引擎上搜索我的问题时发现了这个,并将其应用于此:media query not just on run time, but also when any changes to the window state occur
jQuery(document).ready(function($) {
var mql = window.matchMedia("(max-width: 800px)")
mediaqueryresponse(mql) // call listener function explicitly at run time
mql.addListener(mediaqueryresponse) // attach listener function to listen in on state changes
function mediaqueryresponse(mql){
if (mql.matches){ // if media query matches
// phone
$(".front-page-1").backstretch(["/wp-content/themes/digital-pro/images/americas-tribute-to-paris-mobile.png"]);
} else {
//tab or desktop
$(".front-page-1").backstretch([BackStretchImg.src]);
}
}});
https://stackoverflow.com/questions/36139149
复制相似问题