使用Jquery創建一個層,實現當鼠標移動到層上面不消失,示例代碼如下,感興趣的朋友可以參考下
代碼如下: <!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> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>無標題文檔</title> <style type="text/css"> #Adiv div {float:left; width:100px;border:1px solid #333;margin-top:100px;} #SDiv {width:135px;position: absolute;} </style> <script type="text/javascript" src="js/jquery-1.7.2.js"></script> <script type="text/javascript"> $(function(){ var x = -90; var y = -70; $("#Adiv div").mouseenter(function(e){ this.xx = ($(this).index() + 1) * 100; this.yy = $(this).offset().top; var login = $(this).attr("href"); if($("#SDiv")){$("#SDiv").remove();} var div = "<div id='SDiv' style='border:1px solid #333'><img src='images/login_box/box_onmouse.png' border='0' usemap='#Map' /><map name='Map' id='Map'><area shape='rect' id='divLogin' coords='16,14,67,35' href="+login+" /><area shape='rect' id='divRegister' coords='75,15,127,37' href='http://www.baidu.com' /></map></div>"; $("body").append(div); $("#SDiv").css({ "top": (this.yy + y) + "px", "left": (this.xx + x) + "px" }); }).mouseleave(function (e) { var ex = e.pageX; var ey = e.pageY; var sx = $("#SDiv").offset().top; var sxx = $("#SDiv").offset().top + 72; var sy = $("#SDiv").offset().left; var syy = $("#SDiv").offset().left + 135; if(ey > sx && ey <= sxx && ex > sy && ex <= syy){ $("#SDiv").mouseleave(function(){$("#SDiv").remove();}); return false; } $("#SDiv").remove(); }); }) </script> </head> <body> <!-- <div class="box_onmouse"><img src="images/login_box/box_onmouse.png" border="0" usemap="#Map" /> <map name="Map" id="Map"> <area shape="rect" id="divLogin" coords="16,14,67,35" href="#" /> <area shape="rect" id="divRegister" coords="75,15,127,37" href="#" /> </map> </div> --> <div id="Adiv"> <div href="11111">1111111</div> <div href="22222">2222222</div> <div href="33333">3333333</div> </div> <div>項目中有這樣的需求:鼠標移動到某個div上面時動態創建一個層,這個層中有2個按鈕圖片(美工切換了),每個圖片鏈接不同地址(鏈接地址有前面的div提供),鼠標移除這個層則移除創建的div,如果鼠標移動到這個創建的div上面則不移除</div> </body> </html>