Js animation Learning (4)

Source: Internet
Author: User
The previous section describes how to independently change a single attribute. Starting from this section, we will introduce multiple attribute encapsulation functions. A function can change multiple attribute values. VII. Multi-attribute encapsulation Functions



The previous section describes how to independently change a single attribute. Starting from this section, we will introduce multiple attribute encapsulation functions. A function can change multiple attribute values.

First, we will introduce a very important function getStyle (), which returns the current attribute value of an element. The two parameters are element and attribute. Pay attention to browser compatibility issues.

// Obtain the special encapsulation function of the element style and return the current value of this attribute. function getStyle (obj, attr) {if (obj. currentStyle) {// IE browser return obj. currentStyle [attr];} else {return getComputedStyle (obj, false) [attr]; // Firefox browser }}

Next we will focus on a function to implement animation that changes multiple attributes:

// Multiple objects change the style (width, height, font size, border, etc., transparency needs to be analyzed separately) Speed Buffer encapsulation function startMove (obj, attr, target) {// element, changed the style attribute to reach the target value clearInterval (obj. timer); // first clear the timer obj. timer = setInterval (function () {// 1. take the current value var icur = 0; // icur returns the size of the object style attribute value if (attr = 'opacity ') {// if the attribute is transparent, the return value of transparency is the decimal point of the zero point icur = Math. round (parseFloat (getStyle (obj, attr) * 100); // The round function prevents the opacity from beating back and forth between decimal places} else {icur = parseInt (getStyle (obj, attr);} // 2. calculation speed var speed = (target -Icur)/8; // the denominator is the proportional coefficient K, adjustable speed = speed> 0? Math. ceil (speed): Math. floor (speed); // The buffer speed should be rounded up, otherwise it will stop when the end point is not moved. // 3. checks whether the motion stops if (icur = target) {clearInterval (obj. timer);} else {if (attr = 'opacity ') {obj. style. filter = 'Alpha (opacity: '+ (icur + speed) +') '; // IE browser obj. style. opacity = (icur + speed)/100; // Firefox} else {obj. style [attr] = icur + speed + 'px ';}}, 30 )}

We use two p to verify:

 

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.