简体   繁体   English

带渐变的幻灯片 animation

[英]Slideshow with fade animation

How can I adjust this code so that it doesn't fade out straight away?如何调整此代码以使其不会立即消失? In other words, make the image fade in, sit for a few seconds and then fade back out?换句话说,让图像淡入,静置几秒钟然后淡出?

This is my code right now:这是我现在的代码:

    <div id="slideshow-example" data-component="slideshow">
        <div role="list">
            <div class="slide fade">
                <img src="https://images.unsplash.com/photo-1486312338219-ce68d2c6f44d?w=752&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt="">
            </div>
            <div class="slide fade">
                <img src="https://images.unsplash.com/photo-1488590528505-98d2b5aba04b?w=750&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt="">
            </div>
            <div class="slide fade">
                <img src="https://images.unsplash.com/photo-1498753427761-548428edfa67?w=889&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt="">
            </div>
        </div>
    </div>

[data-component="slideshow"] .slide {
    display: none;
  text-align: center;
}

[data-component="slideshow"] .slide.active {
    display: block;
}

.fade
{
    -webkit-animation: fadeinout 2s linear forwards;
    animation: fadeinout 2s linear forwards;
}

@-webkit-keyframes fadeinout {
    0%,100% { opacity: 0; }
    50% { opacity: 1; }
  }
  
@keyframes fadeinout {
  0%,100% { opacity: 0; }
  50% { opacity: 1; }
}

var slideshows = document.querySelectorAll('[data-component="slideshow"]');
slideshows.forEach(initSlideShow);

function initSlideShow(slideshow) {

    var slides = document.querySelectorAll(`#${slideshow.id} [role="list"] .slide`);

    var index = 0, time = 5000;
    slides[index].classList.add('active');

    setInterval( () => {
        slides[index].classList.remove('active');
        
        index++;
        if (index === slides.length) index = 0;

        slides[index].classList.add('active');

    }, time);
}

Please ignore this part, StackOverflow won't let me post because it's "mostly code" even though there's not much more of an explanation I can give towards my issue.请忽略这部分,StackOverflow 不会让我发布,因为它“主要是代码”,即使我无法对我的问题给出更多解释。 If you are confused on my issue however, just ask me and I'll try and explain it more in-depth.但是,如果您对我的问题感到困惑,请问我,我会尝试更深入地解释它。

You can change the percentage of which the element is shown something like the following:您可以更改元素显示的百分比,如下所示:

 var slideshows = document.querySelectorAll('[data-component="slideshow"]'); slideshows.forEach(initSlideShow); function initSlideShow(slideshow) { var slides = document.querySelectorAll(`#${slideshow.id} [role="list"].slide`); var index = 0, time = 5000; slides[index].classList.add('active'); setInterval( () => { slides[index].classList.remove('active'); index++; if (index === slides.length) index = 0; slides[index].classList.add('active'); }, time); }
 [data-component="slideshow"].slide { display: none; text-align: center; } [data-component="slideshow"].slide.active { display: block; }.fade { -webkit-animation: fadeinout 3s linear forwards; animation: fadeinout 2s linear forwards; } @-webkit-keyframes fadeinout { 0%,100% { opacity: 0; } 20%, 80% { opacity: 1; } } @keyframes fadeinout { 0%,100% { opacity: 0; } 20%,50% { opacity: 1; } }
 <div id="slideshow-example" data-component="slideshow"> <div role="list"> <div class="slide fade"> <img src="https://images.unsplash.com/photo-1486312338219-ce68d2c6f44d?w=752&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt=""> </div> <div class="slide fade"> <img src="https://images.unsplash.com/photo-1488590528505-98d2b5aba04b?w=750&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt=""> </div> <div class="slide fade"> <img src="https://images.unsplash.com/photo-1498753427761-548428edfa67?w=889&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt=""> </div> </div> </div>

Here is a brief re-write of your project using async await syntax and some different logic.这是使用 async await 语法和一些不同的逻辑对您的项目进行的简要重写。

First, I use two helper functions.首先,我使用了两个辅助函数。 One is a signature $ function similar to jQuery, but just to shorted grabbing things from the document.一个是签名$ function 类似于 jQuery,但只是为了从文件中抓取东西做空。 I also allow another element to be passed as the parent we are selecting things from because I feel it makes the code more readable.我还允许将另一个元素作为我们从中选择内容的父元素传递,因为我觉得它使代码更具可读性。 The other is a wait function similar to Python's sleep .另一个是类似于 Python 的 sleepwait function 。 There are better resources for understanding promises, but in short, any function that is called async function will allow the await keyword to "wait" for a promise to resolve.有更好的资源来理解承诺,但简而言之,任何称为async function的 function 都将允许await关键字“等待” promise

Next, I use an anonymous arrow function passed which calls the function instead of just passing the name of the function.接下来,我使用一个匿名箭头 function 传递,它调用 function 而不是只传递 function 的名称。 The reason I do this is to explicitly state which arguments from the forEach method I am passing into my function.我这样做的原因是明确 state 哪个 arguments 从forEach方法传递到我的 function。 If we do not do this, the second argument slides would be passed an index which would not be intended.如果我们不这样做,第二个参数slides将被传递一个不想要的索引。

Inside the function initSlideShow we check to see if slides are defined, and if not we define it.在 function initSlideShow内部,我们检查是否定义了slides ,如果没有定义它。 This way we dont have to use query selector to grab things each iteration, and instead can reuse the previous values on recursive calls.这样我们就不必在每次迭代时使用查询选择器来抓取东西,而是可以在递归调用中重用以前的值。

we set the time we will use later我们设置稍后使用的时间

we start looping through each slide, and first add active which will change the display to block.我们开始循环播放每张幻灯片,并首先添加将显示更改为块的active We need to allow the dom to render the change before fading in, so we wait 10 ms.我们需要让 dom 在淡入之前渲染变化,所以我们等待 10 毫秒。 then we remove the fade class which removes the opacity 0, and the transition css makes it fade in over 1s.然后我们移除fade class 移除不透明度 0,过渡 css 使其淡入超过 1 秒。

We wait the amount of time specified.我们等待指定的时间。

We add the fade class back to the element.我们将渐变 class 添加回元素。

We wait the animation time we remove active, which sets display to none.我们等待 animation 时间我们移除活动,这将显示设置为无。

After we loop through all the slides, we just call the function again passing the slides we already fetched to do it again.在我们遍历所有幻灯片之后,我们只需调用 function 再次传递我们已经获取的幻灯片以再次执行此操作。 A nice little recursive logic to loop the function forever.一个很好的小递归逻辑,可以永远循环 function。

If you have any questions regarding the code, please ask.如果您对代码有任何疑问,请询问。

 const $ = (str, dom = document) => [...dom.querySelectorAll(str)]; const wait = t => new Promise(r => setTimeout(r, t)); const slideshows = $('[data-component="slideshow"]'); slideshows.forEach(el => initSlideShow(el)); async function initSlideShow(container, slides) { if (slides == undefined) slides = $('[role="list"].slide', container); const time = 5000; for (const slide of slides) { slide.classList.add("active"); await wait(10); slide.classList.remove("fade"); await wait(time); slide.classList.add("fade"); await wait(1000); slide.classList.remove("active"); } initSlideShow(container, slides); }
 [data-component="slideshow"].slide { display: none; text-align: center; transition: opacity 1s; } [data-component="slideshow"].slide.active { display: block; }.fade { opacity: 0; }
 <div id="slideshow-example" data-component="slideshow"> <div role="list"> <div class="slide fade"> <img src="https://images.unsplash.com/photo-1486312338219-ce68d2c6f44d?w=752&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt=""> </div> <div class="slide fade"> <img src="https://images.unsplash.com/photo-1488590528505-98d2b5aba04b?w=750&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt=""> </div> <div class="slide fade"> <img src="https://images.unsplash.com/photo-1498753427761-548428edfa67?w=889&ixid=dW5zcGxhc2guY29tOzs7Ozs%3D" alt=""> </div> </div> </div>

There are many things I would do differently in an actual production app though.不过,在实际的生产应用程序中,我会做很多不同的事情。 Changing display none to block frequently like this will likely shift the layout of the page unpredictable.像这样频繁地将 display none 更改为 block 可能会使页面的布局发生不可预测的变化。 I would stick to changing opacity and transition mainly.我会坚持主要改变不透明度和过渡。 Maybe sliding the elements inside a container with hidden overflow when faded out.淡出时,可能会在隐藏溢出的容器内滑动元素。

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

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