Home  >  Article  >  Web Front-end  >  Detailed explanation of the steps to manipulate the transparency of objects in JS

Detailed explanation of the steps to manipulate the transparency of objects in JS

php中世界最好的语言
php中世界最好的语言Original
2018-04-13 10:58:341415browse

This time I will give you a detailed explanation of the steps for operating the transparency of an object with JS. What are the precautions for operating the transparency of an object with JS? The following is a practical case, let's take a look.

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

<script>
  window.onload = function () {
    var op = document.getElementById('p1');
    op.onmousemove = function () {
      startMove(100);
    }
    op.onmouseout = function () {
      startMove(30);
    }
}
var timer = null;
function startMove(iTarget) {
    clearInterval(timer);
    var op = document.getElementById('p1');
    timer = setInterval(function(){
      if(op.offsetAlpha == iTarget){
        ....
      }
    },30);
}
</script>
But in js there are only offsetLeft/Top, offsetWidth/

Height, these four methods, and there is no offsetAlpha method.

Question: So how do we get the transparency of the current object? ?

We can define a variable ourselves var alpha = 30; by judging whether this variable is equal to the target value, we can continue our next operation;

var alpha = 30; // 自定义一个变量
When the alpha and other goals are worth it, clear the

timer, otherwise change the transparency value alpha

if(alpha == iTarget){
   clearInterval(timer);
}else{
   alpha += iSpeed;
   op.style.opacity = alpha/100;
   op.style.filter = 'alpha(opacity:'+alpha+')';
}
The complete code is as follows:

<p id="p1"></p>
css style part:

<style>
    #p1{
      width: 100px;height: 100px;
      background: green;
      opacity:0.3;
      filter:alpha(opacity:30);/*兼容低版本IE*/
    }
</style>
js part:

<script>
  window.onload = function () {
    var op = document.getElementById('p1');
    op.onmousemove = function () {
      startMove(100);
    }
    op.onmouseout = function () {
      startMove(30);
    }
  }
  var timer = null;
  var alpha = 30;
  function startMove(iTarget) {
    clearInterval(timer);
    var op = document.getElementById('p1');
    var iSpeed = 0;
    timer = setInterval(function(){
      if(alpha>iTarget){
        iSpeed = -10;
      }else{
        iSpeed = 10;
      }
      if(alpha == iTarget){
        clearInterval(timer);
      }else{
        alpha += iSpeed;
        op.style.opacity = alpha/100;
        op.style.filter = 'alpha(opacity:'+alpha+')';
      }
    },30);
  }
</script>
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to implement multi-object movement in JS

Steps to achieve seamless scrolling effect with vue.js Detailed explanation

The above is the detailed content of Detailed explanation of the steps to manipulate the transparency of objects in JS. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn