简体   繁体   English

Javascript 干扰 CSS 转换

[英]Javascript interfering with CSS Transition

I've recently started work on a basic landing-page website.我最近开始在一个基本的登陆页面网站上工作。 Part of the page is a basic image slider with both the auto-nav and the manual nav being powered by JS.页面的一部分是一个基本的图像滑块,自动导航和手动导航都由 JS 提供支持。 I got everything working but for one thing: I just can't figure out how to get the smooth transition between images - as seen in this example - to work.我让一切正常,但有一点:我不知道如何让图像之间的平滑过渡 -如本例所示- 工作。 I figured out the problem after reading some related questions on Stackoverflow: To make the Slideshow work I'm using slides[i].style.display = "none";我在阅读 Stackoverflow 上的一些相关问题后发现了这个问题:为了使幻灯片工作,我使用了slides[i].style.display = "none"; and slides[slideIndex-1].style.display = "block";slides[slideIndex-1].style.display = "block"; to update the css code handling the 'display' element - this over rights the 'transition: 2s' css code one would need for the simple slide animation as seen in the video.更新处理 'display' 元素的 css 代码 - 这超过了视频中看到的简单幻灯片动画所需的 'transition: 2s' css 代码。 As I'm terribly new to Web development I could not wrap my head around possible solutions posted here on Stackoverflow and would really appreciate anyone that could help me out with an answer to my specific problem or an alternative way to solve it.由于我对 Web 开发非常陌生,因此我无法围绕 Stackoverflow 上发布的可能解决方案进行思考,并且非常感谢任何可以帮助我解决我的特定问题或解决该问题的替代方法的人。

Cheers, Jerome干杯,杰罗姆

 var slideIndex = 1; showSlides(slideIndex); // Next/previous controls function plusSlides(n) { showSlides(slideIndex += n); } // Thumbnail image controls function currentSlide(n) { showSlides(slideIndex = n); } function showSlides(n) { var i; var slides = document.getElementsByClassName("item"); var dots = document.getElementsByClassName("dot"); if (n > slides.length) { slideIndex = 1 } if (n < 1) { slideIndex = slides.length } for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; } for (i = 0; i < dots.length; i++) { dots[i].className = dots[i].className.replace(" active", ""); } slides[slideIndex - 1].style.display = "block"; dots[slideIndex - 1].className += " active"; } //automatic var slideIndexAuto = 0; showSlidesAuto(); function showSlidesAuto() { var i; var slides = document.getElementsByClassName("item"); var dots = document.getElementsByClassName("dot"); for (i = 0; i < slides.length; i++) { slides[i].style.display = "none"; dots[i].className = dots[i].className.replace(" active", ""); } slideIndexAuto++; if (slideIndexAuto > slides.length) { slideIndexAuto = 1 } slides[slideIndexAuto - 1].style.display = "block"; dots[slideIndexAuto - 1].className += " active"; setTimeout(showSlidesAuto, 5000); }
 .slider { width: 65%; max-width: 940px; height: 500px; border-radius: 0.25rem; position: relative; overflow: hidden; } .slider .left-slide, .slider .right-slide { position: absolute; height: 40px; width: 40px; background-color: #444444; border-radius: 50%; color: #ffffff; font-size: 20px; top: 50%; cursor: pointer; margin-top: -20px; text-align: center; line-height: 40px; } .slider .left-slide:hover, .slider .right-slide:hover { box-shadow: 0px 0px 10px black; background-color: #29a8e2; } .slider .left-slide { left: 30px; } .slider .right-slide { right: 30px; } .slider .slider-items .item img { width: 100%; height: 500px; display: block; } .slider .slider-items .item { position: relative; transition: 4s; } .slider .slider-items .item .caption { position: absolute; width: 100%; height: 100%; bottom: 0px; left: 0px; background-color: rgba(0, 0, 0, .5); }
 <div class="slider"> <div class="slider-items"> <div class="item fade"> <img src="/images/cs-slider-high.jpg" /> <div class="caption"> <p class="caption-text">COMING</p> <p class="caption-text">OKTOBER 10th</p> </div> </div> <div class="item fade"> <img src="/images/building-slider.jpg" /> <div class="caption"> <p class="caption-text-2">Blackstoneroad 109</br> </p> </div> </div> <div class="item fade"> <img src="/images/kontact-slider.jpg" /> <div class="caption"> <p class="caption-text-3">Coffee<br>Drinks<br>Food<br>& More</p> </div> </div> <div class="item fade"> <img src="/images/seminar-slider.jpg" /> <div class="caption"> <p class="caption-text-3">Seminar Rooms<br>Inspiration<br>Shopping<br>& More</p> </div> </div> </div> <!-- Next and previous buttons --> <a class="prev" onclick="plusSlides(-1)">&#10094;</a> <a class="next" onclick="plusSlides(1)">&#10095;</a> </div> <!-- The dots/circles --> <div class="align-text-center"> <span class="dot" onclick="currentSlide(1)"></span> <span class="dot" onclick="currentSlide(2)"></span> <span class="dot" onclick="currentSlide(3)"></span> <span class="dot" onclick="currentSlide(3)"></span> </div>

How is this: http://jsfiddle.net/lharby/qox05y96/这是怎么回事: http : //jsfiddle.net/lharby/qox05y96/

For simplification I have stripped out the dot animation (although that code is closer to the effect you want).为简化起见,我去掉了点动画(尽管该代码更接近您想要的效果)。

Here is the simplified JS:这是简化的JS:

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("item");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {
    slideIndex = 1
  }
  if (n < 1) {
    slideIndex = slides.length
  }
  for (i = 0; i < slides.length; i++) {
    slides[i].classList.remove('active'); // this is updated
  }
  slides[slideIndex - 1].className += " active";
}

//automatic
var slideIndexAuto = 0;
showSlidesAuto();

function showSlidesAuto() {
  var i;
  var slides = document.getElementsByClassName("item");
  var dots = document.getElementsByClassName("dot");
  for (i = 0; i < slides.length; i++) {
    slides[i].classList.remove('active'); // this is updated
    
  }
  slideIndexAuto++;
  if (slideIndexAuto > slides.length) {
    slideIndexAuto = 1
  }
  slides[slideIndexAuto - 1].classList.add('active'); // this is updated
  setTimeout(showSlidesAuto, 4000);
}

This means we also have to change the css.这意味着我们还必须更改 css。 display none/block is harder to animate with css transitions . display none/block 更难用 css transitions 制作动画 So I have used opacity: 0/1 and visibility: hidden/visible .所以我使用了opacity: 0/1visibility: hidden/visible However one other trade off is that that the item elements cannot be positioned relatively this would stack them on top of one another (or side by side) usually for an animated slideshow you would use position absolute for each slide, but I realise that causes you another issue.然而,另一个折衷是项目元素不能相对定位,这通常会将它们堆叠在一起(或并排),通常对于动画幻灯片,您会为每张幻灯片使用绝对位置,但我意识到这会导致您另一个问题。

CSS: CSS:

.slider .slider-items .item {
    visibility: hidden;
    opacity: 0;
    transition: 4s;
}

.slider .slider-items .item.active {
    visibility: visible;
    opacity: 1;
    transition: 4s;
}

At least now all of the css is handled within the css and the js purely adds or removes a class name which I feel makes it a bit easier to update.至少现在所有的 css 都在 css 中处理,而 js 纯粹添加或删除了一个类名,我觉得它更容易更新。

There are priorities as far as what CSS deems should happen. CSS 认为应该发生的事情有优先级。 Take a look at this and let me know if it changes anything for you.看看这个,让我知道它是否对你有任何改变。 If not I'll try and run the code and fix your errors.如果没有,我会尝试运行代码并修复您的错误。 I want to let you know in advance, I am a pure JS developer.我想提前告诉你,我是一个纯JS开发者。 When I need CSS or HTML I create it in JS.当我需要 CSS 或 HTML 时,我会在 JS 中创建它。 I want you to try first, thus it will make you a better developer.我希望你先尝试,这样你才能成为更好的开发者。

https://www.thebookdesigner.com/2017/02/styling-priorities-css-for-ebooks-3/ https://www.thebookdesigner.com/2017/02/styling-priorities-css-for-ebooks-3/

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

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