gpt4 book ai didi

javascript - 纯 JavaScript 淡入功能

转载 作者:可可西里 更新时间:2023-11-01 02:14:30 25 4
gpt4 key购买 nike

嗨, friend 们,当我点击另一个 div 时,我想在一个 div 中淡入淡出,为此我正在使用以下代码。 Code1 工作正常,但我需要使用 Code2

我知道有 jQuery 但我需要在 JavaScript 中执行此操作

你能告诉我我犯了什么错误或者我需要改变什么吗...

代码 1 --- 工作正常

function starter() { fin(); }

function fin()
{
for (i = 0; i <= 1; i += 0.01)
{
i=Math.round(i*100)/100;
setTimeout("seto(" + i + ")", i * 1000);
}
}

function seto(opa)
{
var ele = document.getElementById("div1");
ele.style.opacity = opa;
}

Code2 --- 不起作用

function starter()
{
var ele = document.getElementById("div1");
fin(ele);
}
function fin(ele)
{
for (i = 0; i <= 1; i += 0.01)
{
i=Math.round(i*100)/100;
setTimeout("seto(" + ele + "," + i + ")", i * 1000);
}
}

function seto(ele,opa)
{
ele.style.opacity = opa;
}

最佳答案

基于 this网站

EDIT-1
添加了用户可以指定动画持续时间的功能(@Marzian 评论)

你可以试试这个:

function fadeIn(el, time) {
el.style.opacity = 0;

var last = +new Date();
var tick = function() {
el.style.opacity = +el.style.opacity + (new Date() - last) / time;
last = +new Date();

if (+el.style.opacity < 1) {
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16);
}
};

tick();
}

var el = document.getElementById("div1");
fadeIn(el, 3000); //first argument is the element and second the animation duration in ms

DEMO

关于javascript - 纯 JavaScript 淡入功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23244338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com