js编程艺术小制作五

Posted 天秤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js编程艺术小制作五相关的知识,希望对你有一定的参考价值。

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);

 

以上是关于js编程艺术小制作五的主要内容,如果未能解决你的问题,请参考以下文章

js 编程艺术小制作四

JavaScript编程艺术-第7章代码汇总

VSCode自定义代码片段——JS中的面向对象编程

VSCode自定义代码片段9——JS中的面向对象编程

读书笔记《Unix编程艺术》五

读书笔记《Unix编程艺术》五