0731-55587844
其他(360、58、霸屏、搜狗、小程序)等代理合作請(qǐng)發(fā)郵箱254596208@qq.com
站內(nèi)公告:  如果您有其它業(yè)務(wù)聯(lián)系,請(qǐng)發(fā)郵件至yaner@y1web.com郵箱,謝謝您的配合!
營(yíng)銷型網(wǎng)站建設(shè)第一品牌——優(yōu)度網(wǎng)絡(luò)
現(xiàn)在的位置:首頁(yè) > 發(fā)布營(yíng)銷型網(wǎng)站建設(shè) > 新聞資訊 > 文字跟隨鼠標(biāo)
文字跟隨鼠標(biāo)
信息來(lái)源:優(yōu)度網(wǎng)絡(luò)  發(fā)布日期:2009/4/2 瀏覽數(shù)量:953
文章導(dǎo)讀:

<html>
<head>
<title>文字跟隨鼠標(biāo)</title>
<style type="text/css">
<!--
body{
 background-color:#FFFFFF;
}
.spanstyle{
 color:#2244FF;
 font-family:"Courier New";
 font-size:18px;
 text-decoration:overline underline;
 font-weight:bold;
 position:absolute;  /* 絕對(duì)定位 */
 top:-50px;
}
-->
</style>
<script language="javascript">
var x,y;  //鼠標(biāo)當(dāng)前在頁(yè)面上的位置
var step=10; //字符顯示間距,為了好看,step=0則字符顯示沒(méi)有間距
var flag=0;
var message="Cascading Style Sheet";  //跟隨鼠標(biāo)要顯示的字符串
message=message.split(""); //將字符串分割為字符數(shù)組

var xpos=new Array()  //存儲(chǔ)每個(gè)字符的x位置的數(shù)組
for (i=0;i<message.length;i++) {
 xpos[i]=-50;
}
var ypos=new Array()  //存儲(chǔ)每個(gè)字符的y位置的數(shù)組
for (i=0;i<message.length;i++) {
 ypos[i]=-50;
}

for (i=0;i<message.length;i++) {  //動(dòng)態(tài)生成顯示每個(gè)字符span標(biāo)記,
 //使用span來(lái)標(biāo)記字符,是為了方便使用CSS,并可以自由的絕對(duì)定位
 document.write("<span id='span"+i+"' class='spanstyle'>");
 document.write(message[i]);
 document.write("</span>");
}

if (document.layers){
 document.captureEvents(Event.MOUSEMOVE);
}

function handlerMM(e){ //從事件得到鼠標(biāo)光標(biāo)在頁(yè)面上的位置
 x = (document.layers) ? e.pageX : document.body.scrollLeft+event.clientX;
 y = (document.layers) ? e.pageY : document.body.scrollTop+event.clientY;
 flag=1;
}

function makesnake() {  //重定位每個(gè)字符的位置
 if (flag==1 && document.all) { //如果是IE
  for (i=message.length-1; i>=1; i--) {
   xpos[i]=xpos[i-1]+step;  //從尾向頭確定字符的位置,每個(gè)字符為前一個(gè)字符“歷史”水平坐標(biāo)+step間隔,
            //這樣隨著光標(biāo)移動(dòng)事件,就能得到一個(gè)動(dòng)態(tài)的波浪狀的顯示效果
   ypos[i]=ypos[i-1];  //垂直坐標(biāo)為前一字符的歷史“垂直”坐標(biāo),后一個(gè)字符跟蹤前一個(gè)字符運(yùn)動(dòng)
  }
  xpos[0]=x+step //第一個(gè)字符的坐標(biāo)位置緊跟鼠標(biāo)光標(biāo)
  ypos[0]=y
  //上面的算法將保證,如果鼠標(biāo)光標(biāo)移動(dòng)到新位置,則連續(xù)調(diào)用makenake將會(huì)使這些字符一個(gè)接一個(gè)的移動(dòng)的新位置
  // 該算法顯示字符串就有點(diǎn)象人類的游行隊(duì)伍一樣,
 
  for (i=0; i<=message.length-1; i++) {
   var thisspan = eval("span"+(i)+".style");  //妙用eval根據(jù)字符串得到該字符串表示的對(duì)象
   thisspan.posLeft=xpos[i];
   thisspan.posTop=ypos[i];
  }
 }
 else if (flag==1 && document.layers) {
  for (i=message.length-1; i>=1; i--) {
   xpos[i]=xpos[i-1]+step;
   ypos[i]=ypos[i-1];
  }
  xpos[0]=x+step;
  ypos[0]=y;
  for (i=0; i<=message.length-1; i++) {
   var thisspan = eval("document.span"+i);
   thisspan.left=xpos[i];
   thisspan.top=ypos[i];
  }
 }
 var timer=setTimeout("makesnake()",10)  //設(shè)置10毫秒的定時(shí)器來(lái)連續(xù)調(diào)用makesnake(),時(shí)刻刷新顯示字符串的位置。
}
document.onmousemove = handlerMM;
</script>
   </head>
<body onLoad="makesnake();">
</body>
</html>

相關(guān)信息
  • 網(wǎng)絡(luò)營(yíng)銷培訓(xùn)
  • 微網(wǎng)站
  • 掃二維碼
  • 1
  • 2
  • 3
  • 最新上傳
  • 精品案例
  • 自動(dòng)流水線嘉
  • 湖南省湘江重
  • 湘潭家居\建
  • 湘潭學(xué)校微網(wǎng)