簡體   English   中英

輪播下一個和上一個按鈕不起作用

[英]Carousel next and prev button not working

因此,對於我的網站,我正在嘗試實現“輪播”功能,但是圖片沒有調整大小,next和prev功能不起作用。 有什么建議么?

我嘗試將腳本包含在html函數的底部,並確保所有函數均已定義。 我想做的是讓html頁面上的圖片變小,並在圖片下方具有上一個和下一個功能,並確保輪播能夠正常工作

HTML

    <h1 class="header"> Projects </h1>
        <div id="container">
            <div class = "carousel-item fade" style="width: 50%"> 
                <img src = "hackBU.png"> 
                <div class = "carousel-text"> Text </div>

            </div>
            <div class = "carousel-item fade"> 
                <img src = "pigGame.png"> 
                <div class = "carousel-text"> Text </div>

            </div>
            <div class = "carousel-item fade"> 
                <img src = "connect4.png"> 
                <div class = "carousel-text"> Text </div>

            </div>
            <div class = "carousel-item fade"> 
                <img src = "ese123clock.png"> 
                <div class = "carousel-text"> Text </div>

            </div>

            <!-- Next and previous buttons -->
              <a class="prev" onclick="plusItem(-1)">&#10094;</a>
              <a class="next" onclick="plusItem(1)">&#10095;</a>

        </div>





    </div>






    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script type="text/javascript" src="script.js"></script>

CSS

#projects{
width: auto;
height: 40rem;
margin: 4rem;
text-align: center;


}
.header{

    color: white;
    text-align: center;
    font-family: Helvetica Neue',Helvetica,Arial,sans-serif;

}
.container{
    width: 40rem;
    position: relative;
    margin: auto;
}
.carousel-item{
    display: none;
    width: 10%;
}
.prev,.next{
  cursor: pointer;
  position: inherit;
  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;


}

.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: blue;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}


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

JS

document.addEventListener("DOMContentLoaded", function(event) { 

var itemIndex = 1;
showItem(itemIndex);

// Next/previous controls
function plusItem(n) {
  showItem(itemIndex += n);
}

// Thumbnail image controls
function currentItem(n) {
  showItem(itemIndex = n);
}

function showItem(n) {

  var item = document.getElementsByClassName("carousel-item");
  if (n > item.length) {itemIndex = 1} 
  if (n < 1) {itemIndex = item.length}
  for (var i = 0; i < item.length; i++) {
      item[i].style.display = "none"; 
  }

  item[itemIndex-1].style.display = "block"; 
}
},false);

我假設您只需要有人告訴您為什么您的事件沒有按一下就觸發; 如果您需要更多幫助,請告訴我。

單擊箭頭時,未定義要調用的函數(plusItem),因為您是在未觸發的“ DOMContentLoaded”處理程序中定義它們的。

我將函數聲明移到“ DOMContentLoaded”處理程序之外。 請參見下面的代碼段;

var itemIndex = 1;
    document.addEventListener("DOMContentLoaded", function(event) {
        showItem(itemIndex);
    },false);

    // Next/previous controls
    function plusItem(n) {
        console.log('plusitem')
        showItem(itemIndex += n);
    }

    // Thumbnail image controls
    function currentItem(n) {
        showItem(itemIndex = n);
    }

    function showItem(n) {

        var item = document.getElementsByClassName("carousel-item");
        if (n > item.length) {itemIndex = 1}
        if (n < 1) {itemIndex = item.length}
        for (var i = 0; i < item.length; i++) {
            item[i].style.display = "none";
        }

        item[itemIndex-1].style.display = "block";
    }

至於為什么您的“ DOMContentLoaded”處理程序沒有觸發,我認為對於頁腳腳本檢測頁面加載而言,這執行得太早了。 如果您的腳本位於頁腳中,則無需為此檢測頁面加載。 或者,查看窗口加載: https : //developer.mozilla.org/en-US/docs/Web/API/Window/load_event

為了將來參考,最好在開發JavaScript時查看控制台會給您什么反饋,因為在這種情況下,您會看到“未定義plusItem”這將為您提供線索。 使用控制台日志記錄( console.log(“ abc”) )對查看腳本的哪些部分正在執行也非常有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM