JavaScript 動畫效果

來源:互聯網
上載者:User

JavaScript 動畫效果
JavaScript 動畫效果

一:簡介

通過JavaScript動態改變元素位置來實現動畫效果。執行個體、滑鼠移動到連結、在下方顯示對應圖片。主要是網頁的分層結構思想的應用。

二:重點

        CSS中元素的position屬性、overflow屬性        varvariable = setTimeout("function", interval);//定時觸發函數        clearTimeout(variable);//清除定時觸發函數、variable是setTimeout的傳回值。        parseInt(str);//將字串轉換成整數        parseFloat(str);//將字串轉換成浮點數        Math.ceil(number);//向上取整        Math.floor(number);//向下取整        Math.round(number);//四捨五入

三:實現

效果:


list.html:

        Web Design        <script src="../js/moveElementImprove.js"></script>    <script src="../js/list.js"></script>    <script src="../js/addLoadEvent.js"></script>Web Design

These are the things you should know.

  1. Structure
  2. Presentation
  3. Behavior

list.css:

#slideShow {    width: 100px;    height: 100px;    position: relative;    overflow: hidden;}#preview {    position: absolute;}

list.js:

/** * Created by andychen on 2015/1/9. */function prepareSlideShow() {    var div = document.createElement("div");    div.id = "slideShow";    var img = document.createElement("IMG");    img.id = "preview";    img.src = "../images/preview.png";    img.alt = "building blocks of web design";    div.appendChild(img);    insertAfter(document.getElementById("linkList"), div);    var links = document.getElementsByTagName("a");    links[0].onmouseover = function () {        moveElementImprove("preview", -100, 0, 10);    };    links[1].onmouseover = function () {        moveElementImprove("preview", -200, 0, 10);    };    links[2].onmouseover = function () {        moveElementImprove("preview", -300, 0, 10);    }}function insertAfter(targetTag, newTag) {    var parent = targetTag.parentNode;    if (parent.lastChild == targetTag) {        parent.appendChild(newTag);    } else {        parent.insertBefore(newTag, targetTag.nextSibling);    }}

moveElementImprove.js:

/** * Created by andychen on 2015/1/9. */function moveElementImprove (elementId, final_x, final_y, interval) {    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 topPosition = parseInt(elem.style.top);    var leftPosition = parseInt(elem.style.left);    var dist = 0;    if (topPosition < final_y) {        dist = Math.ceil((final_y - topPosition) / 10);        topPosition += dist;    }    if (topPosition > final_y) {        dist = Math.ceil((topPosition - final_y) / 10);        topPosition -= dist;    }    if (leftPosition < final_x) {        dist = Math.ceil((final_x - leftPosition) / 10);        leftPosition += dist;    }    if (leftPosition > final_x) {        dist = Math.ceil((leftPosition - final_x) / 10);        leftPosition -= dist;    }    if (topPosition == final_y && leftPosition == final_x) {        return true;    }    elem.style.top = topPosition + 'px';    elem.style.left = leftPosition + 'px';    var repeat = "moveElementImprove('"+elementId+"', "+final_x+", "+final_y+", "+interval+")";    elem.movement = setTimeout(repeat, interval);}

addLoadEvent.js 前文已經提到。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.