简体   繁体   English

为什么 jQuery Fading Effect 不能正常工作?

[英]Why does jQuery Fading Effect not work properly?

I have a slideshow on my website and I want the transitions of the slides to have a fading animation.我的网站上有一个幻灯片,我希望幻灯片的过渡具有淡化动画。 For that I used jQuery with the fadeIn and fadeOut commands.为此,我将 jQuery 与 fadeIn 和 fadeOut 命令一起使用。 I actually have two questions:我其实有两个问题:

  1. The animation does work, however not properly;动画确实有效,但不正确; when I click to the next image, it has the normal, hard change and then the new image is fading out and in again instead of one fading out and the other one fading in. Why is that and how can that be corrected?当我单击下一张图像时,它具有正常的硬变化,然后新图像淡出并再次淡入,而不是一个淡出另一个淡入。为什么会这样,如何纠正?

  2. The images are only fading in and out when i click the arrows, not when the slideshow is automatically going through the images with my interval of 5 seconds.只有当我单击箭头时,图像才会淡入和淡出,而不是当幻灯片以 5 秒的间隔自动浏览图像时。 I tried to integrate the effect with a change-event (when the image is changing the animation should appear) but that doesn't work.我尝试将效果与更改事件集成(当图像更改时应该出现动画),但这不起作用。 Do I have to use another eventhandler oder is there a whole other way?我是否必须使用另一个事件处理程序,还有其他方法吗?

I couldn't find anything helpful on Google to these questions so I'd be very grateful if someone could help me with this, since I'm also very new to JavaScript and need it for a university project.我在谷歌上找不到任何对这些问题有帮助的东西,所以如果有人能帮我解决这个问题,我将非常感激,因为我对 JavaScript 也很陌生,并且需要它来完成大学项目。 Thank you!谢谢! :) :)

 var DiashowBilder = new Array("Bilder/1.jpeg", "Bilder/2.jpeg", "Bilder/3.jpeg", "Bilder/4.jpeg", "Bilder/5.jpeg", "Bilder/6.jpeg", "Bilder/7.jpeg", "Bilder/8.jpeg", "Bilder/9.jpeg", "Bilder/10.jpeg"); var index = 0; jQuery.noConflict(); jQuery(document).ready(function() { jQuery("#animation").click(function() { jQuery("#Vorschaubild").fadeOut(400, function() { diashow(); jQuery("#Vorschaubild").fadeIn(400); }); }) jQuery("#animation").change(function() { jQuery("#Vorschaubild").fadeOut(400, function() { diashow(); jQuery("#Vorschaubild").fadeIn(400); }); }) jQuery("#next").click(function() { jQuery("#Vorschaubild").fadeOut(400, function() { diashow(); jQuery("#Vorschaubild").fadeIn(400); }); }) jQuery("#previous").click(function() { jQuery("#Vorschaubild").fadeOut(400, function() { diashow(); jQuery("#Vorschaubild").fadeIn(400); }); }) }); function nextIMG(n) { diashow(index += n); document.getElementsByClassName("dot")[index].classList.add("active"); document.getElementsByClassName("dot")[index -n].classList.remove("active"); if (index == DiashowBilder.length) { index = 0; document.getElementsByClassName("dot")[10].classList.remove("active"); } if (index < 0) { index = DiashowBilder.length -1; } } function currentSlide(n) { diashow(index = n); dot[index].classList.add("active") } function diashow() { document.getElementById("Vorschaubild").src = DiashowBilder[index]; if (index == DiashowBilder.length) { index = 0; } if (index < 0) { index = DiashowBilder.length -1; } } diashow(); function automatischWeiterschalten() { nextIMG(1); } setInterval(automatischWeiterschalten, 5000);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <a class="prev" onclick="nextIMG(-1)" id="previous">&#10094;</a> <div id="animation"> <img id="Vorschaubild" /> <br/><br/> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)" id="dot1"></span> <span class="dot" onclick="currentSlide(2)" id="dot2"></span> <span class="dot" onclick="currentSlide(3)" id="dot3"></span> <span class="dot" onclick="currentSlide(4)" id="dot4"></span> <span class="dot" onclick="currentSlide(5)" id="dot5"></span> <span class="dot" onclick="currentSlide(6)" id="dot6"></span> <span class="dot" onclick="currentSlide(7)" id="dot7"></span> <span class="dot" onclick="currentSlide(8)" id="dot8"></span> <span class="dot" onclick="currentSlide(9)" id="dot9"></span> <span class="dot" onclick="currentSlide(10)" id="dot10"></span> </div> </div> <a class="next" onclick="nextIMG(1)" id="next">&#10095;</a>

The main reason why your code won't work as desired is the fact, that the animation is completely unrelated to the update of the src property.您的代码无法按预期工作的主要原因是动画与 src 属性的更新完全无关。 The update must happen within the fade function, otherwise this will never be synchronized and work properly.更新必须在淡入淡出功能内进行,否则将永远无法同步并正常工作。

I updated your code a little bit and moved the animation to the correct location.我稍微更新了您的代码并将动画移动到正确的位置。 (And using images which are actually displayed) The code is still not 100% working, but it will allow you to continue your development. (并使用实际显示的图像)代码仍然不是 100% 工作,但它可以让你继续你的开发。

Things to keep in mind:要记住的事情:

  • What happens if you go back when you are at index 0如果你在索引 0 时返回会发生什么
  • What happens if the interval image triggers during the 800ms transition you manually started (Maybe consider stopping the interval in this case and restarting it after the transition finished)如果在您手动启动的 800 毫秒转换期间触发间隔图像会发生什么情况(在这种情况下可以考虑停止间隔并在转换完成后重新启动它)
  • Why do you split your logic into nextIMG and diashow.你为什么把你的逻辑拆分成 nextIMG 和 diashow。 Wouldn't it be better to remove the "active" after the fade out and then add the new "active" after the fade in?淡出后删除"active" ,然后在淡入后添加新的"active"不是更好吗?

 var DiashowBilder = new Array("https://images.freeimages.com/images/large-previews/e4e/circulate-abstract-1562332.jpg", "https://images.freeimages.com/images/large-previews/4ea/abstract-building-1226559.jpg", "https://images.freeimages.com/images/large-previews/e4e/circulate-abstract-1562332.jpg", "https://images.freeimages.com/images/large-previews/5ae/summer-holiday-1188633.jpg"); var index = 0; jQuery.noConflict(); jQuery(document).ready(function() { jQuery("#animation").click(function() { nextIMG(1); }) jQuery("#next").click(function() { nextIMG(-1); }) jQuery("#previous").click(function() { nextIMG(1); }) }); function nextIMG(n) { diashow(index += n); document.getElementsByClassName("dot")[index].classList.add("active"); document.getElementsByClassName("dot")[index -n].classList.remove("active"); if (index == DiashowBilder.length) { index = 0; document.getElementsByClassName("dot")[10].classList.remove("active"); } if (index < 0) { index = DiashowBilder.length -1; } } function currentSlide(n) { diashow(index = n); dot[index].classList.add("active") } function diashow() { jQuery("#Vorschaubild").fadeOut(400, function() { document.getElementById("Vorschaubild").src = DiashowBilder[index]; if (index == DiashowBilder.length) { index = 0; } if (index < 0) { index = DiashowBilder.length -1; } jQuery("#Vorschaubild").fadeIn(400); }); } diashow(); function automatischWeiterschalten() { nextIMG(1); } setInterval(automatischWeiterschalten, 5000);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script> <a class="prev" onclick="nextIMG(-1)" id="previous">&#10094;</a> <div id="animation"> <img id="Vorschaubild" /> <br/><br/> <div style="text-align:center"> <span class="dot" onclick="currentSlide(1)" id="dot1"></span> <span class="dot" onclick="currentSlide(2)" id="dot2"></span> <span class="dot" onclick="currentSlide(3)" id="dot3"></span> <span class="dot" onclick="currentSlide(4)" id="dot4"></span> <span class="dot" onclick="currentSlide(5)" id="dot5"></span> <span class="dot" onclick="currentSlide(6)" id="dot6"></span> <span class="dot" onclick="currentSlide(7)" id="dot7"></span> <span class="dot" onclick="currentSlide(8)" id="dot8"></span> <span class="dot" onclick="currentSlide(9)" id="dot9"></span> <span class="dot" onclick="currentSlide(10)" id="dot10"></span> </div> </div> <a class="next" onclick="nextIMG(1)" id="next">&#10095;</a>

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

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