简体   繁体   English

如何让图像每隔 x 秒重复显示和隐藏?

[英]How do I make an image repeatedly show and hide every x seconds?

I have an image that I want to show for 10 seconds and then hide for 10 seconds and then show for 10 seconds and hide for 10 seconds and do that process repeatedly forever, but I don't know how... I needs to be done using either JavaScript, CSS3 or HTML5!我有一个图像,我想显示 10 秒,然后隐藏 10 秒,然后显示 10 秒,隐藏 10 秒,然后永远重复执行该过程,但我不知道如何...我需要成为使用 JavaScript、CSS3 或 HTML5 完成!

HTML for the Image:图像的 HTML:

<div id="promo" class="gif" style="margin-top:33px;">
     <img width="320" height="267" src="assets/promo_xmas.png">
</div>

and I want this to just show for 10 seconds and hide for 10 seconds我希望它只显示 10 秒并隐藏 10 秒

Who ever answers this question can you please put an x for the amount of Seconds?谁回答过这个问题,你能在秒的数量上加一个 x 吗? thanks!谢谢!

I'm surprised no one gave a CSS-only answer yet, so here is one.我很惊讶还没有人给出仅 CSS 的答案,所以这里是一个。

img {
  opacity: 0;
  animation: disappear 10s linear 0s infinite alternate;
}

/* The animation code */
@keyframes disappear {
    from {opacity: 0;}
    to {opacity: 1;}
}

Taking your question quite literal, I think this is what you want.从字面上看你的问题,我认为这就是你想要的。 Image shows for 10s hides for 10s, then shows for 10s and hides for 7s, repeat.图像显示 10 秒隐藏 10 秒,然后显示 10 秒并隐藏 7 秒,重复。

function promoFade() {
 $('#promo').delay(x).fadeOut(150).delay(x).fadeIn(150).delay(x).fadeOut(150).delay(x).fadeIn(150);
};
setInterval("promoFade()", 0);

Added setInterval in case you need to wait for the page to load.添加了setInterval以防您需要等待页面加载。 There are better ways and personally, I would lose the 7s and loop with CSS keyframes as mentioned in other answers, yet if I was chucking something together that was very simple, needing that 7s, this is what I would start with.有更好的方法,就我个人而言,我会丢失 7s 并使用其他答案中提到的 CSS keyframes循环,但是如果我将一些非常简单的东西放在一起,需要 7s,这就是我的开始。

 function promoFade() { $('#promo').delay(10000).fadeOut(150).delay(10000).fadeIn(150).delay(10000).fadeOut(150).delay(7000).fadeIn(150); }; setInterval("promoFade()", 0);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="promo" class="gif" style="margin-top:33px;"> <img width="320" height="267" src="http://placehold.it/320x267/green/ffffff/&text=image"> </div>

<div id="promo" class="gif" style="margin-top:33px;">
     <img width="320" height="267" src="assets/promo_xmas.png">
</div>

<script>
   window.setInterval(function () {
    let gif = document.querySelector('.gif');
    if(gif.classList.contains('hide')) {
      gif.classList.remove('hide');
    } else {
      gif.classList.add('hide');
    }
   }, 10000); // or 7000 or whatever ;)

</script>

<style>
  .hide img {
    display: none;
  }
</style>

As prime suggests, it's better to add a fiddle https://jsfiddle.net/xwd1jcfg/正如主要建议的那样,最好添加一个小提琴https://jsfiddle.net/xwd1jcfg/

The first question is whether you need the image to become invisible with layout remaining unchanged or the entire block to be removed and inserted again.第一个问题是您是否需要在布局保持不变的情况下使图像不可见,还是需要删除并再次插入整个块。 I'm guessing its the former.我猜是前者。 Either way i prefer javascript.无论哪种方式,我都更喜欢 javascript。 Use javascript setTimer to toggle opacity between 1 and 0, or set display to block or none.使用 javascript setTimer 在 1 和 0 之间切换不透明度,或将显示设置为阻止或无。

$(document).ready(function() { $("#div_id_onclick").click(function() { setTimeout(function(){ $("#div_id_img").toggleClass("transparent"); }, X000); });});

Transparent class should be in css, with opacity set to 0.透明类应该在 css 中,不透明度设置为 0。

I'm typing from phone, so please expect syntax mistakes and correct accordingly.我正在通过电话打字,因此请注意语法错误并进行相应更正。 Also set X to any integer value, --edit--还将 X 设置为任何整数值,--edit--

Just reminded myself that you need repeat event and not timeout.只是提醒自己,您需要重复事件而不是超时。 Use setInterval function.使用 setInterval 函数。

setInterval(function(){$('.div_id_img').toggleClass('div_id_img_invisible')}, X000);

Div_id_img_invisible should override display or opacity property of div_id_img. Div_id_img_invisible 应该覆盖 div_id_img 的 display 或 opacity 属性。

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

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