How to change the transparency of a single object in JS
This article mainly introduces the method of JS motion to change the transparency of a single object, and analyzes the relevant operating techniques for dynamic modification of page element attributes in the form of examples. Friends in need can refer to it
The examples in this article describe JS motion How to change the transparency of a single object. Share it with everyone for your reference, the details are as follows:
In addition to achieving the object movement effect by changing the width, height, letf, top position or movement direction 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 four methods: offsetLeft/Top, offsetWidth/Height, and there is no offsetAlpha method.
Question: So how do we get the transparency of the current object? ?
We can define a variable var alpha = 30; by judging whether this variable is equal to the target value, we can continue our next operation;
var alpha = 30; // 自定义一个变量
When 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>
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
What are the methods to add new properties of objects to the detection sequence in vue?
How to achieve the animation effect of bouncing off the edge in jQuery?
How to use sass in vue cli webpack (detailed tutorial)
Implement the method of adding and assigning tag sub-elements in jQuery
How to generate random numbers in JS (detailed tutorial)
How to change the page color in JS (detailed tutorial)
The above is the detailed content of How to change the transparency of a single object in JS. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

In the operation process of the Meituan Takeout Merchant Edition, the setting of the starting delivery price is a crucial link. A reasonable starting delivery price can not only help merchants control costs, but also increase order amounts to a certain extent, thus increasing overall revenue. However, many merchants don’t know much about how to modify the minimum delivery price. So in the following article, the editor of this website will bring you detailed starting price setting guide for merchants. If you want to know more, come to the following article to find out! In the Meituan Takeout Merchant Center, log in and enter the store settings, then select store management. In the switch navigation at the top of the store management page, select delivery information, and then click Add Delivery Area to complete the operation. Once you add a location, the corresponding shipping costs will automatically appear. After completing your order, you will receive

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP

With the rapid development of Internet finance, stock investment has become the choice of more and more people. In stock trading, candle charts are a commonly used technical analysis method. It can show the changing trend of stock prices and help investors make more accurate decisions. This article will introduce the development skills of PHP and JS, lead readers to understand how to draw stock candle charts, and provide specific code examples. 1. Understanding Stock Candle Charts Before introducing how to draw stock candle charts, we first need to understand what a candle chart is. Candlestick charts were developed by the Japanese
