Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di skjobax
    Registrato dal
    Jan 2010
    Messaggi
    569

    Spostare divs in javascript

    Ho un problemino.
    Sapendo che i codici delle frecce sono da 38 a 42, come faccio, con una switch a muovere una div in basso, in alto, a destra e a sinistra a seconda della freccia premuta?

  2. #2
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    codice:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <style type="text/css">
    #idDiv {
    	position:absolute;
    	width:100px;
    	height:100px;
    	background:orange;
    }
    </style>
    <script type="text/JavaScript">
    /*<![CDATA[*/
    function getKeyCode(e,step){
    	var key=(e.which)?e.which:e.keyCode;
    	var dS=document.getElementById('idDiv').style;
    	switch(key){
    		case 37:dS.left=(parseInt(dS.left)-step)+'px';break;
    		case 38:dS.top=(parseInt(dS.top)-step)+'px';break;
    		case 39:dS.left=(parseInt(dS.left)+step)+'px';break;
    		case 40:dS.top=(parseInt(dS.top)+step)+'px';break;
    	}
    }
    /*]]>*/
    </script>
    </head>
    <body onkeydown="getKeyCode(event,5)">
    <div id="idDiv" style="top:100px;left:100px"></div>
    </body>
    </html>

  3. #3
    Utente di HTML.it L'avatar di skjobax
    Registrato dal
    Jan 2010
    Messaggi
    569
    Grazieeeeee!!!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.