admin管理员组文章数量:1335430
I'm using templates/handlebars but none of the event handlers are triggered when the browser window resizes. Not sure how to capture the resize event in order to dynamically set the div's height to be within the viewport
Here's a sample of what I've tried so far using meteor's event map:
Template.basic.events({
'resize window' : function(evt, tmpl){
alert("test");
},
};
Ideally this handler would be called each time the window is resized so I can use $(window).height()
to set the div's height in the html using tmpl.find('#main-div');
.
I'm using templates/handlebars but none of the event handlers are triggered when the browser window resizes. Not sure how to capture the resize event in order to dynamically set the div's height to be within the viewport
Here's a sample of what I've tried so far using meteor's event map:
Template.basic.events({
'resize window' : function(evt, tmpl){
alert("test");
},
};
Ideally this handler would be called each time the window is resized so I can use $(window).height()
to set the div's height in the html using tmpl.find('#main-div');
.
1 Answer
Reset to default 9Most problems which directly rely on jQuery can be solved using the onRendered callback like so:
Template.basic.onRendered(function() {
$(window).resize(function() {
console.log($(window).height());
});
});
Technically this works, but because window
never gets removed as part of the rendering process, this technique has a big disadvantage: it adds a new resize handler every time the template is rendered.
Because window
is always available, you can instead use the created
and destroyed
callbacks to register and unregister the handlers:
Template.basic.onCreated(function() {
$(window).resize(function() {
console.log($(window).height());
});
});
Template.basic.onDestroyed(function() {
$(window).off('resize');
});
Note, however, that stopping the resize handler in onDestroyed may not really be what you want. See this question for more details.
Also note that in the current version of meteor, you can check the number of event handlers like so:
$._data($(window).get(0), "events").resize.length
本文标签: Meteorjs How to set a div height dynamically via JavascriptStack Overflow
版权声明:本文标题:Meteor.js: How to set a div height dynamically via Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742236815a2438229.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论