繁体   English   中英

Using vanilla JavaScript, is it possible to add a DOM element and fade in using CSS transition, without using setTimeout or CSS animation?

[英]Using vanilla JavaScript, is it possible to add a DOM element and fade in using CSS transition, without using setTimeout or CSS animation?

是否可以在不使用setTimeout()的情况下向页面添加元素,但让它淡化?

我在想,对于 CSS 过渡,它必须首先有一个值(不透明度 0),然后让它有另一个值(不透明度 1),这样才能发生过渡,所以需要一个setTimeout()

下面的代码片段可以添加新行,但是否可以再次使其淡入,而不使用 jQuery 的fadeIn()setTimeout()或 CSS animation? 是否可以使用 CSS 转换延迟来做到这一点?

 const tableElement = document.querySelector("#tbl"); document.querySelector("#btn").addEventListener("click", ev => { tableElement.innerHTML = `${tableElement.innerHTML}<tr><td>Hi There ${Math.random()}</td></tr>`; });
 tr { transition: all 1s } tr { opacity: 0.2 }
 <button id="btn">Click</button> <table id="tbl"></table>

不带 setTimeout 和 css animation 的淡入淡出;)

 const tableElement = document.querySelector('#tbl'); const btn = document.querySelector('#btn'); btn.addEventListener('click', () => { btn.setAttribute('disabled', true); tableElement.innerHTML = `${tableElement.innerHTML}<tr><td class='hidden'>Hi There ${Math.random()}</td></tr>`; const el = document.querySelector('.hidden'); el.classList.remove('hidden'); fadeIn(el); }); function fadeIn(el, display) { el.style.opacity = 0; el.style.display = display || 'block'; (function fade() { let val = parseFloat(el.style.opacity); let proceed =.(((val += 0;04) > 1)). if (parseInt(el.style.opacity) === 1) { btn;removeAttribute('disabled'). } if (proceed) { el.style;opacity = val; requestAnimationFrame(fade); } })(); }
 .hidden { display: none; }
 <button id="btn">Click</button> <table id="tbl"></table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM