如何随机移动假光标?

时间:2017-02-28 16:04:37

标签: javascript cursor

我想拥有2个假光标,我试过这个用于创建2个光标。

filter.png

现在他们的动作取决于原始光标, 我的问题是 如何随机移动

1 个答案:

答案 0 :(得分:1)

你可以做那样的事情



// get the fake cursor by is id
var xyMirror = document.getElementById('fakeCursor');
var xyMirror2 = document.getElementById('fakeCursor2');
xyMirror.style.position = "absolute";
xyMirror2.style.position = "absolute";
var xMax = 0;var yMax = 0;
// listen for mouse movements
window.onmousemove = function(event) {  
	// Use event X and Y to set max value
	if (event.clientX > xMax) xMax = event.clientX;
	if (event.clientY > yMax) yMax = event.clientY;
	// Random position for fakeCursor
	xyMirror.style.left = getRandomArbitrary(0, xMax) +'px';
	xyMirror.style.top = getRandomArbitrary(0, yMax)+'px';
	// Random position for fakeCursor2
	xyMirror2.style.left = getRandomArbitrary(0, xMax) +'px';
	xyMirror2.style.top = getRandomArbitrary(0, yMax) +'px';
}

function getRandomArbitrary(min, max) {
	return Math.random() * (max - min) + min;
}

<div id="fakeCursor">fake1</div>
<div id="fakeCursor2">fake2</div>
&#13;
&#13;
&#13;