gpt4 book ai didi

javascript - 取消固定后输入位置

转载 作者:行者123 更新时间:2023-11-28 01:25:56 25 4
gpt4 key购买 nike

我有一个输入,我想将其放置在我每次在其他任何地方用鼠标单击屏幕的任何区域。

$("*").mouseup(function(e) {

var x, y;

x = e.clientX;
y = e.clientY;

$(".myInput").css({
"position": "fixed",
"z-index": "100",
"margin-top": y,
"margin-left": x
});

});

通过上面的代码,输入确实到达了我想要的位置,但是因为它固定位置然后它随着屏幕的滚动而移动。

我尝试了所有的位置选项。

预先感谢您的帮助。

最佳答案

您可以使用 absolute 而不是 fixed 这将阻止它在您滚动时粘住页面。

Use window.scrollY and add it to you e.clientY to make your Y-Pos relative to the scroll.

此外,使用 topleft 而不是使用 margin-topmargin-left


$(document).mouseup(function(e) {
var x, y;

x = e.clientX;
y = e.clientY + window.scrollY;

$(".myInput").css({
"position": "absolute",
"z-index": "100",
"top": y,
"left": x
});

});
div {
height: 1000px;
}
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="myInput" />
<div></div>

关于javascript - 取消固定后输入位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51244585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com