简体   繁体   English

在图像滑块动画中获得平滑过渡

[英]Obtaining a smooth transition in an image slider animation

THE AIM目的

Obtain a smoother transition between images.在图像之间获得更平滑的过渡。 At the moment I have a white background between images for a few milliseconds.目前我在图像之间有几毫秒的白色背景。

THE PROBLEM问题

Apparently I cannot properly replicate this part of my code as I don't think you can use links in background-image: url(...) .显然我无法正确复制这部分代码,因为我认为您不能在background-image: url(...) 中使用链接。 In my actual code, I'm using relative paths.在我的实际代码中,我使用的是相对路径。

The problem is the white background between picture transition when they first load and are not save in the cache.问题是第一次加载时图片过渡之间的白色背景并且没有保存在缓存中。 When starting again the slider, the transition is smoother, ie the white background is not shown.再次启动滑块时,过渡更平滑,即不显示白色背景。

SUMMARY概括

I would like to obtain a smoother transition between pictures when they first load and are not save in the cache.我想在图片首次加载并且未保存在缓存中时获得更平滑的过渡。 How could this be done?这怎么可能? (jQuery is also welcomed) (也欢迎使用 jQuery)

 h1 { display: flex; justify-content: center; margin: 20vh 0; background-color: lightblue; padding: 50px; } @keyframes slide { 0% { background-image: url("https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687"); } 25% { background-image: url("https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687"); } 25.01% { background-image: url("https://image.shutterstock.com/image-photo/micro-peacock-feather-hd-imagebest-260nw-1127238584.jpg"); } 50% { background-image: url("https://image.shutterstock.com/image-photo/micro-peacock-feather-hd-imagebest-260nw-1127238584.jpg"); } 50.1% { background-image: url("https://images.pexels.com/photos/326055/pexels-photo-326055.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"); } 75% { background-image: url("https://images.pexels.com/photos/326055/pexels-photo-326055.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"); } 75.1% { background-image: url("https://media3.s-nbcnews.com/j/newscms/2019_41/3047866/191010-japan-stalker-mc-1121_06b4c20bbf96a51dc8663f334404a899.fit-760w.JPG"); } 100% { background-image: url("https://media3.s-nbcnews.com/j/newscms/2019_41/3047866/191010-japan-stalker-mc-1121_06b4c20bbf96a51dc8663f334404a899.fit-760w.JPG"); } } .slider { background-image: url("https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687"); background-size: cover; background-repeat: no-repeat; background-position: center; animation: slide 30s infinite; } .load { animation: slide 2s; }
 <div class="slider"></div> <div class="load"></div> <h1>SOME CONTENT</h1>

UPDATE更新

The images are now shown as <div class="slider"></div> was missing before.图像现在显示为<div class="slider"></div>之前丢失。 This was pointed out in the first answer below.这是在下面的第一个答案中指出的。 However, the white background between images is still there.但是,图像之间的白色背景仍然存在。

Your slider and load divs didn't show because there was nothing in them to begin with and no default height specified in the css.您的滑块和加载 div 没有显示,因为它们中没有任何内容可以开始,并且在 css 中没有指定默认高度。 I have given the slider a height of 30vh and a width of 100% (I went with that one for demo purposes).我给滑块的高度为 30vh,宽度为 100%(为了演示目的,我使用了那个)。 I reduced the margin/padding around the h1 (there's a lot!) just so you could see the images better in the snippet.我减少了 h1 周围的边距/填充(有很多!),以便您可以更好地在代码段中看到图像。 Still might be viewed better full screen.仍然可以更好地全屏观看。

I added an animation timing (ease-in-out) to the css to make the start and end of the animation slower to make the transition seem a little easier on the eye.我在 css 中添加了一个动画计时(渐进渐出),使动画的开始和结束速度变慢,使过渡看起来更容易一些。 The default is ease which makes it fast in the middle;默认是简单的,这使得它在中间很快; by changing the transition speed, your animation may appear a little smoother.通过更改过渡速度,您的动画可能会显得更流畅一些。

Hope this helps希望这可以帮助

 h1 { display: flex; justify-content: center; margin: 2vh 0; background-color: lightblue; padding: 15px; } @keyframes slide { 0% { background-image: url("https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687"); } 25% { background-image: url("https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687"); } 25.01% { background-image: url("https://image.shutterstock.com/image-photo/micro-peacock-feather-hd-imagebest-260nw-1127238584.jpg"); } 50% { background-image: url("https://image.shutterstock.com/image-photo/micro-peacock-feather-hd-imagebest-260nw-1127238584.jpg"); } 50.1% { background-image: url("https://images.pexels.com/photos/326055/pexels-photo-326055.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"); } 75% { background-image: url("https://images.pexels.com/photos/326055/pexels-photo-326055.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"); } 75.1% { background-image: url("https://media3.s-nbcnews.com/j/newscms/2019_41/3047866/191010-japan-stalker-mc-1121_06b4c20bbf96a51dc8663f334404a899.fit-760w.JPG"); } 100% { background-image: url("https://media3.s-nbcnews.com/j/newscms/2019_41/3047866/191010-japan-stalker-mc-1121_06b4c20bbf96a51dc8663f334404a899.fit-760w.JPG"); } } .slider { display:inline-block; background-image: url("https://s.ftcdn.net/v2013/pics/all/curated/RKyaEDwp8J7JKeZWQPuOVWvkUjGQfpCx_cover_580.jpg?r=1a0fc22192d0c808b8bb2b9bcfbf4a45b1793687"); background-size: cover; background-repeat: no-repeat; background-position: center; animation: slide 5s ease-in-out infinite; height:30vh; width:100%; } .load { animation: slide 5s infinite; }
 <h1>SOME CONTENT</h1> <div class="slider"></div>

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

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