1.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Web Design</title> <link rel="stylesheet" type="text/css" href="styles/list.css"> </head> <body> <h1>Web Design</h1> <p>These are the things you should know</p> <ol id="linklist"> <li> <a href="images/shuangyu.jpg">shuangyu</a> </li> <li> <a href="images/baiyang.jpg">baiyang</a> </li> <li> <a href="images/jinniu.jpg">jinniu</a> </li> <li> <a href="images/shuangzi.jpg">shuangzi</a> </li> </ol> <script type="text/javascript" src="scripts/prepareSlideshow.js"></script> </body> </html>
2.css
#slideshow{ width: 100px; height: 100px; position: relative; overflow: hidden; } #preview{ position: absolute; }
3.js
function addLoadEvent(func) { var oldonload = window.onload; if(typeof window.onload != "function"){ window.onload = func; }else{ window.onload = function(){ oldonload(); func(); } } } function insertAfter(newElement, targetElement){ var parent = targetElement.parentNode; if(parent.lastChild == targetElement){ parent.appendChild(newElement); }else{ parent.insertBefore(newElement, targetElement.nextSibling); } } function moveElement(elementID, final_x, final_y, interval){ if(!document.getElementById) return false; if(!document.getElementById(elementID)) return false; var elem = document.getElementById(elementID); if(elem.movement){ clearTimeout(elem.movement); } if(!elem.style.left){ elem.style.left = "0px"; } if(!elem.style.top){ elem.style.top = "0px"; } var xpos = parseInt(elem.style.left); var ypos = parseInt(elem.style.top); var dist = 0; if(xpos == final_x && ypos == final_y){ return true; } if(xpos < final_x){ dist = Math.ceil((final_x - xpos) / 10); xpos = xpos + dist; } if(xpos > final_x){ dist = Math.ceil((xpos - final_x) / 10); xpos = xpos - dist; } if(ypos < final_y){ dist = Math.ceil((final_y - xpos) / 10); ypos = ypos + dist; } if(ypos > final_y){ dist = Math.ceil((xpos - final_y) / 10); ypos = ypos - dist; } elem.style.left = xpos + "px"; elem.style.top = ypos + "px"; var repeat = "moveElement(‘"+elementID+"‘, "+final_x+", "+final_y+", "+interval+")"; elem.movement = setTimeout(repeat, interval); } function prepareSlideshow(){ if(!document.getElementsByTagName) return false; if(!document.getElementById) return false; if(!document.getElementById("linklist")) return false; var slideshow = document.createElement("div"); slideshow.setAttribute("id", "slideshow"); var preview = document.createElement("img"); preview.setAttribute("src", "images/shuangbaijinshuangzi.gif"); preview.setAttribute("alt", "buliding blocks of web design"); preview.setAttribute("id", "preview"); slideshow.appendChild(preview); var list = document.getElementById("linklist"); insertAfter(slideshow, list); var links = list.getElementsByTagName("a"); links[0].onmouseover = function(){ moveElement("preview", 0, 0, 10); } links[1].onmouseover = function(){ moveElement("preview", -100, 0, 10); } links[2].onmouseover = function() { moveElement("preview", -200, 0, 10) } links[3].onmouseover = function() { moveElement("preview", -300, 0, 10) } } addLoadEvent(prepareSlideshow);
时间: 2024-10-25 14:11:51