【发布时间】:2020-04-21 11:34:28
【问题描述】:
我正在尝试在下面创建列布局,即页眉、内容和粘性页脚。 我希望画布的宽度和高度使用 JavaScript 跟随其内容的 clientWidth 和 clientHeight。
<style>
body {
; margin : 0
; height : 100%
; display : flex
; flex-direction : column
}
header,
footer {
; flex : 0
}
#content {
; flex : 1
}
</style>
<header>header</header>
<div id=content>
<canvas id=canvas></canvas>
</div>
<footer>footer</footer>
<script>
canvas = document.getElementById( 'canvas' )
ctx = canvas.getContext( '2d' )
const
FitCanvas = () => {
canvas.width = canvas.parentNode.clientWidth
canvas.height = canvas.parentNode.clientHeight
ctx.beginPath()
ctx.moveTo( 0, 0 )
ctx.lineTo( canvas.width, canvas.height )
ctx.stroke()
}
window.addEventListener(
'resize'
, FitCanvas
)
FitCanvas()
</script>
展开窗口时看起来不错。但是当缩小窗口时,页脚不会粘住。
我想让缩小的图像与初始图像匹配。
请帮忙,提前谢谢。
【问题讨论】:
标签: javascript html css canvas flexbox