简体   繁体   English

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

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

Using only plain javascript, how to insert a button that will always be floating in the top right corner of the window above other elements ? 仅使用普通的javascript,如何插入一个按钮,该按钮将始终漂浮在其他元素上方的窗口的右上角?

So far I got: 到目前为止,我得到了:

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

Use top instead of margin-top and set a high z-index 使用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); 

You were almost there, but instead of using the margin-top property, use the top property and use position:fixed; 您几乎就在那儿,但是不要使用margin-top属性,而要使用top属性并使用position:fixed; instead of position:absolute; 代替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;"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM