query .js在页面加载时不起作用,仅在屏幕<= 480px时起作用
Enquire.js not working on page load, only works when screen <= 480px
所以我使用query .js为我的网站添加和删除bootstrap的预定义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>
我已经设置了query .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>
问题:
我遇到的问题是,query .js代码只有在屏幕尺寸减小到480px或更小时才会启动。
所以当网站第一次加载时,调整大小代码将无法工作,直到我手动将其大小调整到480px或更低,然后你可以看到调整大小发生了
你可以看看这里的网站
unmatch函数只有在从匹配状态变为不匹配状态时才会起作用。
我想你也想使用setup函数。这将在调用处理程序时运行内部的javascript。以下是来自query .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() {}
});
我认为您可能需要在文档中准备好代码。这里使用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>
相关文章:
- electronic BrowserWindow的最小高度和宽度在hide()show()方法之后不起作用
- 监视函数从服务返回不起作用,但作用域函数起作用
- 幻灯片滚动javascript不起作用
- 简单的javascript在Shopify中不起作用
- Recaptcha在IE7和IE8中不起作用
- Ember Data DS.Model's set函数不起作用
- JsFiddle上的鼠标事件不起作用
- 我的AngularJS表达式没有'不起作用
- 点击按钮输入不起作用
- 面向对象的Javascript代码在IE7中不起作用
- 分部隐藏在jquery中不起作用
- 在phonegap应用程序内部重定向不起作用
- Array.length似乎不起作用;console.log则显示其他情况
- $ionicplatform内的$scope不;不起作用
- 我的javascript for循环不起作用
- Meteor-添加用户自定义字段的方法不起作用
- 为什么 .focus() 不起作用,而 .css(“color”,“red”) 在同一个选择器上起作用
- Textarea必需的attribut在javascript中不起作用
- 为什么javascript:void(0)在Firefox中不起作用
- query .js在页面加载时不起作用,仅在屏幕<= 480px时起作用