我目前有一个小型的 JS 画廊(onmouseover)。代码如下。我想要实现的是小图像,一次只显示几个。我找到了我想要达到的效果的 youtube 视频:视频
HTML(代码省略):
<div id="imagecontent">
<img id="about_mojang" src="images/about_mojang_small.jpg" alt="Plain Mojang Logo">
<img id="about_lego" src="images/about_lego_small.jpg" alt="Minecraft Lego">
<img id="about_cteam" src="images/about_cteam_small.jpg" alt="Cartoon of the Mojang team">
<img id="about_minecraftmojang" src="images/about_minecraftmojang_small.jpg" alt="Minecraft logo of Mojang">
<img id="about_team" src="images/about_team_small.jpg" alt="Photo of the Moajgn team">
<img id="about_wall" src="images/about_wall_small.jpg" alt="A wall in the Mojang office">
</div>
<div id="bigimage"></div>
<script>window.onload=addImages() ;</script>
</body>
JS(代码省略):
function showPic(i_element) {
var source = i_element.getAttribute("id") ;
source = "images/"+source+".jpg" ;
var alt = i_element.getAttribute("alt") ;
var i = document.createElement("img") ;
i.setAttribute("src",source) ;
i.setAttribute("alt",alt) ;
var placeholder = document.getElementById("bigimage") ;
if (placeholder.childNodes.length != 0 )
{ placeholder.removeChild(placeholder.childNodes[0]); }
placeholder.appendChild(i) ;
}
// add the onclick event to the DOM
function addImages() {
var item = document.getElementById("imagecontent").getElementsByTagName("img") ;
for (var i=0 ; i<item.length ; i++) {
item[i].onmouseover = function() {showPic(this) ; } ;
}
}