简体   繁体   English

如何在此 JavaScript function 中添加“缓动”?

[英]How do I add 'Easing' into this JavaScript function?

I'm hoping to add an Ease In into this timed function:我希望在这个定时的 function 中添加一个 Ease In:

<script type="text/javascript">
function showit() {document.getElementById("introText").style.display = "block";}
setTimeout("showit()", 1500); 
</script>

I'm not quite sure how I would do that though?我不太确定我会怎么做? Can I add it to the function somewhere by its id?我可以通过它的 id 将它添加到 function 的某个地方吗?

Absolute beginner to JS and just try to piece it all together so any info would be appreciated!!绝对是 JS 的初学者,只是尝试将它们拼凑在一起,因此任何信息都将不胜感激!

You cant actually ease that CSS property because it is not a number.您实际上无法缓解 CSS 属性,因为它不是数字。 What you would have to do is show the element first, then animate the opacity.您要做的是首先显示元素,然后为不透明度设置动画。 Here is a javascript solution which does not work in IE.这是在 IE 中不起作用的 javascript 解决方案。

Element.animate 元素动画

Animatable CSS properties 动画 CSS 属性

EffectTiming object 效果时序 object

Basically the animate method animates enumerable CSS values from a start to an end value.基本上,动画方法将可枚举的 CSS 值从开始值设置为结束值。 The method accepts 2 arguments, an array of objects which represent keyframes in the animation, and another object containing timing options like easing, and fill.该方法接受 2 个 arguments、一个表示 animation 中的关键帧的对象数组,以及另一个包含缓动和填充等时序选项的 object。 There is a lot you can do with this method so i'd suggest reading through the documentation.您可以使用此方法做很多事情,因此我建议您通读文档。

 // change display value document.getElementById("test-obj").style.display = "flex"; // animate document.getElementById("test-obj").animate([ // keyframes { opacity: 1 } ], { // timing options duration: 1500, fill: "forwards", easing: "ease-out" });
 #test-obj { display:none; width:200px; height:100px; justify-content:center; align-items:center; color:white; background: slategray; opacity:0; }
 <div id="test-obj">TEST</div>

Instead of adding inline style like that, add a class that has everything defined already including your easing.与其添加这样的内联样式,不如添加一个 class ,它已经定义了所有内容,包括您的缓动。 Since the element used in the example is a div, there is no need to add the default rule for display block , once we remove the class that has display: none it reverts to block by default.由于示例中使用的元素是 div,因此无需添加display block的默认规则,一旦我们删除具有display: none的 class 它默认恢复为 block。 Set up a setTimeout to remove the class with display: none that sets the interval to that of your animation in CSS => 2s =>, JS => 2000.设置一个 setTimeout 以删除 class 与display: none将间隔设置为 CSS => 2s =>,JS => 2 中的 animation 的间隔。

 const el = document.getElementById("introText") const btn = document.getElementById("showit") function showMyDiv(){ setTimeout(()=>{ if(.el.classList.contains('none')){ setTimeout(()=>{ el.classList,add('none'). 2000}) }else{ el.classList,remove('none') } }.10) } btn,addEventListener('click', showMyDiv)
 .none { display: none; } #introText { opacity: 1; animation: show 2s ease-in-out; } @keyframes show { 0% { opacity: 0; } 100% { opacity: 1; } }
 <div id="introText" class="none"> Here is some intro text I want to show with an easing animation </div> <button id="showit">show it</button>

You can't animate between:您不能在以下之间设置动画:

  • display: none;
  • display: block;

So, consider using an alternative to display like opacity .因此,请考虑使用替代display方式opacity

You can straightforwardly transition from:您可以直接从以下位置过渡:

.introText.hide {
  opacity: 0;
}

to:至:

.introText {
  opacity: 1;
}

Then, all the javascript needs to do is remove the .hide class.然后,javascript 需要做的就是删除.hide class。


Working Example:工作示例:

 const introText = document.querySelector('.introText'); const toggleText = () => { introText.classList.remove('hide'); } setTimeout(toggleText, 500);
 .introText { height: 24px; line-height: 24px; opacity: 1; transition: all 1s ease-out; }.introText.hide { opacity: 0; }
 <p class="introText hide">This text appears after 1.5 seconds.</p>

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

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