JS motion changes the transparency of a single object, and js Motion Object transparency

Source: Internet
Author: User

JS motion changes the transparency of a single object, and js Motion Object transparency

This article describes how JS motion changes the transparency of a single object. We will share this with you for your reference. The details are as follows:

In addition to changing the width, height, letf, top position, or motion direction of an object, changing the transparency of an object is also a special motion effect.

<script>  window.onload = function () {    var oDiv = document.getElementById('div1');    oDiv.onmousemove = function () {      startMove(100);    }    oDiv.onmouseout = function () {      startMove(30);    }}var timer = null;function startMove(iTarget) {    clearInterval(timer);    var oDiv = document.getElementById('div1');    timer = setInterval(function(){      if(oDiv.offsetAlpha == iTarget){        ....      }    },30);}</script>

However, there are only offsetLeft/Top, offsetWidth/Height methods in js, and there is no offsetAlpha method.

Q: How can we obtain the transparency of the current object ??

We can define a variable var alpha = 30 by ourselves. We can continue our next step by judging whether the variable is equal to the target value;

Var alpha = 30; // customize a variable

When the target such as alpha is worthwhile, the timer is clear; otherwise, the transparency value alpha is changed.

if(alpha == iTarget){   clearInterval(timer);}else{   alpha += iSpeed;   oDiv.style.opacity = alpha/100;   oDiv.style.filter = 'alpha(opacity:'+alpha+')';}

The complete code is as follows:

<div id="div1"></div>

Css style section:

<Style> # div1 {width: 100px; height: 100px; background: green; opacity: 0.3; filter: alpha (opacity: 30 ); /* compatible with earlier IE versions */} </style>

Js section:

<script>  window.onload = function () {    var oDiv = document.getElementById('div1');    oDiv.onmousemove = function () {      startMove(100);    }    oDiv.onmouseout = function () {      startMove(30);    }  }  var timer = null;  var alpha = 30;  function startMove(iTarget) {    clearInterval(timer);    var oDiv = document.getElementById('div1');    var iSpeed = 0;    timer = setInterval(function(){      if(alpha>iTarget){        iSpeed = -10;      }else{        iSpeed = 10;      }      if(alpha == iTarget){        clearInterval(timer);      }else{        alpha += iSpeed;        oDiv.style.opacity = alpha/100;        oDiv.style.filter = 'alpha(opacity:'+alpha+')';      }    },30);  }</script>

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.