简体   繁体   English

循环淡入淡出的Divs

[英]Divs that fade in and out in a loop

I've looked at all the other questions similar or identical to mine but I still can't find what I'm looking for. 我看过所有与我相似或相同的其他问题,但仍然找不到我想要的东西。 I'm still new to jquery but all I want is my three divs to be on loop and fade in and out when they change. 我对jQuery还是很陌生,但我只想让我的三个div循环并在它们改变时淡入和淡出。 What I'm seeing is people using fast speeds, the first div also fades in and then the rest will follow but I want the first div to just be present when the page loads and then switch after some time. 我所看到的是人们使用速度快,第一个div也逐渐消失,然后其余的都会跟随,但我希望第一个div仅在页面加载时出现,然后在一段时间后切换。 Here's my html 这是我的HTML

<div id="Reviews">
    <div class="Review1">
       <p>Customer Review 1</p>
       <span class="CustomerName">- Bob</span>
     </div>
     <div class="Review2">
       <p>Customer Review 2</p>
       <span class="CustomerName">- Mary</span>
     </div>
        <div class="Review3">
        <p>Customer Review 3</p>
        <span class="CustomerName">- Jess</span>
     </div>
</div>

Now I have separate classes for them but I'd rather set up the query to fade in and out by calling using "#Reviews div" something like that, but changing the class names of the divs is fine if needed to make my fade in/out to work. 现在,我为它们提供了单独的类,但我希望通过使用类似“ #Reviews div”的调用来设置要淡入和淡出的查询,但是如果需要使我的淡入淡出,更改div的类名也可以/上班。 I'd prefer to just add reviews if needed and it'll just adjust by the class name or just by being a div. 我更愿意在需要时添加评论,它会根据课程名称或作为div进行调整。 I'm also seeing coding that'll fade my divs but the following div will show up below the current one and then the current one will disappear and the following moves up, where as I want it to stay vertically aligned top just fade in and out while on a loop so it always goes back to the first and then goes again. 我还看到了将使div消失的编码,但是下面的div将显示在当前div的下面,然后当前的div将消失,随后的div向上移动,在这里我希望它保持垂直对齐,顶部只是淡入并在循环中时,它总是返回到第一个,然后再返回。 I thought this would be easier to do but apparently not. 我认为这样做会更容易,但显然并非如此。 I've tried using coding from what I saw from other questions but I just can't find what I'm looking for specifically. 我尝试从其他问题中看到的内容使用编码,但我只是找不到我想要的东西。

If you want to do this in pure CSS, You could do it, using @keyframes 如果要在纯CSS中执行此操作,可以使用@keyframes进行

This solution is very specific to the use case. 该解决方案非常适合用例。 Having 3 divs and 3 seconds of visibility. 具有3 div和3秒的可见度。

You could have the time of X seconds for all the divs to be displayed once in a cycle. 您可能需要X秒的时间才能使所有div在一个周期中显示一次。

Give each div its share of X and repeat the animations. 给每个div其X的份额并重复动画。

For the sake of simplicity, I defined X as 6 seconds, so each of your review divs get 3 seconds of time. 为了简单起见,我将X定义为6秒,因此您的每个评论div都有3秒的时间。

If you are looking for some inspiration, to implement something on your own, hopefully this could be your starting point. 如果您正在寻找灵感,自己实现一些东西,希望这可以成为您的起点。

 .reviews { position: relative; } .review { position: absolute; transform-style: preserve-3d; animation-duration: 6s; animation-iteration-count: infinite; animation-timing-function: linear; } .review-1 { animation-name: reviewOne; color: red; } .review-2 { animation-name: reviewTwo; color: blue; } .review-3 { animation-name: reviewThree; color: green; } @keyframes reviewOne { 0% { opacity: 1; } 32% { opacity: 1; } 33% { opacity: 0; } 100% { opacity: 0 } } @keyframes reviewTwo { 0% { opacity: 0; } 32% { opacity: 0; } 33% { opacity: 0.5; } 34% { opacity: 1; } 65% { opacity: 1; } 66% { opacity: 0; } 100% { opacity: 0 } } @keyframes reviewThree { 0% { opacity: 0; } 65% { opacity: 0; } 66% { opacity: 0.5; } 67% { opacity: 1; } 100% { opacity: 1; } } 
 <div id="reviews"> <div class="review review-1"> <p>Customer Review 1</p> <span class="CustomerName">- Bob</span> </div> <div class="review review-2"> <p>Customer Review 2</p> <span class="CustomerName">- Mary</span> </div> <div class="review review-3"> <p>Customer Review 3</p> <span class="CustomerName">- Jess</span> </div> </div> 

If you want to fadein and fadeout all the divs in a loop .you can do as the following 如果要在循环中淡入和淡出所有div,可以执行以下操作

check this snippet 检查此片段

 $(document).ready(function() { setInterval(function() { fadeinOut(); }, 1000) }); var count = -1; function fadeinOut() { var reviews = $('#Reviews div'); var reviewLength = reviews.length - 1; count < reviewLength ? count++ : count = 0; reviews.fadeOut().delay(2000).eq(count).fadeIn().addClass('top'); } 
 #Reviews div {} #Reviews { position: relative; } #Reviews div:not(.Review1) { display: none; } .top { display: block; z-index: 1; position: absolute; top: 0; left: 0; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="Reviews"> <div class="Review1"> <p>Customer Review 1</p> <span class="CustomerName">- Bob</span> </div> <div class="Review2"> <p>Customer Review 2</p> <span class="CustomerName">- Mary</span> </div> <div class="Review3"> <p>Customer Review 3</p> <span class="CustomerName">- Jess</span> </div> </div> 

Hope it helps 希望能帮助到你

Here's a quick implementation using jQuery with the "slick" carousel plugin . 这是使用带有“ slick”轮播插件的 jQuery的快速实现。 Run the demo below to see it in action. 运行下面的演示以查看实际操作。

 $(document).ready(function() { $('#Reviews').slick({ fade: true, // change transition from slide to fade autoplay: true, // autoplay the slideshow so no interaction needed arrows: false // remove the default Prev/Next arrrows }); }); 
 <link rel="stylesheet" type="text/css" href="http://kenwheeler.github.io/slick/slick/slick.css" /> <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="http://kenwheeler.github.io/slick/slick/slick.js"></script> <div id="Reviews"> <div class="Review1"> <p>Customer Review 1</p> <span class="CustomerName">- Bob</span> </div> <div class="Review2"> <p>Customer Review 2</p> <span class="CustomerName">- Mary</span> </div> <div class="Review3"> <p>Customer Review 3</p> <span class="CustomerName">- Jess</span> </div> </div> 

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

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