本文實例講述如何用jQuery實現一個仿微軟首頁、隨滑鼠變化而滑動的窗口效果,分享給大家參考。
這是一款仿微軟首頁的感應式滑動窗口:滑鼠移上去後,窗口會向左拉長,移開後又恢復原樣。我是在微軟官方網站發現的,看了看代碼,發現實現起來並不難,於是整理出來與大家共享。
先看運行效果截圖:

具體代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="https://www.w3.org/1999/xhtml"> <head> <title>jQuery仿微軟首頁感應鼠標變化的滑動窗口</title> <meta http-equiv="content-type" content="text/html;charset=gb2312"> <style type="text/css"> .box{width:100px;height:100px;position:absolute;right:0;color:#fff;} .box1{top:100px;background:#000;} .box2{top:250px;background:#333;} .box3{top:400px;background:#666;} .box4{top:550px;background:#999;} </style> <script type="text/javascript" src="jquery1.3.2.js"></script> </head> <body> <div class="box box1">應用</div> <div class="box box2">下載</div> <div class="box box3">Windows Phone</div> <div class="box box4">Office</div> <script type="text/javascript"> $('.box').each(function(){ $(this).hover(function(){ $(this).stop().animate({width:'150'},1000); },function(){ $(this).stop().animate({width:'100'},1000); }); }); </script> </body> </html>實現思路非常直觀:用 each 遍歷每個 box,滑鼠移入時用 animate 把寬度從100拉長到150,移出時再動畫回到100。這裡特意調用了 stop(),避免連續快速滑動時動畫堆疊錯亂。加上 position:absolute 固定在右側,就形成了微軟首頁那種從右向左伸展的立體菜單效果。
希望本文所述對你的jQuery程序設計有所幫助。