One cannot control the movement of the mouse pointer using JavaScript. However, if you are interested in changing the position of an element when the mouse cursor moves to a specific area, you can achieve this with the following code:
var elm = document.getElementById( 'area' ),
rct = elm.getBoundingClientRect(),
elW = rct.width,
elH = rct.height,
mrg = 50;
document.addEventListener( 'mousemove', function( e ) {
var vpW = Math.max( document.documentElement.clientWidth, window.innerWidth || 0 ),
vpH = Math.max( document.documentElement.clientHeight, window.innerHeight || 0 ),
rec = elm.getBoundingClientRect(),
elL = rec.left,
elT = rec.top,
mLp = e.pageX,
mTp = e.pageY,
eLp,
eTp;
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>
However, it is generally not advisable to restrict the mouse cursor from entering an element's area. A better approach would be to hide the element as the mouse cursor approaches it. You can achieve this effect with the following code:
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>
If you find the above solutions suitable, you can achieve the same effect without using JavaScript and relying purely on 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>