Hi All,
分享一个通过JS来显示相应图片的详细信息。
需求:进入页面时,动态加载图片信息;当鼠标移动到某一图片上时,则显示该图片的大图片并显示相应说明信息;当鼠标移开图片时,清除新创建的元素。
1. 用HTML画好布局:
1 <body onload="initEvent()">
2 <div id="peopleInfo" style="position:absolute; top:20%; left:30%;">
3 <h2>人员信息:</h2>
4 </div>
5 </body>
2. 用CSS为新创建的元素的添加样式:
<style type="text/css">
.newDiv {
position:absolute;
width:200px;
height:200px;
border: 1px solid blue;
}
.newImg {
position:absolute;
width:100px;
height:100px;
margin: 2px 2px;
}
.newlbName {
float:right;
color:red;
margin-right:40px;
margin-top: 20px;
}
.newlbHight {
float:right;
color:red;
margin-right:-42px;
margin-top: 50px;
}
.lbDescription {
position:absolute;
font: normal 24px Arial, Helvetica, sans-serif;
color:black;
top:110px;
left:20px;
padding: 8px 1px;
}
</style>
3. 用JS控制行为:
<script type="text/javascript">
var data = { // 数据源
"/Imgs/katong_4_1440x900.jpg": ["苏童", "158cm", "温柔,委婉"],
"/Imgs/katong_2_1440x900.jpg": ["紫童", "172cm", "性格率真"],
"/Imgs/katong_5_1440x900.jpg": ["小倩", "165cm", "爱好广泛"],
"/Imgs/katong_3_1440x900.jpg": ["顺姬", "158cm", "美丽,大方"]
}; function initEvent() { // load事件触发initEvent()方法
var div = document.getElementById("peopleInfo");
var table = document.createElement("table");
var count = 0;
for (var key in data) { // 动态加载图片详细
var tr, td, img;
var dataValue = data[key];
td = document.createElement("td");
img = document.createElement("img");
img.id = count;
img.setAttribute("src", key);
img.setAttribute("alt1", key);
img.setAttribute("alt2", dataValue[0]);
img.setAttribute("alt3", dataValue[1]);
img.setAttribute("alt4", dataValue[2]);
img.width = '50';
img.height = '50';
img.onmouseover = function () { // 鼠标移动时动态创建相应图片的详细信息
// 动态创建的div,并设置其属性
var newDiv = document.createElement("div");
newDiv.className = "newDiv";
var x = window.event.clientX;
var y = window.event.clientY;
newDiv.style.top = y+"px";
newDiv.style.left = x + "px"; // 动态创建img,并设置相应属性
var newImg = document.createElement("img");
newImg.className = "newImg";
var imgPath = this.getAttribute("alt1").toString();
newImg.src = imgPath;
var name = this.getAttribute("alt2").toString();
var height = this.getAttribute("alt3").toString();
var description=this.getAttribute("alt4").toString(); // 动态创建label,显示姓名信息
var lbName = document.createElement("label");
lbName.className = "newlbName";
lbName.innerText = name;
newDiv.appendChild(lbName); // 动态创建label,显示身高信息
var lbHight = document.createElement("label");
lbHight.className = "newlbHight";
lbHight.innerText = height;
newDiv.appendChild(lbHight); // 动态创建label,显示描述信息
var lbDescription = document.createElement("label");
lbDescription.className = "lbDescription";
lbDescription.innerText = description;
newDiv.appendChild(lbDescription); // 将创建的元素添加到新的div中
newDiv.appendChild(newImg);
document.body.appendChild(newDiv); }
img.onmouseout = function () { //鼠标离开时,移出掉新创建的div元素
var divs = document.getElementsByTagName("div");
for (var i = 0; i < divs.length; i++)
{
var div = divs[i];
if (div.className == "newDiv")
{
document.body.removeChild(div);
}
}
} td.appendChild(img);
if (count % 2 == 0) { // 设置换行
tr = document.createElement("tr");
}
tr.appendChild(td);
table.appendChild(tr);
count++;
}
div.appendChild(table);
}
</script>
4. 显示结果:
一个JS版的例子就好了,大家可以尝试一下 :).