javascript - 通过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)
它似乎只是作为默认大小出现在角落里。提前致谢!
解决方案
CSS 宽度和高度需要单位:
c.setAttribute("style", "position: fixed; z-index: 1000000; top: 0%; left: 0%; background-color:
red; width: " + w + "px; height: " + h + "px;");
推荐阅读
- java - 收到错误“找不到符号类”。为什么?
- node.js - NodeJs Jenkins 插件无法与 dockerfile 代理一起使用
- python - 我得到 Value 'func.Out' is unsubscriptable 试图使用 python 函数在 azure blob 存储中写入
- typescript - Typescript keyof 类型根据键的数量变化
- ios - 使用未解析的标识符“referenceWithThreadConfined”
- python - 散景:悬停工具提示以纳秒为单位显示时间 - 如何转换?
- r - 在R中使用latticeExtra中的doubleYScale时如何更改Y轴刻度的字体大小
- php - 如何在 laravel 中使用 phpword 将 html/php 代码转换为 doc
- d3.js - 如何使用 d3 的可缩放朝阳
- python - Tensorflow无法在Tensorflow图函数中获取值