发布网友 发布时间:2022-04-24 04:28
共5个回答
懂视网 时间:2022-04-24 08:49
第一种 按钮控制input type="button" value="左" id="1"> <input type="button" value="右" id="2"> <div id="3"> div { width: 100px; height: 100px; background-color: bisque; position: absolute; left: 100px; top: 100px; }
然后在script中获得div和两个按钮
var left = document.getElementById("2"); var right = document.getElementById("1"); var div = document.getElementById("3");
然后添加onclick函数
left.onclick = function () { } right.onclick = function () { }设置一个变量,控制div的left改变
var x = 100;当点击按钮时触发
left.onclick = function () { x=x+10; div.style.left = x+"px"; } right.onclick = function () { x=x-10; div.style.left = x+"px"; }源码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> <style> div { width: 100px; height: 100px; background-color: bisque; position: absolute; left: 100px; top: 100px; } </style> </head> <body> <input type="button" value="左" id="1"> <input type="button" value="右" id="2"> <div id="3"> </div> <script> var left = document.getElementById("2"); var right = document.getElementById("1"); var div = document.getElementById("3"); var x = 100; left.onclick = function () { x=x+10; div.style.left = x+"px"; } right.onclick = function () { x=x-10; div.style.left = x+"px"; } </script> </body> </html>
<div id="3"> </div> <style> div { width: 100px; height: 100px; background-color: bisque; position: absolute; left: 100px; top: 100px; } </style>
在script里面 获得div
var div=document.getElementById("3");
然后声明两个变量控制改变div的left和top
var px=100; var py =100;
然后获得键值
document.onkeydown(在document文档对象中,按任何键都会触发此函数)
alert中输出的event.keyCode会对应按键时,当前键相应的事件值(即每一个按键对应为一个键值)
document.onkeydown = function(){ alert(event.keyCode); }
然后通过测试上下左右得到键值,在swich语句中改变div的left和top改变其位置
switch (event.keyCode){ case 37: px = px-10; div.style.left = px+"px"; break; case 38: py = py-10; div.style.top = py+"px"; break; case 39: px = px+10; div.style.left = px+"px"; break; case 40: py = py+10; div.style.top = py+"px"; break; }
源码:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> <style> div { width: 100px; height: 100px; background-color: bisque; position: absolute; left: 100px; top: 100px; } </style> </head> <body> <div id="3"> </div> <script> var div=document.getElementById("3"); var px=100; var py =100; document.onkeydown = function(){ // alert(event.keyCode); switch (event.keyCode){ case 37: px = px-10; div.style.left = px+"px"; break; case 38: py = py-10; div.style.top = py+"px"; break; case 39: px = px+10; div.style.left = px+"px"; break; case 40: py = py+10; div.style.top = py+"px"; break; } } </script> </body> </html>
热心网友 时间:2022-04-24 05:57
//html
热心网友 时间:2022-04-24 07:15
javascript操作对象略显麻烦,建议采用Jquery.
Jquery代码如下
$("button").click(function(){
热心网友 时间:2022-04-24 08:50
用jquery的话,有现成的方法。slideout和slidein方法。热心网友 时间:2022-04-24 10:41
functionchangeBody(index){switch(index){case1:{document.getElementById('iDBody1').style.display="";document.getElementById('iDBody2').style.display="none";document.getElementById('iDBody3').style.display="none";}case2:{document.getElementById('iDBody1').style.display="none";document.getElementById('iDBody2').style.display="";document.getElementById('iDBody3').style.display="none";}case3:{document.getElementById('iDBody1').style.display="none";document.getElementById('iDBody2').style.display="none";document.getElementById('iDBody3').style.display="";}}}