因此,我使用Enquire.js添加和删除引导程序的预定义css类为我的网站。以下是我所拥有的:
一些引导HTML缩略图:
<div class="row">
<div class="thumb thumb col-xs-12 col-md-3">
<a href="#" class="thumbnail">
<img src="..." class="img-rounded img-responsive" alt="...">
</a>
</div>
<div id ="thumb" class="thumb col-xs-12 col-md-3">
<a href="#" class="thumbnail">
<img src="..." class="img-rounded img-responsive" alt="...">
</a>
</div>
</div>我已经设置了enquire.js,以便根据屏幕大小调整缩略图大小:
<script type="text/javascript">
var $info = $('.thumb');
enquire.register("(max-width: 480px)", {
match: function() {
$info.removeClass('col-xs-6');
$info.addClass('col-xs-12');
},
unmatch: function() {
$info.removeClass('col-xs-12');
$info.addClass('col-xs-6');
}
}).listen();
</script> 问题:
我遇到的问题是,enquire.js代码只在屏幕大小减少到480 in或更少时才会启动。
因此,当第一次加载站点时,重新大小的代码将无法工作,直到我将其手动调整到480‘t或更低,然后您才能看到调整大小的过程。
您可以查看站点这里。
发布于 2014-07-26 18:02:29
非匹配函数只有在从匹配状态转到不匹配状态时才能工作。
我认为您也希望使用安装函数。这将在调用处理程序时在内部运行javascript。这是来自enquire.js站点的四个主要调用
enquire.register("screen and (max-width:45em)", {
// OPTIONAL
// If supplied, triggered when a media query matches.
match : function() {},
// OPTIONAL
// If supplied, triggered when the media query transitions
// *from a matched state to an unmatched state*.
unmatch : function() {},
// OPTIONAL
// If supplied, triggered once, when the handler is registered.
setup : function() {},
// OPTIONAL, defaults to false
// If set to true, defers execution of the setup function
// until the first time the media query is matched
deferSetup : true,
// OPTIONAL
// If supplied, triggered when handler is unregistered.
// Place cleanup code here
destroy : function() {}
});发布于 2014-07-26 17:43:03
我认为您可能需要准备好文档中的代码。这里使用jQuery:
<script type="text/javascript">
$(function() {
var $info = $('.thumb');
enquire.register("(max-width: 480px)", {
match: function() {
$info.removeClass('col-xs-6');
$info.addClass('col-xs-12');
},
unmatch: function() {
$info.removeClass('col-xs-12');
$info.addClass('col-xs-6');
}
});
});
</script> https://stackoverflow.com/questions/24971699
复制相似问题