JavaScript 绝对定位

发布于 2024-10-14 06:04:06 字数 945 浏览 1 评论 0原文

我正在尝试使用 JavaScript 创建一个新的 div 层,该层可以在页面加载后绝对定位在页面上。

我的代码如下:

<html><head>
<script type="text/javascript"> 
function showLayer() {
var myLayer = document.createElement('div');
myLayer.id = 'bookingLayer';
myLayer.style.position = 'absolute';
myLayer.style.x = 10;
myLayer.style.y = 10;
myLayer.style.width = 300;
myLayer.style.height = 300;
myLayer.style.padding = '10px';
myLayer.style.background = '#00ff00';
myLayer.style.display = 'block';
myLayer.style.zIndex = 99;
myLayer.innerHTML = 'This is the layer created by the JavaScript.';
document.body.appendChild(myLayer);
}
</script>
</head><body bgcolor=red>This is the normal HTML content.
<script type="text/javascript"> 
showLayer();
</script>
</body></html>

页面可以在这里看到。

我遇到的问题是 div 位于原始正文内容之后,而不是位于新图层上。我该如何补救?

I am trying to create a new div layer using JavaScript that can be absolutely positioned on the page after page load.

My code is as follows:

<html><head>
<script type="text/javascript"> 
function showLayer() {
var myLayer = document.createElement('div');
myLayer.id = 'bookingLayer';
myLayer.style.position = 'absolute';
myLayer.style.x = 10;
myLayer.style.y = 10;
myLayer.style.width = 300;
myLayer.style.height = 300;
myLayer.style.padding = '10px';
myLayer.style.background = '#00ff00';
myLayer.style.display = 'block';
myLayer.style.zIndex = 99;
myLayer.innerHTML = 'This is the layer created by the JavaScript.';
document.body.appendChild(myLayer);
}
</script>
</head><body bgcolor=red>This is the normal HTML content.
<script type="text/javascript"> 
showLayer();
</script>
</body></html>

The page can be seen here.

The problem I am having is that the div is sitting after the original body content rather than over it on a new layer. How can I remedy this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

许一世地老天荒 2024-10-21 06:04:06

尝试使用此方法:

var myLayer = document.createElement('div');
myLayer.id = 'bookingLayer';
myLayer.style.position = 'absolute';
myLayer.style.left = '10px';
myLayer.style.top = '10px';
myLayer.style.width = '300px';
myLayer.style.height = '300px';
myLayer.style.padding = '10px';
myLayer.style.background = '#00ff00';
myLayer.innerHTML = 'This is the layer created by the JavaScript.';
document.body.appendChild(myLayer);

它不起作用的原因是您使用了 xy css 属性(不存在)而不是 left顶部。另外,对于左、上、宽、高,您必须指定一个单位(例如 px 表示像素)。

Try with this instead:

var myLayer = document.createElement('div');
myLayer.id = 'bookingLayer';
myLayer.style.position = 'absolute';
myLayer.style.left = '10px';
myLayer.style.top = '10px';
myLayer.style.width = '300px';
myLayer.style.height = '300px';
myLayer.style.padding = '10px';
myLayer.style.background = '#00ff00';
myLayer.innerHTML = 'This is the layer created by the JavaScript.';
document.body.appendChild(myLayer);

The reason it was not working is that you used x and y css properties (which don't exist) instead of left and top. Also, for left, top, width, height you had to specify a unit (e.g. px for pixels).

天冷不及心凉 2024-10-21 06:04:06

尝试这些,

  • 使用顶部和左侧而不是 x 和 y 设置位置。
  • 绝对定位元素需要包含在也具有位置样式的元素内。通常的技巧是创建一个带有position:relative的容器。
  • 您应该使用 + 'px' 设置宽度、高度、顶部、左侧等样式。
  • 您不需要为 div 设置 display: block,因为它已经是块元素。

Try these,

  • Set the position using top and left rather than x and y.
  • An absolute positioned element needs to be contained inside something that also has a position style. The usual trick is to create a container with position: relative.
  • You should be setting styles such as width, height, top, left etc with + 'px'.
  • You don't need to set display: block for the div as it is already a block element.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文