淡入淡出 javascript
fade in image javascript
我是 JavaScript 的新手,我正在尝试编写一个代码,用我熟悉的命令淡入图片。我在这里看到了几个例子,但它们没有用。这是我尝试做的:
function myFunction() {
for (i = 1; i < 20; i++) {
setTimeout(function() {
o = 1 - 1 / i
}, 200); //this should increase the opacity
document.getElementById("dog").style.opacity = o
}
}
img {
opacity: 0;
filter: alpha(opacity=40);
}
<center>
<img id="dog" src="dog.jpg" draggable="true" ondragstart="drag(event)" width="500" height="500">
</center>
<button onclick="myFunction()">Lets Rock</button>
当我 运行 它时,它不会淡入。它从空白屏幕开始(它应该如此),但不是在我单击按钮后淡入,而是弹出 (不淡入)点击几下后。
非常感谢您给予的帮助
爱丽儿
您可能会发现使用 CSS 更容易控制不透明度。它对浏览器的工作较少,只需要很少的 javascript 来切换 class.
var img = document.getElementById("dog"),
btn = document.getElementById("button");
btn.addEventListener("click", function( event ) {
img.className = (img.className==="in")?"out":"in";
}, false);
#dog {
opacity: 0;
-webkit-transition: opacity 2s ease;
-moz-transition: opacity 2s ease;
-ms-transition: opacity 2s ease;
-o-transition: opacity 2s ease;
transition: opacity 2s ease;
}
#dog.in {
opacity: 1;
}
<button id="button">Lets Rock</button><br />
<img id="dog" src="http://lorempixel.com/500/500/cats/" class="off" draggable="true" ondragstart="drag(event)" width="500" height="500">
试试这个:http://jsfiddle.net/kishoresahas/4wg8zcsg/
function fadeIn(el) {
el.style.opacity = 0;
var tick = function () {
el.style.opacity = +el.style.opacity + 0.01;
if (+el.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16)
}
};
tick();
}
function myFunction() {
var el = document.getElementById("dog");
console.log(el);
fadeIn(el);
}
img {
opacity: 0;
filter: alpha(opacity=40);
}
<button onclick="myFunction()">Lets Rock</button>
<center>
<img id="dog" src="https://i.stack.imgur.com/NHlV8.jpg" draggable="true" ondragstart="drag(event)" width="500" height="500">
</center>
我是 JavaScript 的新手,我正在尝试编写一个代码,用我熟悉的命令淡入图片。我在这里看到了几个例子,但它们没有用。这是我尝试做的:
function myFunction() {
for (i = 1; i < 20; i++) {
setTimeout(function() {
o = 1 - 1 / i
}, 200); //this should increase the opacity
document.getElementById("dog").style.opacity = o
}
}
img {
opacity: 0;
filter: alpha(opacity=40);
}
<center>
<img id="dog" src="dog.jpg" draggable="true" ondragstart="drag(event)" width="500" height="500">
</center>
<button onclick="myFunction()">Lets Rock</button>
当我 运行 它时,它不会淡入。它从空白屏幕开始(它应该如此),但不是在我单击按钮后淡入,而是弹出 (不淡入)点击几下后。
非常感谢您给予的帮助
爱丽儿
您可能会发现使用 CSS 更容易控制不透明度。它对浏览器的工作较少,只需要很少的 javascript 来切换 class.
var img = document.getElementById("dog"),
btn = document.getElementById("button");
btn.addEventListener("click", function( event ) {
img.className = (img.className==="in")?"out":"in";
}, false);
#dog {
opacity: 0;
-webkit-transition: opacity 2s ease;
-moz-transition: opacity 2s ease;
-ms-transition: opacity 2s ease;
-o-transition: opacity 2s ease;
transition: opacity 2s ease;
}
#dog.in {
opacity: 1;
}
<button id="button">Lets Rock</button><br />
<img id="dog" src="http://lorempixel.com/500/500/cats/" class="off" draggable="true" ondragstart="drag(event)" width="500" height="500">
试试这个:http://jsfiddle.net/kishoresahas/4wg8zcsg/
function fadeIn(el) {
el.style.opacity = 0;
var tick = function () {
el.style.opacity = +el.style.opacity + 0.01;
if (+el.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16)
}
};
tick();
}
function myFunction() {
var el = document.getElementById("dog");
console.log(el);
fadeIn(el);
}
img {
opacity: 0;
filter: alpha(opacity=40);
}
<button onclick="myFunction()">Lets Rock</button>
<center>
<img id="dog" src="https://i.stack.imgur.com/NHlV8.jpg" draggable="true" ondragstart="drag(event)" width="500" height="500">
</center>