幻灯片放映的第一个元素不会显示,按钮也不起作用

时间:2017-04-15 08:40:19

标签: javascript html css slideshow

所以我试图在同一页面上创建多个幻灯片,我现在设法获得此代码:

HTML

<div class="portfolio-slideshow">
    <a class="prev" onclick="slide(0,-1)">❮</a>
    <div class="slide"> <img src="img/dailyui/008.png" class="slideimg_0" id="0"> </div>
    <div class="slide"> <img src="img/dailyui/007.jpg" class="slideimg_0" id="1"> </div>
    <div class="slide"> <img src="img/dailyui/006.jpg" class="slideimg_0" id="2"> </div>
    <div class="slide"> <img src="img/dailyui/003.jpg" class="slideimg_0" id="3"> </div>
    <a class="next" onclick="slide(0,1)">❯</a>
</div>

<div class="portfolio-slideshow">
    <a class="prev" onclick="slide(1,-1)">❮</a>
    <div class="slide"> <img src="img/dailyui/008.png" class="slideimg_1" id="0"> </div>
    <div class="slide"> <img src="img/dailyui/007.jpg" class="slideimg_1" id="1"> </div>
    <div class="slide"> <img src="img/dailyui/006.jpg" class="slideimg_1" id="2"> </div>
    <div class="slide"> <img src="img/dailyui/003.jpg" class="slideimg_1" id="3"> </div>
    <a class="next" onclick="slide(1,1)">❯</a>
</div>

我的身体:

<body onLoad="showSlides()">

SCSS

.portfolio-slideshow{
    width: 30%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.slide{
    display: none;
    padding:0 35px;

    img{
        max-height:40vh;
        max-width:100%;
        border-radius:5px;
        -webkit-filter: drop-shadow(5px 5px 5px #000);
        filter: drop-shadow(0px 2px 5px #222);
        cursor: zoom-in;
        display: none;
    }
}

.prev,.next{
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.2s ease;
  opacity: 0.25;

  &:hover{ opacity: 1; }
}

的Javascript

var slideIndex = [0,0]
function showSlides(){
    var aux = slideIndex.length;
    var i;
    for(i=0; i<aux; i++){
        slide(i,0);
    }
}
function slide(n,m){
    var i;
    var aux = document.getElementsByClassName("slideimg_" + n);
    var aux2 = aux.length - 1;
    slideIndex[n] += m;
    if (slideIndex[n] < 0) slideIndex = aux2;
    else if (slideIndex[n] > aux2) slideIndex = 0;
    for(i=0; i<aux.length; i++){ aux[i].style.display = "none"; }
    aux[slideIndex[n]].style.display = "block";
}

提到的另一件事是,当我点击下一个/上一个按钮时,我得到了这个:

  

未捕获的TypeError:无法读取属性&#39; style&#39;未定义的       在幻灯片(websitejs2.js:17)       在HTMLAnchorElement.onclick(index2.html:87)

那么,你们中有谁可以帮助我完成任务吗?

1 个答案:

答案 0 :(得分:0)

通过代码中的评论。如果有疑问,请添加评论

&#13;
&#13;
var slideIndex = [0, 0]

function showSlides() {
  var aux = slideIndex.length;
  var i;
  for (i = 0; i < aux; i++) {
    slide(i, 0);
  }
}

function slide(n, m) {
  var i;
  var aux = document.getElementsByClassName("slideimg_" + n);
  var aux2 = aux.length - 1;
  /*added logic */
  if (slideIndex[n] == 3 && m == 1) {
    slideIndex[n] = -1
  }
  if (slideIndex[n] == 0 && m == -1) {
    slideIndex[n] = 4
  }
  /*added logic end*/
  slideIndex[n] += m;

  if (slideIndex[n] < 0) slideIndex = aux2;
  else if (slideIndex[n] > aux2) slideIndex = 0;
  for (i = 0; i < aux.length; i++) {
    aux[i].style.display = "none";
  }
  aux[slideIndex[n]].style.display = "block";
}
&#13;
.portfolio-slideshow {
  width: 30%;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.slide {
  display: block;
  /*changed to block*/
  padding: 0 35px;
  img {
    max-height: 40vh;
    max-width: 100%;
    border-radius: 5px;
    -webkit-filter: drop-shadow(5px 5px 5px #000);
    filter: drop-shadow(0px 2px 5px #222);
    cursor: zoom-in;
    display: none;
  }
}

.prev,
.next {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: black;
  /*changed to black*/
  font-weight: bold;
  font-size: 18px;
  transition: 0.2s ease;
  opacity: 1;
  /*changed to 1*/
  &:hover {
    opacity: 1;
  }
}


/*added*/

img {
  height: 100px;
  width: 100px;
  display: none;
}
&#13;
<body onLoad="showSlides()">
  <div class="portfolio-slideshow">
    <a class="prev" onclick="slide(0,-1)">❮</a>
    <div class="slide"> <img src="https://static.pexels.com/photos/158507/marigold-calendula-orange-blossom-158507.jpeg" class="slideimg_0" id="0"> </div>
    <div class="slide"> <img src="https://static.pexels.com/photos/59836/snowflake-flower-blossom-bloom-59836.jpeg" class="slideimg_0" id="1"> </div>
    <div class="slide"> <img src="https://static.pexels.com/photos/93013/pexels-photo-93013.jpeg" class="slideimg_0" id="2"> </div>
    <div class="slide"> <img src="https://static.pexels.com/photos/101538/dandelion-seeds-nature-spring-101538.jpeg" class="slideimg_0" id="3"> </div>
    <a class="next" onclick="slide(0,1)">❯</a>
  </div>

  <div class="portfolio-slideshow">
    <a class="prev" onclick="slide(1,-1)">❮</a>
    <div class="slide"> <img src="https://static.pexels.com/photos/158507/marigold-calendula-orange-blossom-158507.jpeg" class="slideimg_1" id="0"> </div>
    <div class="slide"> <img src="https://static.pexels.com/photos/59836/snowflake-flower-blossom-bloom-59836.jpeg" class="slideimg_1" id="1"> </div>
    <div class="slide"> <img src="https://static.pexels.com/photos/93013/pexels-photo-93013.jpeg" class="slideimg_1" id="2"> </div>
    <div class="slide"> <img src="https://static.pexels.com/photos/101538/dandelion-seeds-nature-spring-101538.jpeg" class="slideimg_1" id="3"> </div>
    <a class="next" onclick="slide(1,1)">❯</a>
  </div>
</body>
&#13;
&#13;
&#13;