首页 > 解决方案 > 通过javascript的宽度和高度不起作用?

问题描述

我正在制作一个允许您在页面上绘图的 javascript 小书签。当您滚动时,图纸会留在原处。(我有我的理由)。它附加了一个带有(在测试阶段)红色背景的画布。我创建了两个变量,宽度和高度,即屏幕宽度和高度。

var c = document.createElement("canvas");
var w = screen.width;
var h = screen.height;
c.setAttribute("id", "canvas");
c.setAttribute("style", "position: fixed; z-index: 1000000; top: 0%; left: 0%; background-color:         
red; width: " + w + "; height: " + h + ";");
console.log(w + ", " + h)
document.body.appendChild(c)

它似乎只是作为默认大小出现在角落里。提前致谢!

标签: javascript

解决方案


CSS 宽度和高度需要单位:

c.setAttribute("style", "position: fixed; z-index: 1000000; top: 0%; left: 0%; background-color:         
red; width: " + w + "px; height: " + h + "px;");

推荐阅读