Function attributes for setting the transparency of DOM nodes in js: filter= "alpha(opacity=" value ")" (compatible with IE) and opacity=value/100 (compatible with FF and GG).
Let’s first look at the compatibility code for setting transparency:
function setOpacity(ele, opacity) {
if (ele.style.opacity != undefined) {
///Compatible with FF, GG and new version IE
ele.style.opacity = opacity / 100;
} else {
///Compatible with older versions ie
ele.style.filter = "alpha(opacity=" opacity ")";
}
}
Some friends write like this:
function setOpacity(ele, opacity) {
if (document.all) {
///Compatible with ie
ele.style.filter = "alpha(opacity=" opacity " )";
}
ele {
///Compatible with FF and GG
ele.style.opacity = opacity / 100;
}
}
I want to say that there is a problem when running under IE10. There is no response after clicking. Because IE10 supports the opacity attribute but not filter, this method is not advisable.
fadein function code:
function fadein (ele, opacity, speed) {
if (ele) {
var v = ele.style.filter.replace("alpha(opacity=", "").replace(")", "") || ele.style.opacity;
v < 1 && (v = v * 100);
var count = speed / 1000;
var avg = count < 2 ? (opacity / count) : (opacity / count - 1);
var timer = null;
timer = setInterval(function() {
if (v < opacity) {
v = avg;
setOpacity (ele, v);
} else {
clearInterval(timer);
}
}, 500);
}
}
fadeout Function code:
function fadeout(ele, opacity, speed) {
if (ele) {
var v = ele.style.filter.replace("alpha(opacity=", "").replace(")", "") || ele.style.opacity || 100;
v < 1 && (v = v * 100);
var count = speed / 1000;
var avg = (100 - opacity) / count;
var timer = null;
timer = setInterval(function() {
if (v - avg > opacity) {
v -= avg;
setOpacity(ele, v);
} else {
clearInterval(timer);
}
}, 500);
}
}
The following is a demo example:
If you have any better implementation, please leave a message. . .
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