Getting the width and height of a page? #2405
-
QuestionHello there, 🙂 I am trying to get the width and height of the whole page for something I'm working on. This seemed like a fairly simple thing to do, but I'm ashamed to say it has been quite a hard ordeal for me. I've already tried the following:
The closest I've come is by rewriting @falkoschindler's solution from #1731 like this: with ui.page_sticky():#.props("hidden"):
btn = ui.button().props("size=1px; dense").on("resize", lambda e: print(e.args))
ui.add_head_html(f'''
<script>
function emitResizeEvent() {{
content = document.querySelector('.nicegui-content');
const dimensions = {{
w: content.offsetParent.offsetWidth,
h: content.offsetParent.offsetHeight
}};
getElement({btn.id}).$emit('resize', dimensions);
}}
window.onload = emitResizeEvent;
window.onresize = emitResizeEvent;
</script>
''') In this code, I:
This kind of works, but has caveats:
At this point I don't know what else to do but to ask for help. I'm going to cry when someone inevitably points out there was some sort of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @PoshoDev, In version 1.4.7 we introduced a simpler way to emit global events: ui.add_head_html('''
<script>
function emitSize() {
emitEvent('resize', {
width: document.body.offsetWidth,
height: document.body.offsetHeight,
});
}
window.onload = emitSize;
window.onresize = emitSize;
</script>
''')
ui.on('resize', lambda e: print(f'resize: {e.args}')) In my tests this emits the size when the page loads as well as when it is resized. Does this fulfil your needs? |
Beta Was this translation helpful? Give feedback.
Hi @PoshoDev,
In version 1.4.7 we introduced a simpler way to emit global events:
In my tests this emits the size when the page loads as well as when it is resized. Does this fulfil your needs?