如何在JS中的屏幕右上角插入一个浮动按钮

How to insert a floating button on top right corner of screen in JS

本文关键字:一个 按钮 插入 JS 右上角 屏幕      更新时间:2024-01-02

仅使用普通javascript,如何在窗口的右上角插入一个始终浮动在其他元素上方的按钮?

到目前为止,我得到了:

var button = document.createElement("Button");
button.innerHTML = "Title";
button.style = "margin-top:0%;right:0%;position:absolute;"
document.body.appendChild(button);

使用top而不是margin-top,并设置高z-index

var button = document.createElement("Button");
button.innerHTML = "Title";
button.style = "top:0;right:0;position:absolute;z-index: 9999"
document.body.appendChild(button);

您几乎做到了,但不要使用margin-top属性,而是使用top属性,并使用position:fixed;而不是position:absolute;

var button = document.createElement("Button");
button.innerHTML = "Title";
button.style = "top:0;right:0;position:fixed;"
document.body.appendChild(button);

将定位更改为固定,顶部更改为0

button.style = "top:0%;right:0%;position:fixed;"