首页 > 解决方案 > 轮播正在与英雄图像和导航栏合并

问题描述

我正在制作一个有趣的小网站...我在https://www.w3schools.com的帮助下创建了 Hero Image 和 Navbar,还尝试添加 Carousel,但它似乎在 Navbar 和 Hero 之间合并图片...

参考: 没有旋转木马

使用轮播:合并!

我想要实现的是This但它应该在它下面,而不是合并到...

我尝试了很多方法并搜索了整个 StackOverFlow 站点,但似乎我有一个愚蠢的大脑,无法理解这个愚蠢的问题。

以下是一些源代码,以备不时之需: CSS

.hero-image{
    background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("heroimage.jpg");
    height: 50%;
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
    position: relative;
}
.button {
    background-color: #4CAF50; /* Green */
    border: none;
    color: white;
    padding: 16px 32px;
    text-align: center;
    text-decoration: none;
    display: inline-block;
    font-size: 16px;
    margin: 4px 2px;
    transition-duration: 0.4s;
    cursor: pointer;
}
.hero-text{
    text-align: center;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: white;
    font-family: 'Ubuntu', sans-serif;
}
nav {
    background-color: black;
    height: 50px;
}
nav a {
    float: right;
    color: white;
    background-color: 1a1a1a;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
    font-size: 16px;
    font-family: 'Open Sans', sans-serif;
}
nav a:hover {
    background-color: #3b86ff;
}
* {box-sizing:border-box}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: fixed;
  margin: auto;
  z-index:4;
}

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

/* Next & previous buttons */
.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;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

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

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

/* 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}
}

HTML

<html>
    <head>
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
        <link rel="preconnect" href="https://fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@300&display=swap" rel="stylesheet">
        <link rel="preconnect" href="https://fonts.gstatic.com">
        <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&family=Ubuntu:wght@300&display=swap" rel="stylesheet">
        <link rel="stylesheet" href="styles.css">
        <title>
        Checkmate - Chess Tournaments Online
        </title>
        <link rel = "icon" href ="CheckmateIcon.jpeg" type = "image/x-icon">
    </head>
<body>
    <div class="hero-image">
        <div class="hero-text">
            <h1 style="font-size:50px">Checkmate</h1>
            <p>Chess Tournaments Online</p>
        </div>
    </div>
                <script>
                    var slideIndex = 1;
                    showSlides(slideIndex);
                        function plusSlides(n) {
                        showSlides(slideIndex += n);
                        }
                        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";
                        }
                </script>>
    <nav>
        <a href=#>Tournaments</a>
        <a href=#>Event Plan</a>
        <a href=#>Register</a>
    <nav>
    <div class="slideshow-container">
    <div class="mySlides fade">
        <div class="numbertext">1 / 3</div>
            <img src="img1.jpg" style="width:100%">
        <div class="text">Caption Text</div>
    </div>
    <div class="mySlides fade">
        <div class="numbertext">2 / 3</div>
            <img src="img2.jpg" style="width:100%">
        <div class="text">Caption Two</div>
    </div>
    <div class="mySlides fade">
        <div class="numbertext">3 / 3</div>
            <img src="img3.jpg" style="width:100%">
        <div class="text">Caption Three</div>
    </div>
    <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
    <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
<br>
    <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>
    </div>
</body>
</html>

告诉我我做错了什么TT_TT

标签: javascripthtmlcss

解决方案


我可以从这里看到的是您position:fixed在幻灯片容器上使用而不给它top属性。它会坚持top

尝试这个

/* 幻灯片容器 */

.slideshow-container {
  max-width: 1000px;
  position: fixed;
  margin: auto;
  z-index:4;
  top:50px;

}

 <body>
<nav>
  <a href="#">Tournaments</a>
  <a href="#">Event Plan</a>
  <a href="#">Register</a>
  <nav>
    <div class="hero-image">
      <div class="hero-text">
        <h1 style="font-size: 50px">Checkmate</h1>
        <p>Chess Tournaments Online</p>
      </div>
    </div>
  </nav>
</nav>


<div class="slideshow-container">
  <div class="mySlides fade">
    <div class="numbertext">1 / 3</div>
    <img
      src="https://cdn.pixabay.com/photo/2021/05/06/10/27/tulips-6233077__480.jpg"
      style="width: 100%"
    />
    <div class="text">Caption Text</div>
  </div>
  <div class="mySlides fade">
    <div class="numbertext">2 / 3</div>
    <img
      src="https://cdn.pixabay.com/photo/2021/05/06/10/27/tulips-6233077__480.jpg"
      style="width: 100%"
    />
    <div class="text">Caption Two</div>
  </div>
  <div class="mySlides fade">
    <div class="numbertext">3 / 3</div>
    <img
      src="https://cdn.pixabay.com/photo/2021/05/06/10/27/tulips-6233077__480.jpg"
      style="width: 100%"
    />
    <div class="text">Caption Three</div>
  </div>
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
<br />
<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>
</div>
<script>
  var slideIndex = 1;
  showSlides(slideIndex);
  function plusSlides(n) {
    showSlides((slideIndex += n));
  }
  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";
  }
</script>

推荐阅读