Javascript multi-object arbitrary motion frame

Source: Internet
Author: User

Javascript multi-object arbitrary motion frame

Each complex motion effect is composed of a simple effect. We can develop a framework for a simple effect, that is, to write a general function, so that we can transform any attribute of the element, complex effects can be easily achieved.

Note:

When you change the transparency of an element, special processing is required. If you change the width of an element, for an element with a border, if we use obj. when offsetWidth is used to obtain the element width, an error occurs because offsetWidth includes the border. If the border is 1px, called in the timer function, obj. style. width = obj. offsetWidth-1 + 'px ';, we will find that the width of the element is not reduced, but will increase. This is also why we use our custom getStyle () to get the element width, instead of using the offsetWidth attribute.

 New Web Project<Script> function getStyle (obj, name) {if (obj. currentStyle) {return obj. currentStyle [name];} else {return getComputedStyle (obj, null) [name];} window. onload = function () {var objs = document. getElementsByTagName ('div '); objs [1]. onmouseover = function () {startMove (this, 'width', 400) ;}; objs [1]. onmouseout = function () {startMove (this, 'width', 200) ;}; objs [0]. onmouseover = function () {startMove (this, 'height', 400) ;}; objs [0]. onmo Useout = function () {startMove (this, 'height', 200) ;}; objs [2]. onmouseover = function () {startMove (this, 'opacity ', 100) ;}; objs [2]. onmouseout = function () {startMove (this, 'opacity ', 30) ;};}; function startMove (obj, atrr, itarget) {clearInterval (obj. timer); obj. timer = setInterval (function () {var nowValue; if (atrr = 'opacity ') {nowValue = Math. round (parseFloat (getStyle (obj, atrr) * 100); // an error occurs in the computer operation. You need to take the approximate document. title = nowValue;} Else {nowValue = parseInt (getStyle (obj, atrr);} var speed = (itarget-nowValue)/5; speed = speed> 0? Math. ceil (speed): Math. floor (speed); if (nowValue = itarget) {clearInterval (obj. timer);} else {if (atrr = 'opacity ') {obj. style [atrr] = (nowValue + speed)/100; obj. style. filter = 'Alpha (opacity: '+ (nowValue + speed) +') '; // IE earlier version settings} else {obj. style [atrr] = nowValue + speed + 'px ';}}, 30) ;}</script>

Run:


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.