计算窗口/主体/元素大小必须在页面呈现后完成。您需要config
在 Mithril 呈现页面后允许 DOM 操作的属性。你可以在这里阅读:http: //lhorie.github.io/mithril/mithril.html#the-config-attribute
你可以这样做:http: //jsbin.com/woxuku/1/edit ?js,output
App = {}
App.controller = function (attrs) {
}
App.view = function (ctrl, attrs) {
return m("div.app", {
style: {
position: 'absolute',
height: '300px',
width: '400px',
backgroundColor: 'lightblue'
}
}, [
"navbar",
m('nav', {
style: {
position: 'relative',
backgroundColor: 'yellow'
},
config: function(el, isInit, context){
el.style.height = ($('.app').getBoundingClientRect().height / 2).toString() + 'px'
el.style.width = ($('.app').getBoundingClientRect().width / 2).toString() + 'px'
}
}, 'menu')
])
}
m.mount(document.body, App)