分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

07.30《jQuery》——2.2使用键盘上下左右键控制div框

发布时间:2023-09-06 02:07责任编辑:胡小海关键词:divjQuery
<!DOCTYPE html><html> ???<head> ???????<meta charset="UTF-8"> ???????<title></title> ???????<style type="text/css"> ???????????#bbb { ???????????????height: 300px; ???????????????width: 300px; ???????????????background-color: aliceblue; ???????????????position: relative; ???????????} ???????</style> ???????<script src="../jquery-3.2.1/jquery-3.2.1.js"></script> ???????<script type="text/javascript"> ???????????$(function() { ???????????????$("#left").click(function() { ???????????????????$("div:not(:animated)").animate({ ???????????????????????left: "-=100" ???????????????????}, 50); ???????????????}); ???????????????$("#right").click(function() { ???????????????????$("div:not(:animated)").animate({ ???????????????????????left: "+=100" ???????????????????}, 50); ???????????????}); ???????????????$("#up").click(function() { ???????????????????$("div:not(:animated)").animate({ ???????????????????????top: "-=100" ???????????????????}, 50); ???????????????}); ???????????????$("#down").click(function() { ???????????????????$("div:not(:animated)").animate({ ???????????????????????top: "+=100" ???????????????????}, 50); ???????????????}); ???????????}) ???????????$(document).keydown(function() { ???????????????if(event.keyCode == ‘37‘) { ???????????????????$("div:not(:animated)").animate({ ???????????????????????left: "-=100" ???????????????????}, 50); ???????????????} ???????????????if(event.keyCode == ‘39‘) { ???????????????????$("div:not(:animated)").animate({ ???????????????????????left: "+=100" ???????????????????}, 50); ???????????????} ???????????????if(event.keyCode == ‘38‘) { ???????????????????$("div:not(:animated)").animate({ ???????????????????????top: "-=100" ???????????????????}, 50); ???????????????} ???????????????if(event.keyCode == ‘40‘) { ???????????????????$("div:not(:animated)").animate({ ???????????????????????top: "+=100" ???????????????????}, 50); ???????????????} ???????????}) ???????</script> ???</head> ???<body> ???????<button id="left">left</button> ???????<button id="right">right</button> ???????<button id="up">up</button> ???????<button id="down">down</button> ???????<div id="bbb"></div> ???</body></html>

代码通过添加keydown方法,实现通过键盘操作div框移动的功能。

07.30《jQuery》——2.2使用键盘上下左右键控制div框

原文地址:https://www.cnblogs.com/justlive-tears/p/9393684.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved