【问题标题】:Moving a slideshow using CSS/HTML使用 CSS/HTML 移动幻灯片
【发布时间】:2020-08-18 21:11:58
【问题描述】:

我对 HTML/CSS 很陌生,偶然发现了一个教程,可以帮助我为我正在制作的网站创建图像幻灯片。我想出了如何让幻灯片工作,但还没有想出如何移动它。 抱歉,如果格式关闭,我会使用更漂亮的,但不确定我是否喜欢它。提前致谢!

Video by Clever Techie

/*image slider*/

.slide-container {
  top: 150%;
  width: 586px;
  height: 330;
  margin-left: auto;
  margin-right: auto;
  overflow: hidden;
  text-align: center;
}

.image-container {
  top: 100%;
  width: 2344px;
  height: 330;
  position: relative;
  transition: left 0.5s;
  -webkit-transition: left 0.5s;
  -moz-transition: left 1s;
}

.slider-image {
  float: left;
  margin: 0px;
  padding: 0px;
}

.button-container {
  top: 120%;
  position: relative;
}

.slider-button {
  top: 120%;
  display: inline-block;
  height: 10px;
  width: 10px;
  border-radius: 5px;
  background-color: #fff;
}

#slider-image-1:target~.image-container {
  left: 0px;
}

#slider-image-2:target~.image-container {
  left: -586px;
}

#slider-image-3:target~.image-container {
  left: -1172px;
}

#slider-image-4:target~.image-container {
  left: -1758px;
}
<div class="title">
  <h1>Photoreal Scenery</h1>
</div>

<div class="slide-container">
  <span id="slider-image-1"></span>
  <span id="slider-image-2"></span>
  <span id="slider-image-3"></span>
  <span id="slider-image-4"></span>
  <div class="image-container">
    <img src="https://via.placeholder.com/150" class="slider-image" />
    <img src="https://via.placeholder.com/150" class="slider-image" />
    <img src="https://via.placeholder.com/150" class="slider-image" />
    <img src="https://via.placeholder.com/150" class="slider-image" />
  </div>
</div>
<div class="button-container">
  <a href="#slider-image-1" class="slider-button"></a>
  <a href="#slider-image-2" class="slider-button"></a>
  <a href="#slider-image-3" class="slider-button"></a>
  <a href="#slider-image-4" class="slider-button"></a>
</div>

【问题讨论】:

标签: html css


【解决方案1】:

由于您没有描述您需要哪种类型的幻灯片,我创建了可以自动运行或可以由用户控制的幻灯片。用户点击或网站加载后,10 秒后开始自动幻灯片放映。自动幻灯片每 3 秒更换一次幻灯片。看看这个 sn-p:

var slideIndex = 0;
var timer1, timer;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("dot");

function showSlides() {//called by automatic slideshow
    clearTimeout(timer1);//clear previously set timer 

    //clear styles
    for (var i = 0; i < slides.length; i++)
        slides[i].style.display = "none";           
    for (var i = 0; i < dots.length; i++) 
        dots[i].className = dots[i].className.replace(" active", "");
    
    //reset slideIndex
    if (++slideIndex == slides.length)
        slideIndex = 0;

    //reset styles
    slides[slideIndex].style.display = "block"; 
    dots[slideIndex].className += " active";
    
    //reset timer
    timer1 = setTimeout(showSlides, 3000);
}

function showSlide(n) {//called when user manually choose particular slide
    //clear previously set timers
    clearTimeout(timer1);
    clearTimeout(timer);

    //clear styles
    for (var i = 0; i < slides.length; i++) 
        slides[i].style.display = "none";
    for (var i = 0; i < dots.length; i++) 
        dots[i].className = dots[i].className.replace(" active", "");

    //reset slideIndex
    if (n == slides.length)
        slideIndex = 0; 
    if (n < 0)
        slideIndex = slides.length;

    //reset styles
    slides[slideIndex].style.display = "block"; 
    dots[slideIndex].className += " active";

    //reset timer for automatic slideshow
    timer = setTimeout(showSlides, 10000);
}
/* make width, height, min/max properties includes content, padding and border */
* {box-sizing:border-box}

/* Slideshow container */
.slideshow-container {
    width: 50vw;
    height: 50vh;
    position: relative;
    margin: auto;
    clear:both;
    background-color: black;        
}

/* Hide the images by default */
.mySlides {
    display: none;
}

/* make images fit to slide width */
.mySlides img {
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
    text-align: center;
    height: 50vh; object-fit:  fill;
}

/* Next & previous buttons */
.prev, .next {
    cursor: pointer;
    position: absolute;
    top: 50%;
    margin: 0;
    width: auto;
    padding: 16px;
    color: white;
    font-weight: bold;
    font-size: xx-large;
    transition: 0.6s;
    user-select: none;
    -ms-transform: translateY(-50%);
    transform: translateY(-50%);

}

/* Position the "next button" to the right */
.next {
    right: 0;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
    background-color: black;
    opacity: 0.7;
}

/* Caption text */
.text {
    color: #f2f2f2;
    font-size: 4em;
    padding-bottom: : 12px;
    position: absolute;
    bottom: 8px;
    width: 100%;
    text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
    color: #f2f2f2;
    font-size: 1.5em;
    padding: 8px 12px;
    position: absolute;
    top: 0;
    z-index: 1;
}


/* Dots container */
.dots-container {
    text-align: center;
}


/* The dots/bullets/indicators */
.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;
}

/* Fading animation */
.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}
}

/* Small devices specific alterations */
@media only screen and (max-width: 600px) {
    /* Slideshow container */
    .slideshow-container {
        width: 96vw;
    }

    .text {
        font-size: 1.5em;
    }

    .numbertext {
        font-size: 1em;
    }

    /* Next & previous buttons */
    .prev, .next {
        padding: 4px;
        font-weight: bold;
        font-size: large;
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Apple</title>
</head>
<!-- start slideshow on page load. -->
<body onload="showSlide(1);">
    <div class="title">
        <h1>Photoreal Scenery</h1>
    </div>

    <!-- Slideshow container -->
    <div class="slideshow-container">

        <!-- Full-width images with number and caption text -->
        <div class="mySlides fade" >
            <div class="numbertext">1 / 4</div>
            <img src="red.png">
            <div class="text">Caption Text</div>
        </div>

        <div class="mySlides fade">
            <div class="numbertext">2 / 4</div>
            <img src="blue.png">
            <div class="text">Caption Two</div>
        </div>

        <div class="mySlides fade">
            <div class="numbertext">3 / 4</div>
            <img src="green.png">
            <div class="text">Caption Three</div>
        </div>

        <div class="mySlides fade">
            <div class="numbertext">4 / 4</div>
            <img src="j.jpg">
            <div class="text">Caption Three</div>
        </div>

        <!-- Next and previous buttons -->
        <a class="prev" onclick="showSlide(--slideIndex);">&#10094;</a>
        <a class="next" onclick="showSlide(++slideIndex);">&#10095;</a>
    </div>
    <br>

    <!-- The dots/circles -->
    <div class="dots-container">
        <span class="dot" onclick="showSlide(slideIndex = 0)"></span> 
        <span class="dot" onclick="showSlide(slideIndex = 1)"></span> 
        <span class="dot" onclick="showSlide(slideIndex = 2)"></span> 
        <span class="dot" onclick="showSlide(slideIndex = 3)"></span> 
    </div>
</body>
</html>

逻辑如下:

HTML

我们添加了&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt; 以使页面宽度跟随设备宽度并将初始缩放设置为1.0。

这些是类

  1. slideshow-container - 存储容器
  2. mySlides - 幻灯片
  3. fade - 淡出动画
  4. 上一个/下一个 - 上一个、下一个按钮
  5. dots-container - 存储点
  6. 点 - 单个点

CSS 重要的 CSS 添加是

  1. *(all) - 在高度宽度测量中包含边框、填充
  2. slideshow-container - 将其设置为视口高度和宽度的 50%
  3. mySlides - 隐藏所有幻灯片
  4. mySlides img - 居中图像并设置边界
  5. .prev, .next - 禁用选择,改为指向光标添加过渡
  6. .numbertext - 将 z-index 设置为 1
  7. .dot - 更改为指向光标添加过渡
  8. @-webkit-keyframes fade - 设置 IE 特定的淡入淡出规则
  9. @keyframes fade - 设置渐变规则
  10. @media only screen and (max-width: 600px) - 针对小屏幕设备的调整

JavaScript 有两个功能:

  1. showSlides() - 用于创建自动幻灯片。
  2. showSlide(n) - 用于查看用户选择的幻灯片。

【讨论】:

  • 您的代码有错误。在第一项上单击左键时显示错误。
【解决方案2】:

欢迎来到 SO。

在这种情况下,您似乎正在尝试纯 HTML CSS 解决方案,请检查此 sn-p(使用完整的网页浏览)。与此相关的教程是here

.row {
  background: #fff;
  margin-top: 20px;
}

.col {
  border: solid 1px #6c757d;
  padding: 10px;
}
* {
  box-sizing: border-box;
}

.slider {
  width: 300px;
  text-align: center;
  overflow: hidden;
}

.slides {
  display: flex;
  
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  
  
  
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  
  /*
  scroll-snap-points-x: repeat(300px);
  scroll-snap-type: mandatory;
  */
}
.slides::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
.slides::-webkit-scrollbar-thumb {
  background: black;
  border-radius: 10px;
}
.slides::-webkit-scrollbar-track {
  background: transparent;
}
.slides > div {
  scroll-snap-align: start;
  flex-shrink: 0;
  width: 100%;
  height: 400px;
  margin-right: 50px;
  border-radius: 10px;
  background: #eee;
  transform-origin: center center;
  transform: scale(1);
  transition: transform 0.5s;
  position: relative;
  
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 100px;
}
.slides > div:target {
/*   transform: scale(0.8); */
}
.author-info {
  background: rgba(0, 0, 0, 0.75);
  color: white;
  padding: 0.75rem;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  margin: 0;
}
.author-info a {
  color: white;
}
img {
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.slider > a {
  display: inline-flex;
  width: 1.5rem;
  height: 1.5rem;
  background: white;
  text-decoration: none;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  margin: 0 0 0.5rem 0;
  position: relative;
}
.slider > a:active {
  top: 1px;
}
.slider > a:focus {
  background: #000;
}

/* Don't need button navigation */
@supports (scroll-snap-type) {
  .slider > a {
    display: none;
  }
}

html, body {
  height: 100%;
  overflow: hidden;
}
body {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(to bottom, #ddd, #eee);
  font-family: 'Ropa Sans', sans-serif;
}
<div class="slider">
   <a href="#slide-1">1</a>
   <a href="#slide-2">2</a>
   <a href="#slide-3">3</a>
   <a href="#slide-4">4</a>
   <a href="#slide-5">5</a>
   <div class="slides">
      <div id="slide-1">
         <img
            src="https://image.freepik.com/free-photo/empty-wooden-plank-floor-space-with-green-garden-leaves-product-display-space-with-fresh-green-nature_1150-18098.jpg"
            class="slider-image"
            />
      </div>
      <div id="slide-2">
         <img
            src="https://image.freepik.com/free-photo/wooden-board-empty-table-top-blurred-background_1253-1584.jpg"
            class="slider-image"
            />
      </div>
      <div id="slide-3">
         <img
            src="https://image.freepik.com/free-photo/boards-with-unfocused-background_1253-55.jpg"
            class="slider-image"
            />
      </div>
      <div id="slide-4">
         <img
            src="https://image.freepik.com/free-photo/wooden-table-looking-out-defocussed-room-interior_1048-11830.jpg"
            class="slider-image"
            />
      </div>
      <div id="slide-5">
         <img
            src="https://image.freepik.com/free-photo/empty-wood-table-top-blur-glass-window-interior-restaurant-banner-mock-up-abstract-background-can-used-display-montage-your-products_7191-1132.jpg"
            class="slider-image"
            />
      </div>
   </div>
</div>

另外,正如 Squashie01 所建议的,您可以使用引导类来轻松完成此操作,这里是 fiddle 使用最新的引导类。参考文档here.

【讨论】:

    【解决方案3】:
        <div id="carouselExampleControls" class="carousel slide" data-ride="carousel">
           <div class="carousel-inner">
               <div class="carousel-item active">
                 <img class="d-block w-100" src="..." alt="First slide">
               </div>
               <div class="carousel-item">
                 <img class="d-block w-100" src="..." alt="Second slide">
               </div>
               <div class="carousel-item">
                 <img class="d-block w-100" src="..." alt="Third slide">
               </div>
             </div>
             <a class="carousel-control-prev" href="#carouselExampleControls" role="button" data-slide="prev">
               <span class="carousel-control-prev-icon" aria-hidden="true"></span>
               <span class="sr-only">Previous</span>
             </a>
             <a class="carousel-control-next" href="#carouselExampleControls" role="button" data-slide="next">
               <span class="carousel-control-next-icon" aria-hidden="true"></span>
               <span class="sr-only">Next</span>
             </a>
           </div>                                                         
    

    【讨论】:

      猜你喜欢
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 2015-12-02
      • 2012-08-07
      • 2015-04-21
      相关资源
      最近更新 更多