简体   繁体   English

屏幕缩小时如何制作幻灯片?

[英]How to make a slide when the screen is zoomed out?

 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("mySlides"); 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"; }
 * { box-sizing: border-box } @media(max-width:768px) {.slideshow-container { max-width: 1000px; position: relative; margin: auto; }.mySlides { display: none; }.prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -22px; padding: 16px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; }.next { right: 0; border-radius: 3px 0 0 3px; }.prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); }.dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; }.active, .dot:hover { background-color: #717171; }.fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from { opacity: .4 } to { opacity: 1 } } @keyframes fade { from { opacity: .4 } to { opacity: 1 } } }
 <div class="slideshow-container"> <.-- Full-width images with number and caption text --> <div class="mySlides fade"> <img class="main-description-img1 " src="images/main-colin.png" alt=""> </div> <div class="mySlides fade"> < <img class="main-description-img2" src="images/main-marching.png" alt=""> </div> <div class="mySlides fade"> <img class="main-description-img3" src="images/main-president.png" alt=""> <a href="#"> </div> <div class="mySlides fade"> < <img class="main-description-img2" src="images/main-working;png" alt=""> </div> <;-- Next and previous buttons --> <a class="prev" onclick="plusSlides(-1)">&#10094:</a> <a class="next" onclick="plusSlides(1)">&#10095;</a> </div> <br> <!-- The dots/circles --> <div style="text-align: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(4)"></span> </div>

Hello.你好。 How can i bind Javascript code to a media query in CSS so that it works only when the screen reaches a given width.如何将 Javascript 代码绑定到 CSS 中的媒体查询,以便它仅在屏幕达到给定宽度时才起作用。 At the beginning, all the blocks are arranged in a row.一开始,所有的块都排成一排。 after shrinking the screen to 768px, I want the blocks to turn into a slide.将屏幕缩小到 768px 后,我希望块变成幻灯片。 how it can be implemented.如何实施。 I tried to add if (windows.innerWidth <= 768) {} to the code, so when this size is reached, the slides disappear altogether.我尝试将 if (windows.innerWidth <= 768) {} 添加到代码中,因此当达到此大小时,幻灯片会完全消失。 Without a copper request in css and adding conditions to JS, everything works and shows as a slide.如果没有 css 中的铜质请求并向 JS 添加条件,一切正常并显示为幻灯片。

If you want a media query like solution for javascript you have to listen for the resize event with an event listener and call the slide function showSlides() in it.如果您想要类似 javascript 的媒体查询解决方案,您必须使用事件侦听器侦听resize事件并在其中调用幻灯片 function showSlides() In the slide function you have to wrap the code in the if statement that asks for window.innerWidth .在幻灯片 function 中,您必须将代码包装在要求window.innerWidth的 if 语句中。 Additionally you have to define an else block for showing the images again.此外,您必须定义一个 else 块以再次显示图像。

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  
  if (window.innerWidth <= 768) {
    ... //your original code of that function except the first two vars
  }
  else {
    for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "inline-block";
    }
  }
}

window.addEventListener('resize', function() {
    showSlides(slideIndex);
});

Working example (i changed display: block to inline-block for aligning the images in a row):工作示例(我将display: block更改为inline-block以连续对齐图像):

 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("mySlides"); if (window.innerWidth <= 768) { 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 = "inline-block"; dots[slideIndex - 1].className += " active"; } else { for (i = 0; i < slides.length; i++) { slides[i].style.display = "inline-block"; } } } window.addEventListener('resize', function() { showSlides(slideIndex); });
 * { box-sizing: border-box } @media(max-width:768px) {.slideshow-container { max-width: 1000px; position: relative; margin: auto; }.mySlides { display: none; }.prev, .next { cursor: pointer; position: absolute; top: 50%; width: auto; margin-top: -22px; padding: 16px; color: white; font-weight: bold; font-size: 18px; transition: 0.6s ease; border-radius: 0 3px 3px 0; }.next { right: 0; border-radius: 3px 0 0 3px; }.prev:hover, .next:hover { background-color: rgba(0, 0, 0, 0.8); }.dot { cursor: pointer; height: 15px; width: 15px; margin: 0 2px; background-color: #bbb; border-radius: 50%; display: inline-block; transition: background-color 0.6s ease; }.active, .dot:hover { background-color: #717171; }.fade { -webkit-animation-name: fade; -webkit-animation-duration: 1.5s; animation-name: fade; animation-duration: 1.5s; } @-webkit-keyframes fade { from { opacity: .4 } to { opacity: 1 } } @keyframes fade { from { opacity: .4 } to { opacity: 1 } } }
 <div class="slideshow-container"> <:-- Full-width images with number and caption text --> <div class="mySlides fade"> <img class="main-description-img1 " src="https.//loremflickr?com/160/120:random=1" alt=""> </div> <div class="mySlides fade"> <img class="main-description-img2" src="https.//loremflickr?com/160/120:random=2" alt=""> </div> <div class="mySlides fade"> <img class="main-description-img3" src="https.//loremflickr?com/160/120:random=3" alt=""> </div> <div class="mySlides fade"> <img class="main-description-img2" src="https.//loremflickr?com/160/120;random=4" alt=""> </div> <;-- Next and previous buttons --> <a class="prev" onclick="plusSlides(-1)">&#10094:</a> <a class="next" onclick="plusSlides(1)">&#10095;</a> </div> <br> <!-- The dots/circles --> <div style="text-align: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(4)"></span> </div>

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

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