本文實例分析了jQuery使用animate實現ul列表項相互飄動效果。分享給大家供大家參考,具體如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> <style type="text/css"> body{font-size:12px;maring:0px;padding:0px;} #main{width:600px;margin:auto;background-color:gold;} ul{float:left;margin:30px;width:80px;height:140px;padding:2px;background-color:#0099cc} #ulRight{margin-left:300px;} li{list-style-type:none;margin-top:3px;background-color:Gray;} </style> <script src="../js/jquery-1.5.1.min.js" type="text/javascript"></script> <script type="text/javascript"> $(function () { $("li[name='pigeon']").click(function () { //開始飄動的位置,由于ulRight的margin-left為300px所以要加300,同理leftUl也一樣 var rightUlLeft = parseInt($("#ulRight").position().left + 300) + "px"; var leftUlLeft = parseInt($("#ulLeft").position().left + 30) + "px"; if ($(this).parent().attr("id") == "ulLeft") { $(this).queue(function (next) { $(this).css({ "position": "absolute", "left": leftUlLeft }); next(); }) .animate({ "left": rightUlLeft }, 2000) .queue(function () { $(this).appendTo("#ulRight").css({ "position": "static" }); $(this).dequeue(); }); } else { $(this).queue(function (next) { $(this).css({ "position": "absolute", "left": rightUlLeft }); next(); }) .animate({ "left": leftUlLeft }, 2000) .queue(function () { $(this).appendTo("#ulLeft").css({ "position": "static" }); $(this).dequeue(); }); } }); }); </script></head><body> <div id="main"> <ul id="ulLeft"> <li name='pigeon'>白鴿</li> <li name='pigeon'>花鴿</li> <li name='pigeon'>黑鴿</li> <li name='pigeon'>灰鴿</li> <li name='pigeon'>紅鴿</li> <li name='pigeon'>絳鴿</li> </ul> <ul id="ulRight"> </ul> <div style="clear:both;"></div> </div></body></html>更多關于jQuery相關內容感興趣的讀者可查看本站專題:《jQuery常用插件及用法總結》、《jQuery常見經典特效匯總》、《jQuery form操作技巧匯總》、《jQuery操作json數據技巧匯總》、《jQuery擴展技巧總結》、《jQuery拖拽特效與技巧總結》、《jQuery表格(table)操作技巧匯總》、《jquery中Ajax用法總結》、《jQuery動畫與特效用法總結》及《jquery選擇器用法總結》
希望本文所述對大家jQuery程序設計有所幫助。
新聞熱點
疑難解答