jQuery開發之超連結提示效果和圖片提示效果,jquery超連結
1,超連結提示效果
超連結內建的有提示效果的,這裡就把自己定義的提示效果放在一起可以進行一下比較。範例程式碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030"><title>jQuery test </title><script type ="text/javascript" src ="jquery.js" ></script><script type ="text/javascript">$(document).ready(function(){ var x = 10; var y = 20; $("a.tooltip").mouseover(function(e){ this.myTitle = this.title; this.title =""; var tooltip ="<div id ='tooltip'>"+this.myTitle+"</div>"; $("body").append(tooltip); //將它追加到文檔中 $("#tooltip").css({"top":(e.pageY+y)+"px","left":(e.pageX+x)+"px","color":"#00ff00"}).show("fast");//設定x座標和y座標,並顯示 }) .mouseout(function(){ this.title = this.myTitle; $("#tooltip").remove(); //移除 }) .mousemove(function(e){ $("#tooltip").css({"top":(e.pageY+y)+"px","left":(e.pageX+x)+"px","color":"#00ff00"}); });});</script></head><body><p><a href ="#" class ="tooltip" title ="這是我的超級連結提示1.">提示1.</a></p><p><a href ="#" class ="tooltip" title ="這是我的超級連結提示2.">提示2.</a></p><p><a href ="#" title ="內建提示1.">內建提示1.</a></p><p><a href ="#" title ="內建提示2.">內建提示2.</a></p></body></html>
運行效果如下:
可以看到自訂的提示效果可以實現的效果更好些。而且可以設定的樣式更多,是網頁看起來更加的好看。
2,圖片提示效果
範例程式碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB18030"><title>jQuery test </title><script type ="text/javascript" src ="jquery.js" ></script><script type ="text/javascript">$(document).ready(function(){ var x = 10; var y = 20; $("a.tooltip").mouseover(function(e){ this.myTitle = this.title; this.title =""; var imgTitle =this.myTitle?"<br/>"+this.myTitle:+""; var tooltip ="<div id ='tooltip'><img src='"+this.href+"' alt ='圖片預覽'/>"+this.myTitle+"</div>";//這個地方要特別注意,要給this.href 外面添加單引號 $("body").append(tooltip); //將它追加到文檔中 $("#tooltip").css({"top":(e.pageY+y)+"px","left":(e.pageX+x)+"px","color":"#00ff00"}).show("fast");//設定x座標和y座標,並顯示 }) .mouseout(function(){ this.title = this.myTitle; $("#tooltip").remove(); //移除 }) .mousemove(function(e){ $("#tooltip").css({"top":(e.pageY+y)+"px","left":(e.pageX+x)+"px","color":"#00ff00"}); });});</script></head><body><ul><li><a href ="http://img0.bdstatic.com/img/image/shouye/sheying0617.jpg" class ="tooltip" title ="小鳥"><img src ="http://img0.bdstatic.com/img/image/shouye/sheying0617.jpg" alt ="小鳥照片" /></a></li><li><a href ="http://img0.bdstatic.com/img/image/shouye/bizhi0617.jpg" class ="tooltip" title ="鮮花" ><img src ="http://img0.bdstatic.com/img/image/shouye/bizhi0617.jpg" alt ="鮮花圖片" /></a></li></ul></body></html>
運行效果如下: