html移动光标左右上下健,JS实现盒子跟着鼠标移动及键盘方向键控制盒子移动效果示例...

本文实例讲述了JS实现盒子跟着鼠标移动及键盘方向键控制盒子移动。分享给大家供大家参考,具体如下:

1. 盒子跟着鼠标移动

content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">

Document

*{

margin:0;

padding:0;

}

div{

width: 100px;

height: 100px;

background: red;

position: absolute;

}

document.οnmοusemοve=function fn(ev) {

var event=window.event||ev;

var oDiv=document.getElementsByTagName("div");

for(x=0;x

oDiv[x].style.transition=(x*0.5)+"s";

oDiv[x].style.left=event.clientX+"px";

oDiv[x].style.top=event.clientY+"px";

}

}

2. 键盘方向键控制盒子移动

div{

width:100px;

height:100px;

background: red;

position:absolute;

left:100px;

top:100px;

transition: 0.1s;

}

window.onload = function(){

var oDiv = document.getElementById("div1");

var left = false;

var right = false;

var top = false;

var bottom = false;

document.onkeydown = function(ev){

var oEvent = ev || event;

var keyCode = oEvent.keyCode;

switch(keyCode){

case 37:

left=true;

break;

case 38:

top=true;

break;

case 39:

right=true;

break;

case 40:

bottom=true;

break;

}

};

setInterval(function(){

if(left){

oDiv.style.left = oDiv.offsetLeft-10+"px";

}else if(top){

oDiv.style.top = oDiv.offsetTop-10+"px";

}else if(right){

oDiv.style.left = oDiv.offsetLeft+10+"px";

}else if(bottom){

oDiv.style.top = oDiv.offsetTop+10+"px";

}

},50);

document.onkeyup = function(ev){

var oEvent = ev || event;

var keyCode = oEvent.keyCode;

switch(keyCode){

case 37:

left=false;

break;

case 38:

top=false;

break;

case 39:

right=false;

break;

case 40:

bottom=false;

break;

}

}

}

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:http://tools.jb51.net/code/HtmlJsRun测试一下运行效果。

希望本文所述对大家JavaScript程序设计有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个示例,使用 `window.document.onkeydown` 来监听键盘事件,实现上下左右控制光标焦点的功能: ```html <template> <div> <input ref="input1" type="text" /> <input ref="input2" type="text" /> <input ref="input3" type="text" /> <input ref="input4" type="text" /> </div> </template> <script> export default { mounted() { window.document.onkeydown = (event) => { const { key } = event; switch (key) { case "ArrowUp": this.moveFocus("up"); break; case "ArrowDown": this.moveFocus("down"); break; case "ArrowLeft": this.moveFocus("left"); break; case "ArrowRight": this.moveFocus("right"); break; } }; }, methods: { moveFocus(direction) { const inputs = [ this.$refs.input1, this.$refs.input2, this.$refs.input3, this.$refs.input4, ]; const focusedElement = document.activeElement; let currentIndex = -1; if (focusedElement) { currentIndex = inputs.findIndex((input) => input === focusedElement); } let nextIndex; switch (direction) { case "up": nextIndex = currentIndex - 2; break; case "down": nextIndex = currentIndex + 2; break; case "left": nextIndex = currentIndex - 1; break; case "right": nextIndex = currentIndex + 1; break; } if (nextIndex >= 0 && nextIndex < inputs.length) { inputs[nextIndex].focus(); } }, }, }; </script> ``` 在上述示例中,我们监听了 `window.document` 对象的 `onkeydown` 事件,并根据按下的键盘按键来执行相应的操作。在 `moveFocus` 方法中,我们获取了所有的输入框元素,并根据当前焦点元素的位置和移动方向来计算下一个焦点的位置,然后调用 `focus` 方法设置焦点到对应的输入框。 请注意,具体的实现方式可能因你所使用的框架或组件库而有所不同。上述示例是基于 Vue.js 的单文件组件,你可以根据自己的项目需求进行相应的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值