标签:
跟踪算法 红快攻击蓝块
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>跟踪算法</title> <meta name="author" content="Administrator" /> <script src="jquery-1.11.1.js"></script> <!-- Date: 2015-05-30 --> <style> #div1{width:20px;height:20px;background:red;position:absolute;left:50px;top:500px} #div2{width:20px;height:20px;background:blue;position:absolute;right:50px;top:400px} </style> <script> window.onload = function(){ //固定的减 运动的 setInterval(function(){ var a = $(‘#div2‘).offset().left - $(‘#div1‘).offset().left; var b = $(‘#div2‘).offset().top - $(‘#div1‘).offset().top; var c = Math.sqrt( a*a + b*b ); //定义斜边的速度10 算出x轴 速度 和y轴速度 var speedX = 10*a/c; var speedY = 10*b/c; //攻击蓝快 $(‘#div1‘).css({ ‘left‘:$(‘#div1‘).offset().left + speedX, ‘top‘: $(‘#div1‘).offset().top + speedY }) },30) } </script> </head> <body> <div id="div1"></div> <div id="div2"></div> </body> </html>
标签:
原文地址:http://www.cnblogs.com/webskill/p/4540481.html