王朝知道
分享
 
 
 

JavaScript如何实现图象指着鼠标转?

王朝知道·作者佚名  2011-09-20  
宽屏版  字体: |||超大  
 
分类: 电脑/网络 >> 程序设计 >> 其他编程语言
 
问题描述:

JavaScript如何实现图象指着鼠标转 ,就是当鼠标绕着图象运行,图象像时钟指针那样,定点指着鼠标转,鼠标走到哪它指到哪!(我说得够白的了)

参考答案:

1

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

<title>围绕图片疯狂旋转的花朵</title>

</head>

<body>

<script language="JavaScript">

<!--

var images='images/flower2.gif';//将图片名称改为你的图像!

var amount=7;//Number of images here. Do not alter for this script!!

var speed=1;

var RunTime = 0;

var cntr=0;

var xcntr=100;

var pulse=25;

var Xpos = 0;

var Ypos = 0;

var _y;

var temp;

/*If you use an image larger or smaller than the one I've used

the cursor will be off center.Alter the 2 variables below to center it.*/

var UpDown=-10;

var LeftRight=-5;

if (document.all){

document.write('<div id="ieDiv" style="position:absolute;top:0px;left:0px">');

document.write('<div id="c" style="position:relative">');

for (n=0; n < amount; n++)

document.write('<img src="'+images+'" style="position:absolute;top:0px;left:0px;visibility:hidden">');

document.write('</div>');

document.write('</div>');

function FollowMouse(){

Xpos = document.body.scrollLeft+event.x+UpDown;

Ypos = document.body.scrollTop+event.y+LeftRight;

}

document.onmousemove = FollowMouse;

}

else if (document.layers){

window.captureEvents(Event.MOUSEMOVE);

function xFollowMouse(evnt){

Xpos = evnt.pageX+UpDown;

Ypos = evnt.pageY+LeftRight;

}

window.onMouseMove = xFollowMouse;

for (ns=0; ns < amount; ns++)

document.write("<LAYER NAME='n"+ns+"' LEFT=0 TOP=0 VISIBILITY=HIDE><IMG SRC='"+images+"'></LAYER>");

}

function msi(){

if (document.layers){

for ( i = 0 ; i < ns ; i++ )

{

temp="n"+i

document.layers[0].visibility='show';

document.layers[0].top = Ypos+cntr*Math.cos((RunTime+i*4.5)/5);

document.layers[0].left =Xpos+cntr*Math.sin((RunTime+i*4.5)/5);

}

cntr+=1;

RunTime+=speed;

stp=setTimeout('msi()',10);

if (cntr>=100)

{

cntr=100;

speed=2.5;

for ( i = 0 ; i < ns ; i++ )

{

temp="n"+i

document.layers[temp].visibility='show';

document.layers[temp].top=Ypos+cntr*Math.cos((RunTime-100)*i/90);

document.layers[temp].left=Xpos+cntr*Math.sin((RunTime-100)*i/90);

}

}

if (RunTime>182)

{

speed=0.5;

for ( i = 0 ; i < ns ; i++ )

{

temp="n"+i

document.layers[temp].top=Ypos+xcntr*Math.cos(((RunTime-182)+i*4.5)/5);

document.layers[temp].left=Xpos+xcntr*Math.sin(((RunTime-182)+i*4.5)/5)*Math.cos((RunTime-182)/5);

}

}

}

else if (document.all){

for (i=0;i<ieDiv.all.c.all.length;i++)

{

ieDiv.all.c.all[0].style.visibility='visible';

ieDiv.all.c.all[0].style.top=Ypos+cntr*Math.cos((RunTime+i*4.5)/5);

ieDiv.all.c.all[0].style.left=Xpos+cntr*Math.sin((RunTime+i*4.5)/5);

}

cntr+=1;

RunTime+=speed;

stp=setTimeout('msi()',10);

if (cntr>=100)

{

cntr=100;

speed=2.5;

for (i=0;i<ieDiv.all.c.all.length;i++)

{

ieDiv.all.c.all[i].style.visibility='visible';

ieDiv.all.c.all[i].style.top=Ypos+cntr*Math.cos((RunTime-100)*i/90);

ieDiv.all.c.all[i].style.left=Xpos+cntr*Math.sin((RunTime-100)*i/90);

}

}

if (RunTime>182)

{

speed=0.5;

for (i=0;i<ieDiv.all.c.all.length;i++)

{

ieDiv.all.c.all[i].style.top=Ypos+xcntr*Math.cos(((RunTime-182)+i*4.5)/5);

ieDiv.all.c.all[i].style.left=Xpos+xcntr*Math.sin(((RunTime-182)+i*4.5)/5)*Math.cos((RunTime-182)/5);

}

}

}

if (RunTime>210)

{

xcntr-=10;

}

if (document.layers)

_y=-window.innerWidth-90;

else if (document.all)

_y=-document.body.clientWidth-90;

if (xcntr <= _y)

{

RunTime=0;

speed=1;

cntr=0;

xcntr=100;

}

}

msi()

//-->

</script>

</body>

</html>

2

<html>

<head>

<title>很酷的跟随鼠标的三色彩带</title>

</head><body bgColor=#000000>

<!--将以下代码加入HTML的<Body></Body>之间-->

<script language="JavaScript">

<!--

var a_Colour='fff000';

var b_Colour='00ff00';

var c_Colour='ff00ff';

var Size=120;

var YDummy=new Array(),XDummy=new Array(),xpos=0,ypos=0,ThisStep=0;step=0.6;

if (document.layers){

window.captureEvents(Event.MOUSEMOVE);

function nsMouse(evnt){

xpos = window.pageYOffset+evnt.pageX+6;

ypos = window.pageYOffset+evnt.pageY+16;

}

window.onMouseMove = nsMouse;

}

else if (document.all)

{

function ieMouse(){

xpos = document.body.scrollLeft+event.x+6;

ypos = document.body.scrollTop+event.y+16;

}

document.onmousemove = ieMouse;

}

function swirl(){

for (i = 0; i < 3; i++)

{

YDummy[i]=ypos+Size*Math.sin((1*Math.sin((ThisStep)/10))+i*2)*Math.sin((ThisStep)/4);

XDummy[i]=xpos+Size*Math.cos((1*Math.sin((ThisStep)/10))+i*2)*Math.sin((ThisStep)/4);

}

ThisStep+=step;

setTimeout('swirl()',10);

}

var amount=10;

if (document.layers){

for (i = 0; i < amount; i++)

{

document.write('<layer name=nsa'+i+' top=0 left=0 width='+i/2+' height='+i/2+' bgcolor='+a_Colour+'></layer>');

document.write('<layer name=nsb'+i+' top=0 left=0 width='+i/2+' height='+i/2+' bgcolor='+b_Colour+'></layer>');

document.write('<layer name=nsc'+i+' top=0 left=0 width='+i/2+' height='+i/2+' bgcolor='+c_Colour+'></layer>');

}

}

else if (document.all){

document.write('<div id="ODiv" style="position:absolute;top:0px;left:0px">'

+'<div id="IDiv" style="position:relative">');

for (i = 0; i < amount; i++)

{

document.write('<div id=x style="position:absolute;top:0px;left:0px;width:'+i/2+';height:'+i/2+';background:'+a_Colour+';font-size:'+i/2+'"></div>');

document.write('<div id=y style="position:absolute;top:0px;left:0px;width:'+i/2+';height:'+i/2+';background:'+b_Colour+';font-size:'+i/2+'"></div>');

document.write('<div id=z style="position:absolute;top:0px;left:0px;width:'+i/2+';height:'+i/2+';background:'+c_Colour+';font-size:'+i/2+'"></div>');

}

document.write('</div></div>');

}

function prepos(){

var ntscp=document.layers;

var msie=document.all;

if (document.layers){

for (i = 0; i < amount; i++)

{

if (i < amount-1)

{

ntscp['nsa'+i].top=ntscp['nsa'+(i+1)].top;ntscp['nsa'+i].left=ntscp['nsa'+(i+1)].left;

ntscp['nsb'+i].top=ntscp['nsb'+(i+1)].top;ntscp['nsb'+i].left=ntscp['nsb'+(i+1)].left;

ntscp['nsc'+i].top=ntscp['nsc'+(i+1)].top;ntscp['nsc'+i].left=ntscp['nsc'+(i+1)].left;

}

else

{

ntscp['nsa'+i].top=YDummy[0];ntscp['nsa'+i].left=XDummy[0];

ntscp['nsb'+i].top=YDummy[1];ntscp['nsb'+i].left=XDummy[1];

ntscp['nsc'+i].top=YDummy[2];ntscp['nsc'+i].left=XDummy[2];

}

}

}

else if (document.all){

for (i = 0; i < amount; i++)

{

if (i < amount-1)

{

msie.x[i].style.top=msie.x[i+1].style.top;msie.x[i].style.left=msie.x[i+1].style.left;

msie.y[i].style.top=msie.y[i+1].style.top;msie.y[i].style.left=msie.y[i+1].style.left;

msie.z[i].style.top=msie.z[i+1].style.top;msie.z[i].style.left=msie.z[i+1].style.left;

}

else

{

msie.x[i].style.top=YDummy[0];msie.x[i].style.left=XDummy[0];

msie.y[i].style.top=YDummy[1];msie.y[i].style.left=XDummy[1];

msie.z[i].style.top=YDummy[2];msie.z[i].style.left=XDummy[2];

}

}

}

setTimeout("prepos()",10);

}

function Start(){

swirl(),prepos()

}

window.onload=Start;

// -->

</script>

</body></html>

3

<html>

<head>

<title>围绕鼠标的立体星环</title>

<meta content="text/html; charset=gb2312" http-equiv="Content-Type">

<script language="JavaScript">

<!--

ns=(document.layers)?1:0;

Clrs=new Array('ff0000','00ff00','ffffff','ff00ff','ffa500','ffff00','00ff00','ffffff','ff00ff')

var amount=8

var step=0.3;

var currStep=0;

var Ypos=0;

var Xpos=0;

if (ns){

for (i=0; i < amount; i++)

document.write('<LAYER NAME="n'+i+'" LEFT=0 TOP=0 BGCOLOR=#FFFFFF CLIP="0,0,2,2"></LAYER>');

window.captureEvents(Event.MOUSEMOVE);

function nMouse(evnt){

Ypos = evnt.pageY;

Xpos = evnt.pageX;

}

window.onMouseMove=nMouse;

}

else{

document.write('<div style="position:absolute;top:0px;left:0px">');

document.write('<div style="position:relative">');

for (i=0; i < amount; i++)

document.write('<div id="me" style="position:absolute;top:0px;left:0px;width:2px;height:2px;font-size:2px;background:#00aaff"></div>');

document.write('</div></div>');

function iMouse(){

Ypos=event.y+document.body.scrollTop;

Xpos=event.x+document.body.scrollLeft;

}

document.onmousemove = iMouse;

}

function Comet(){

for (i=0; i < amount; i++){

var randCol=Math.floor(Math.random()*Clrs.length);

var layer=(document.layers)?document.layers['n'+i]:me[i].style;

layer.top =Ypos+60*Math.sin((currStep + i*3.1)/4)*Math.cos(currStep/10);

layer.left=Xpos+60*Math.cos((currStep + i*3.1)/4);

if (ns) layer.bgColor=Clrs[randCol];else layer.background=Clrs[randCol];

}

currStep+=step;

setTimeout("Comet()",10);

}

window.onload=Comet;

// -->

</script>

</head>

<body bgcolor="#000000">

</body>

</html>

小贴士:① 若网友所发内容与教科书相悖,请以教科书为准;② 若网友所发内容与科学常识、官方权威机构相悖,请以后者为准;③ 若网友所发内容不正确或者违背公序良俗,右下举报/纠错。
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
如何用java替换看不见的字符比如零宽空格&#8203;十六进制U+200B
 干货   2023-09-10
网页字号不能单数吗,网页字体大小为什么一般都是偶数
 干货   2023-09-06
java.lang.ArrayIndexOutOfBoundsException: 4096
 干货   2023-09-06
Noto Sans CJK SC字体下载地址
 干货   2023-08-30
window.navigator和navigator的区别是什么?
 干货   2023-08-23
js获取referer、useragent、浏览器语言
 干货   2023-08-23
oscache遇到404时会不会缓存?
 干货   2023-08-23
linux下用rm -rf *删除大量文件太慢怎么解决?
 干货   2023-08-08
刀郎新歌破世界纪录!
 娱乐   2023-08-01
js实现放大缩小页面
 干货   2023-07-31
生成式人工智能服务管理暂行办法
 百态   2023-07-31
英语学习:过去完成时The Past Perfect Tense举例说明
 干货   2023-07-31
Mysql常用sql命令语句整理
 干货   2023-07-30
科学家复活了46000年前的虫子
 探索   2023-07-29
英语学习:过去进行时The Past Continuous Tense举例说明
 干货   2023-07-28
meta name="applicable-device"告知页面适合哪种终端设备:PC端、移动端还是自适应
 干货   2023-07-28
只用css如何实现打字机特效?
 百态   2023-07-15
css怎么实现上下滚动
 干货   2023-06-28
canvas怎么画一个三角形?
 干货   2023-06-28
canvas怎么画一个椭圆形?
 干货   2023-06-28
canvas怎么画一个圆形?
 干货   2023-06-28
canvas怎么画一个正方形?
 干货   2023-06-28
中国河南省郑州市金水区蜘蛛爬虫ip大全
 干货   2023-06-22
javascript简易动态时间代码
 干货   2023-06-20
感谢员工的付出和激励的话怎么说?
 干货   2023-06-18
 
>>返回首页<<
 
 
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有