JavaScript DOM Programming Art: Animating effects

Source: Internet
Author: User

See the tenth chapter, according to the specifications of the book successfully wrote a dynamic position conversion.

The example of this book is simple, but what is important is the ideas contained therein.

1 functionmoveelement (elementid,finax,finay,interval)2 {3     if(!document.getelementbyid)return false;4     if(!document.getelementbyid (ElementID))return false;5     varelem=document.getElementById (elementid);6     varxpos=parseint (elem.style.left);7     varypos=parseint (elem.style.top);8     if(xpos==finax&&ypos==Finay)9     {Ten         returnture; One     } A     if(Xpos<finax) xpos++; -     if(Xpos>finax) xpos--; -     if(Ypos<finay) ypos++; the     if(Ypos>finay) ypos--; -elem.style.left=xpos+ "px"; -elem.style.top=ypos+ "px"; -     varrepeat = "moveelement ('" +elementid+ "'," +finalx+ "," +finaly+ "," +interval+ ")"; +Movement=setTimeout (repeat,interval); - } +Addloadevent (moveelement);

The above section of code is a JavaScript function segment. This function is the movement of elements.

The function parameters are: The ID of the element, the final coordinate after the move, and the duration of the move.

The behavior is first judged, if the element ID cannot be read then return FALSE. Otherwise, the initial position of the element is read. The difference between the judgment and the final position.

Then use the SetTimeout function, which is a function that allows a function to start executing after a certain period of time.

Finally, use addloadevent to ensure that this function segment is executed after the entire HTML load.

Can be used in pictures, text and other elements, the idea is the same. Just the details of the different times need to be considered.

JavaScript DOM Programming Art: Animating effects

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.