当我有一个滑块时右边的空白区域

时间:2016-07-27 23:35:53

标签: javascript html css slideshow

所以我想尝试一个包含内容的滑块菜单。好吧,我已完成所有菜单,并且滑块有效,但右侧只有一个很小的空白区域,使其滚动。这真的没什么大不了的,但确实让我感到烦恼。如果你们能帮助我解决这个问题,那将非常感激。

$('span.nav-btn').click(function() {


  $(window).resize(function() {
    if ($(window).width() > 1000) {

      $('ul.nav').removeAttr('style');
    }
  })
});
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";
}
/* Slideshow container */

.slideshow-container {
  width: 100%;
  position: relative;
}
/* Next & previous buttons */

.prev,
.next {
  cursor: pointer;
  position: absolute;
  top: 0;
  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;
}
/* 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: 13px;
  width: 13px;
  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,
body {
  padding: 0;
  margin: 0;
  font-family: "Roboto", sans-serif;
}
.nav {
  background-color: #E74C3C;
  color: #fff;
  margin: 0;
  list-style: none;
  height: 100%;
}
.nav > li {
  display: inline-block;
}
.nav > li > a {
  text-decoration: none;
  font-size: 40px;
  color: #fff;
  padding-left: 10px;
  padding-right: 10px;
  padding-bottom: 25px;
  padding-top: 15px;
  font-weight: bold;
}
.nav li:hover {
  background-color: rgb(255, 0, 0);
  margin: 0;
  padding;
  0;
}
h2 {
  font-size: 100px;
}
.content {
  /*
	width: 100%;*/
  background-color: #333;
}
.contentArea {
  width: 1000px;
  background-color: #fff;
}
@media (max-width: 1000px) {
  .nav {
    text-align: left;
    display: none;
  }
  .nav > li {
    display: block;
  }
  .nav > li > a {
    font-size: 80px;
    line-height: 150px;
  }
  .nav-btn {
    display: block;
    background-color: #333;
    color: #FFF;
    font-size: 150px;
    text-align: center;
    cursor: pointer;
    height: 200px;
  }
  .nav-btn:before {
    content: "Menu";
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="container">
  <!-- Begin Navigation -->
  <nav>
    <span class="nav-btn"></span>
    <ul class="nav">
      <li><a href="#home">HOME</a>
      </li>
      <li><a href="#about">ABOUT</a>
      </li>
      <li><a href="#projects">PROJECTS</a>
      </li>
      <li><a href="#videos">VIDEOS</a>
      </li>
      <li><a href="#contact">CONTACT</a>
      </li>
    </ul>
  </nav>
  <!-- End Navigation -->

  <!-- Begin Content -->
  <div class="content">

    <div class="slideshow-container">
      <div class="mySlides fade">
        <div class="numbertext">1 / 3</div>
        <img src="http://www.placecage.com/c/460/460" style="width:100%">
        <div class="text">Caption Text</div>
      </div>
      <div class="mySlides fade">
        <div class="numbertext">2 / 3</div>
        <img src="http://www.placecage.com/g/460/460" style="width:100%">
        <div class="text">Caption Two</div>
      </div>
      <div class="mySlides fade">
        <div class="numbertext">3 / 3</div>
        <img src="http://www.placecage.com/460/460" 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>

    <!-- Begin Content Area -->
    <div class="contentArea">
      <p>hi</p>
    </div>
    <!-- End Content Area -->
  </div>
  <!-- End Content -->
</div>

我把滑块css和主css放在不同的文件中,因为我想让它保持干净整洁。

此外,如果您想查看该网站,可以在此处访问该网站。 jumpymotionpictures.net

2 个答案:

答案 0 :(得分:0)

我建议您在CSS中添加以下内容:

body {
  overflow-x: hidden;
}

这将通过隐藏x轴上的溢出来简单地停止......

因此,您将无法滚动,也无法看到空白区域!

答案 1 :(得分:0)

这有几个原因。

  1. 您的contentArea的固定宽度为1000像素,请尝试将其更改为其他内容,例如100%。这对小屏幕有帮助。

  2. 有一个类text的容器。在大多数浏览器中,box-sizing默认为content-box,不包括宽度计算中的填充。当容器的宽度设置为100%并且它具有填充时,填充将被添加到该100%的顶部。 要将填充包含在容器的宽度中,请将其添加到text类样式中:box-sizing: border-box;

  3. &#13;
    &#13;
    $('span.nav-btn').click(function() {
    
    
      $(window).resize(function() {
        if ($(window).width() > 1000) {
    
          $('ul.nav').removeAttr('style');
        }
      })
    });
    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";
    }
    &#13;
    /* Slideshow container */
    
    .slideshow-container {
      width: 100%;
      position: relative;
    }
    /* Next & previous buttons */
    
    .prev,
    .next {
      cursor: pointer;
      position: absolute;
      top: 0;
      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;
    }
    /* 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;
      box-sizing: border-box;
    }
    /* 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: 13px;
      width: 13px;
      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,
    body {
      padding: 0;
      margin: 0;
      font-family: "Roboto", sans-serif;
    }
    .nav {
      background-color: #E74C3C;
      color: #fff;
      margin: 0;
      list-style: none;
      height: 100%;
    }
    .nav > li {
      display: inline-block;
    }
    .nav > li > a {
      text-decoration: none;
      font-size: 40px;
      color: #fff;
      padding-left: 10px;
      padding-right: 10px;
      padding-bottom: 25px;
      padding-top: 15px;
      font-weight: bold;
    }
    .nav li:hover {
      background-color: rgb(255, 0, 0);
      margin: 0;
      padding;
      0;
    }
    h2 {
      font-size: 100px;
    }
    .content {
      /*
    	width: 100%;*/
      background-color: #333;
    }
    .contentArea {
      width: 100%;
      background-color: #fff;
    }
    @media (max-width: 1000px) {
      .nav {
        text-align: left;
        display: none;
      }
      .nav > li {
        display: block;
      }
      .nav > li > a {
        font-size: 80px;
        line-height: 150px;
      }
      .nav-btn {
        display: block;
        background-color: #333;
        color: #FFF;
        font-size: 150px;
        text-align: center;
        cursor: pointer;
        height: 200px;
      }
      .nav-btn:before {
        content: "Menu";
      }
    }
    &#13;
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <div class="container">
      <!-- Begin Navigation -->
      <nav>
        <span class="nav-btn"></span>
        <ul class="nav">
          <li><a href="#home">HOME</a>
          </li>
          <li><a href="#about">ABOUT</a>
          </li>
          <li><a href="#projects">PROJECTS</a>
          </li>
          <li><a href="#videos">VIDEOS</a>
          </li>
          <li><a href="#contact">CONTACT</a>
          </li>
        </ul>
      </nav>
      <!-- End Navigation -->
    
      <!-- Begin Content -->
      <div class="content">
    
        <div class="slideshow-container">
          <div class="mySlides fade">
            <div class="numbertext">1 / 3</div>
            <img src="http://www.placecage.com/c/460/460" style="width:100%">
            <div class="text">Caption Text</div>
          </div>
          <div class="mySlides fade">
            <div class="numbertext">2 / 3</div>
            <img src="http://www.placecage.com/g/460/460" style="width:100%">
            <div class="text">Caption Two</div>
          </div>
          <div class="mySlides fade">
            <div class="numbertext">3 / 3</div>
            <img src="http://www.placecage.com/460/460" 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>
    
        <!-- Begin Content Area -->
        <div class="contentArea">
          <p>hi</p>
        </div>
        <!-- End Content Area -->
      </div>
      <!-- End Content -->
    </div>
    &#13;
    &#13;
    &#13;