[英]How to change the mouse position?
I want to make the mouse cannot enter some areas. 我要使鼠标无法进入某些区域。 So when I move the mouse cursor to that area, its positions is changed programmatically. 因此,当我将鼠标光标移到该区域时,其位置将以编程方式更改。
What I mean is I want to make some areas that the user cannot move the mouse in to. 我的意思是我想做一些用户无法将鼠标移入的区域。
Would u tell me how to do it in javavscript? 你能告诉我如何在javavscript中做到吗?
Actually You cannot move the mouse pointer with javascript.But if you asked about: how to change the position of the element, when the mouse cursor move to that area
. 实际上,您不能使用javascript移动鼠标指针。但是,如果您询问以下内容: how to change the position of the element, when the mouse cursor move to that area
。 For do this, see the code snippet below: 为此,请参见下面的代码片段:
var elm = document.getElementById( 'area' ), /* get the element */ rct = elm.getBoundingClientRect(), /* return size of element */ elW = rct.width, /* return element width */ elH = rct.height, /* return element height */ mrg = 50; /* element margin */ document.addEventListener( 'mousemove', function( e ) { var vpW = Math.max( document.documentElement.clientWidth, window.innerWidth || 0 ), /* return Viewport width */ vpH = Math.max( document.documentElement.clientHeight, window.innerHeight || 0 ), /* return Viewport height */ rec = elm.getBoundingClientRect(), /* return size of element */ elL = rec.left, /* return element left position */ elT = rec.top, /* return element top position */ mLp = e.pageX, /* return mouse left position */ mTp = e.pageY, /* return mouse top position */ eLp, /* Save new left position */ eTp; /* Save new top position */ if ( mLp > elL - mrg && mLp < elL && mTp > elT - mrg && mTp < elT + elH + mrg ) { eLp = mLp + mrg; if ( mLp + mrg + elW > vpW ) eLp = mLp - mrg - elW } if ( mLp > elL && mLp < elL + elW + mrg && mTp > elT - mrg && mTp < elT + elH + mrg ) { eLp = mLp - mrg - elW; if ( mLp - mrg - elW < 0 ) eLp = mLp + mrg } if ( mTp > elT - mrg && mTp < elT && mLp > elL - mrg && mLp < elL + elW + mrg ) { eTp = mTp + mrg; if ( mTp + mrg + elH > vpH ) eTp = mTp - mrg - elH } if ( mTp > elT && mTp < elT + elH + mrg && mLp > elL - mrg && mLp < elL + elW + mrg ) { eTp = mTp - mrg - elH; if ( mTp - mrg - elH < 0 ) eTp = mTp + mrg } elm.style.left = eLp + 'px'; elm.style.top = eTp + 'px'; } )
#area { position: absolute; width: 50px; height: 50px; left: 50%; top: 50%; border: 3px solid #888; background-color: #aaa } #area:hover { background-color: red }
<div id="area"></div>
But it's not a good idea to prevent mouse cursor from getting into the element area. 但是,防止鼠标光标进入元素区域不是一个好主意 。 To do this, it's better to hide the element as you move the mouse cursor to near it. 为此,最好在将鼠标光标移到元素附近时将其隐藏。 See the code snippet below: 请参见下面的代码段:
var elm = document.getElementById( 'area' ), rct = elm.getBoundingClientRect(), elW = rct.width, elH = rct.height, elL = rct.left, elT = rct.top, mrg = 30; document.addEventListener( 'mousemove', function( e ) { var mLp = e.pageX, mTp = e.pageY; if ( mLp > elL - mrg && mLp < elL + elW + mrg && mTp > elT - mrg && mTp < elT + elH + mrg ) elm.style.display = 'none' else elm.style.display = 'block' } )
#area { position: absolute; width: 50px; height: 50px; left: 50%; top: 50%; border: 3px solid #999; background-color: #aaa } #area:hover { background-color: red }
<div id="area"></div>
And if you accept the above solution, you can simply do this without using javascript
and using CSS
only. 并且,如果您接受上述解决方案,则可以简单地执行此操作,而无需使用javascript
和仅使用CSS
。
#parent { position: absolute; padding: 1px; left: 30%; top: 30% } #parent:hover > div#area { opacity: 0; cursor: none } #area { cursor: default; opacity: 1; width: 50px; height: 50px; border: 3px solid #999; background-color: #aaa }
<div id="parent"> <div id="area"></div> </div>
Unfortunately, this is not currently a JavaScript feature as I found after a little research. 不幸的是,这不是我经过一些研究后发现的JavaScript功能。 Here is my source. 这是我的消息来源。
This would definitely raise privacy concerns, as a user may not want to fight for control over their mouse. 这肯定会引起隐私问题,因为用户可能不想争夺对鼠标的控制权。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.