本文實例講述了jquery實現鼠標滑過小圖時顯示大圖的方法。分享給大家供大家參考。具體實現方法如下:
代碼如下:<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title></title>
</head>
<style type="text/css">
li{list-style:none;float:left;margin-left:10px;}
</style>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type="text/javascript">
var data = {
"images/11_s.jpg":["images/11_b.jpg","美人1"],
"images/22_s.jpg":["images/22_b.jpg","美人2"],
"images/33_s.jpg":["images/33_b.jpg","美人3"],
"images/44_s.jpg":["images/44_b.jpg","美人4"]
};
$(function(){
$.each(data,function(key,value){
//初始化最後一個div為隱藏
$("div").last().hide();
//創建小圖的節點
var smallPath = $("<img src='" + key + "' />").css({"margin":"5px","padding":"2px","border":"1px solid #000"});
//設置大圖地址和名稱
bigImgPath = smallPath.attr("bigMapPath",value[0]);
bigImgName = smallPath.attr("bigMapName",value[1]);
$("div").first().append(smallPath);
//小圖上添加事件
smallPath.mouseover(function(){
//最後一個div淡入效果
$("div").last().fadeIn("fast");
//獲取大圖地址
$("#show").attr("src",$(this).attr("bigMapPath"));
//獲取大圖名稱並設置樣式
$("#imgTitle").text($(this).attr("bigMapName")).css({"background":"#ebf1de","padding":"10px","margin-bottom":"10px"});
});
smallPath.mouseout(function(){
$("div").last().fadeOut("fast");
});
});
});
</script>
<body>
<div></div>
<div>
<img id="show" src="" />
</div>
</body>
</html>
希望本文所述對大家的jQuery程序設計有所幫助。