通过在一段时间内改变不透明度在 JavaScript 中创建褪色图像
Creating a fading image in JavaScript by changing opacity over a period of time
我是 JavaScript 的新手。我真的很想找出一种简单的技术来改变位于另一个图像之上的图像的不透明度以逐渐显示底部图像。我想通过淡出来做到这一点。换句话说,随着时间的推移,每一秒都逐渐从 style.opacity 属性 中减去。
我想出了以下方法,但它似乎对我不起作用。
我的代码的当前状态发生了变化,不透明度突然从 1 变为负数(-0.1、-0.2、-0.3 等)。这基本上会立即从顶部图像过渡到显示底部图像。
我想随着每一秒的过去逐渐从 1 的不透明度变为(0.9、0.8、0.7 等)。
我想纯粹地完成这个 JavaScript。我在 CSS 中发现了类似的东西,但我不想使用那种方法。
我假设 (var currentOp = document.getElementById('guitar2').style.opacity;)
将 currentOp 变量视为字符串。因此,我尝试了将解析为浮动的组合,进行减法并改回字符串,随着时间的推移逐渐设置不透明度无效。
<!DOCTYPE>
<html>
<head>
<style>
#guitar1
{
position: relative;
top: 0;
left: 0;
}
#guitar2
{
position: absolute;
top: 0px;
left: 0px;
opacity: 1;
}
</style>
</head>
<body onload="runTime()">
<script>
var timer;
function reduceOpacity()
{
var op = document.getElementById('guitar2');
var currentOp = document.getElementById('guitar2').style.opacity;
//currentOp = (parseFloat(currentOp) - 0.1).toString();
currentOp -= 0.1;
op.style.opacity = currentOp;
}
function runTime()
{
timer = setInterval(reduceOpacity, 1000);
}
</script>
<h4 id="header">Gibson Les Paul</h4>
<div style="position: relative; left: 0; top: 0;">
<img src="gibson1.png" id="guitar1" width="500" />
<img src="gibson2.png" id="guitar2" width="500" />
</div>
<button type="button" onclick='document.getElementById("header").innerHTML = "yea"'>Click Me!</button>
</body>
</html>
使用 getComputedStyle
和 getPropertyValue
检索加载时元素样式的准确表示。
style.property
有不更新的习惯,有时候会莫名其妙的拉取默认值,很多值都是设置后才能抓取到的。如果您 console.log(op.style.opacity)
在您的原始示例中,您会看到它拉出一个空字符串,然后在将其转置为 0 后从那里开始倒计时。
然后您可以使用 clearInterval
在该点之后连续 运行 停止函数。
//get the style value with getComputedStyle, which is an accurate representation of
//all element styling on load.
var currentOp = getComputedStyle(op).getPropertyValue("opacity");
// clear the interval if opacity is less than or equal to 0
if(currentOp <= 0) clearInterval(timer);
console.log(currentOp);
<!DOCTYPE>
<html>
<head>
<style>
#guitar1
{
position: relative;
top: 0;
left: 0;
}
#guitar2
{
position: absolute;
top: 0px;
left: 0px;
opacity: 1;
}
</style>
</head>
<body onload="runTime()">
<script>
var timer;
function reduceOpacity()
{
var op = document.getElementById('guitar2');
//get the style value with getComputedStyle, which is an accurate representation of
//all element styling on load.
var currentOp = getComputedStyle(op).getPropertyValue("opacity");
// clear the interval if opacity is less than or equal to 0
if(currentOp <= 0) clearInterval(timer);
console.log(currentOp);
//currentOp = (parseFloat(currentOp) - 0.1).toString();
currentOp -= 0.1;
op.style.opacity = currentOp;
}
function runTime()
{
timer = setInterval(reduceOpacity, 1000);
}
</script>
<h4 id="header">Gibson Les Paul</h4>
<div style="position: relative; left: 0; top: 0;">
<img src="gibson1.png" id="guitar1" width="500" />
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAAP1BMVEWzs7P///+xsbG2traurq7y8vLLy8u+vr7Hx8fX19fExMTh4eHAwMDc3Nzn5+f8/Pzq6ur29vbT09Pv7++oqKiXjvXjAAAHrUlEQVR4nO2d6ZaDIAyFFVBbtVrrvP+zDgTcderGkpzJn6kL5H4QILbqRFHEMxbRNJZx+FvHnCYi43Ut//A6jmuSiEyjRVkc00QEwDjOzAd6iCMumogTKoqIMyZ6iAsiaogrPLQQV2koIW6w0EHcJKGC+AcHDcQ/KSggfmHAj/iVADviDv24EXepx4y4UztexN3KsSIe0I0T8ZBqjIgHNeNDPKwYG+IJvbgQT6nFhHhSKx7E00qxIF7QiQPxkkoMiBc1ho94WWHoiDfoCxvxFnUhI96kLVzE25SFinijrjARb1UVIuLNmsJDvF1RaIgW9ISFaEVNSIiWtISDaE1JKIgWdYSBaFVFC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gUAP+koAH4BwcVwM1ApQO4wUIJcJWGFuBKoFIDXBDRA5wxUQScBCpNwDEXhf99tmqmFzN4/SRFQINYqxdv1tbu9fNsErGGDzyjCSgRM9mDv1k6auXmcfibAAAAAElFTkSuQmCC" id="guitar2" width="500" />
</div>
<button type="button" onclick='document.getElementById("header").innerHTML = "yea"'>Click Me!</button>
</body>
</html>
注意:我不得不替换你的图片,因为它是本地文件。
如果你不喜欢JavaScript,我还有一个选择。 Animate.css 是一个令人难以置信的样式表,它将以更简单的方式做同样的事情。 (加上它是纯粹的 CSS 更好更统一的支持)
在头部导入这个
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css" />
那么这是您的相应代码:
<div style="position: relative; left: 0; top: 0;">
<img src="gibson1.png" id="guitar1" width="500" />
<img src="gibson2.png" id="guitar2" class="animated fadeOut delay-5s" style="animation-duration: 3s;" width="500" />
</div>
可能不是您正在寻找的答案,但它比 JS + JQ 动画要容易得多。
祝一切顺利!
我是 JavaScript 的新手。我真的很想找出一种简单的技术来改变位于另一个图像之上的图像的不透明度以逐渐显示底部图像。我想通过淡出来做到这一点。换句话说,随着时间的推移,每一秒都逐渐从 style.opacity 属性 中减去。
我想出了以下方法,但它似乎对我不起作用。
我的代码的当前状态发生了变化,不透明度突然从 1 变为负数(-0.1、-0.2、-0.3 等)。这基本上会立即从顶部图像过渡到显示底部图像。
我想随着每一秒的过去逐渐从 1 的不透明度变为(0.9、0.8、0.7 等)。
我想纯粹地完成这个 JavaScript。我在 CSS 中发现了类似的东西,但我不想使用那种方法。
我假设 (var currentOp = document.getElementById('guitar2').style.opacity;)
将 currentOp 变量视为字符串。因此,我尝试了将解析为浮动的组合,进行减法并改回字符串,随着时间的推移逐渐设置不透明度无效。
<!DOCTYPE>
<html>
<head>
<style>
#guitar1
{
position: relative;
top: 0;
left: 0;
}
#guitar2
{
position: absolute;
top: 0px;
left: 0px;
opacity: 1;
}
</style>
</head>
<body onload="runTime()">
<script>
var timer;
function reduceOpacity()
{
var op = document.getElementById('guitar2');
var currentOp = document.getElementById('guitar2').style.opacity;
//currentOp = (parseFloat(currentOp) - 0.1).toString();
currentOp -= 0.1;
op.style.opacity = currentOp;
}
function runTime()
{
timer = setInterval(reduceOpacity, 1000);
}
</script>
<h4 id="header">Gibson Les Paul</h4>
<div style="position: relative; left: 0; top: 0;">
<img src="gibson1.png" id="guitar1" width="500" />
<img src="gibson2.png" id="guitar2" width="500" />
</div>
<button type="button" onclick='document.getElementById("header").innerHTML = "yea"'>Click Me!</button>
</body>
</html>
使用 getComputedStyle
和 getPropertyValue
检索加载时元素样式的准确表示。
style.property
有不更新的习惯,有时候会莫名其妙的拉取默认值,很多值都是设置后才能抓取到的。如果您 console.log(op.style.opacity)
在您的原始示例中,您会看到它拉出一个空字符串,然后在将其转置为 0 后从那里开始倒计时。
然后您可以使用 clearInterval
在该点之后连续 运行 停止函数。
//get the style value with getComputedStyle, which is an accurate representation of
//all element styling on load.
var currentOp = getComputedStyle(op).getPropertyValue("opacity");
// clear the interval if opacity is less than or equal to 0
if(currentOp <= 0) clearInterval(timer);
console.log(currentOp);
<!DOCTYPE>
<html>
<head>
<style>
#guitar1
{
position: relative;
top: 0;
left: 0;
}
#guitar2
{
position: absolute;
top: 0px;
left: 0px;
opacity: 1;
}
</style>
</head>
<body onload="runTime()">
<script>
var timer;
function reduceOpacity()
{
var op = document.getElementById('guitar2');
//get the style value with getComputedStyle, which is an accurate representation of
//all element styling on load.
var currentOp = getComputedStyle(op).getPropertyValue("opacity");
// clear the interval if opacity is less than or equal to 0
if(currentOp <= 0) clearInterval(timer);
console.log(currentOp);
//currentOp = (parseFloat(currentOp) - 0.1).toString();
currentOp -= 0.1;
op.style.opacity = currentOp;
}
function runTime()
{
timer = setInterval(reduceOpacity, 1000);
}
</script>
<h4 id="header">Gibson Les Paul</h4>
<div style="position: relative; left: 0; top: 0;">
<img src="gibson1.png" id="guitar1" width="500" />
<img src="data:image/png;base64,iVBORw0K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KwRhv6rputpIOcxTxTxfmx7XtrAr774fkfwMm/zs++XsxyN9PQ/6eKPL3tZG/N5H8/aXk7xEmf583+Xv1yT9vQf6ZGfLPPZF/do3884fknyEl/xww+We5yT+PT/6dCuTfi0H+3Sbk309D/h1D5N8TRf5dX+Tf10b+nXvk35tI/t2XtiV4R7QvwDOiC/deEd0494joyrU3RHeOPSG6dOsF0a1TD4iuXTpHdN+mjj36GBdOffqZ2xx69bU+OfPrL8dw5NlnnujEt99c34F339dr1v37BhxuybWkwD+gZQ0hAFpVEQagxUANBdCaknAALWkJCdBKoIYFaEFPaIC3KwoP8OZADRHwVlVhAt6oK1TA2wI1XMCbtIUMeIu6sAFvCNTQAS8rDB/wokYMgJcCFQfgBZ1YAE8rxQN4MlAxAZ5SiwvwhF5sgIcDFR/gQc0YAQ+pxgl4IFCxAu5Wjhdwp3bMgLsCFTfgDv3YAb8S4Af8EqgUAP+koAH4BwcVwM1ApQO4wUIJcJWGFuBKoFIDXBDRA5wxUQScBCpNwDEXhf99tmqmFzN4/SRFQINYqxdv1tbu9fNsErGGDzyjCSgRM9mDv1k6auXmcfibAAAAAElFTkSuQmCC" id="guitar2" width="500" />
</div>
<button type="button" onclick='document.getElementById("header").innerHTML = "yea"'>Click Me!</button>
</body>
</html>
注意:我不得不替换你的图片,因为它是本地文件。
如果你不喜欢JavaScript,我还有一个选择。 Animate.css 是一个令人难以置信的样式表,它将以更简单的方式做同样的事情。 (加上它是纯粹的 CSS 更好更统一的支持)
在头部导入这个
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css" />
那么这是您的相应代码:
<div style="position: relative; left: 0; top: 0;">
<img src="gibson1.png" id="guitar1" width="500" />
<img src="gibson2.png" id="guitar2" class="animated fadeOut delay-5s" style="animation-duration: 3s;" width="500" />
</div>
可能不是您正在寻找的答案,但它比 JS + JQ 动画要容易得多。
祝一切顺利!