Uncaught TypeError: Cannot read property 'style' of undefined - multiple slideshow - javascript

时间:2019-03-19 15:18:44

标签: javascript html css

Here's the code:

var slideIndex = [1, 1];
        var slideId = ["mySlides1", "mySlides2"]
        showSlides(1, 0);
        showSlides(1, 1);

        function plusSlides(n, no) {
            showSlides(slideIndex[no] += n, no);
        }

        function showSlides(n, no) {
            var i;
            var x = document.getElementsByClassName(slideId[no]);
            if (n > x.length) {
                slideIndex[no] = 1
            }
            if (n < 1) {
                slideIndex[no] = x.length
            }
            for (i = 0; i < x.length; i++) {
                x[i].style.display = "none";
            }
            x[slideIndex[no] - 1].style.display = "block";
        }
* {
    box-sizing: border-box;
}

body {
    background-color: #addddc;
}

.mySlides1,
.mySlides2 {
    display: none;
}


.slideshow-container {
    max-width: 950px;
    position: relative;
    margin: 0 auto;
}



.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
    }
}



.prev,
.next {
    cursor: pointer;
    position: absolute;
    top: 50%;
    width: auto;
    padding: 16px;
    margin-top: -22px;
    color: rgb(14, 13, 13);
    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;
}



.prev:hover,
.next:hover {
    background-color: #f1f1f1;
    color: black;
}


 

.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;
    }
}


/* RATES */

.rates-wrapper,
.rates-wrapper1,
.rates-wrapper2 {
    margin-left: 55px;
    box-sizing: border-box;
}

#rates {
    margin: 600px auto 0;
    /* width: 100%; */
    max-width: 100%;
    /* height: 1500px; */
    padding: 200px;
    box-sizing: border-box;
    background-color: #addddc;
}

   

.box-infos {
    width: 260px;
    height: 290px;
    background-color: #fff;
    border-radius: 0px;
    margin-top: 20px;
}

.box-infos h2 {
    font-family: 'Open Sans Condensed', sans-serif;
    font-weight: normal;
    font-size: 18px;
    font-stretch: ultra-condensed;
    text-align: center;
    letter-spacing: -0.5px;
    color: #53c3c5;
    padding: 10px;
}

.box-infos p {
    font-family: 'Open Sans Condensed', sans-serif;
    font-weight: bolder;
    font-size: 12px;
    font-stretch: ultra-condensed;
    text-align: center;
    letter-spacing: -0.5px;
    color: #666666;
    padding-top: -5px;
}

.clear2 {
    clear: both;
}

.box-wrap {
    margin-right: 30px;
    float: left;
}

.clear3 {
    clear: both;
}


/* pack2 */

.rates-bed1 {
    width: 840px;
    height: 105px;
    font-family: 'Amatic SC', cursive;
    font-weight: bold;
    font-size: 30px;
    background: #007b7b;
    border-radius: 5px;
    text-align: left;
    text-transform: uppercase;
    color: #fff;
    letter-spacing: 0.3px;
    line-height: 30px;
    margin-top: 10px;
    margin-bottom: 20px;
    padding-left: 120px;
    padding-top: 20px;
    box-sizing: border-box;
}



#rates {
    margin: 600px auto 0;
    width: 100%;
    max-width: 100%;
    padding: 200px;
    box-sizing: border-box;
    background-color: #addddc;
}

.title-rates1 {
    width: 260px;
    height: 45px;
    background-color: #007b7b;
    margin-top: -20px;
    border-radius: 5px;
    box-sizing: border-box;
}

.title-rates1 h1 {
    font-family: 'Amatic SC', cursive;
    font-size: 30px;
    letter-spacing: 1px;
    color: #fff;
    text-align: center;
}

.box-infos1 {
    width: 260px;
    height: 360px;
    background-color: #fff;
    border-radius: 0px;
    margin-top: 20px;
}

.box-infos1 h2 {
    font-family: 'Open Sans Condensed', sans-serif;
    font-weight: normal;
    font-size: 18px;
    font-stretch: ultra-condensed;
    text-align: center;
    letter-spacing: -0.5px;
    color: #007b7b;
    padding: 10px;
}

.box-infos1 p {
    font-family: 'Open Sans Condensed', sans-serif;
    font-weight: bolder;
    font-size: 12px;
    font-stretch: ultra-condensed;
    text-align: center;
    letter-spacing: -0.5px;
    color: #666666;
    padding-top: -5px;
}



.clear2 {
    clear: both;
}

.box-wrap1 {
    margin-right: 30px;
    float: left;
}

.clear3 {
    clear: both;
}


/* pack3 */

.rates-bed2 {
    width: 840px;
    height: 105px;
    font-family: 'Amatic SC', cursive;
    font-weight: bold;
    font-size: 30px;
    background: #00435d;
    border-radius: 5px;
    text-align: left;
    text-transform: uppercase;
    color: #fff;
    letter-spacing: 0.3px;
    line-height: 30px;
    margin-top: 10px;
    margin-bottom: 20px;
    padding-left: 120px;
    padding-top: 20px;
    box-sizing: border-box;
}



#rates {
    margin: 600px auto 0;
    width: 100%;
    max-width: 100%;
    padding: 200px;
    box-sizing: border-box;
    background-color: #addddc;
}

.title-rates2 {
    width: 260px;
    height: 45px;
    background-color: #00435d;
    margin-top: -20px;
    border-radius: 5px;
    box-sizing: border-box;
}

.title-rates2 h1 {
    font-family: 'Amatic SC', cursive;
    font-size: 30px;
    letter-spacing: 1px;
    color: #fff;
    text-align: center;
}

.box-infos2 {
    width: 260px;
    height: 370px;
    background-color: #fff;
    border-radius: 0px;
    margin-top: 20px;
}

.box-infos2 h2 {
    font-family: 'Open Sans Condensed', sans-serif;
    font-weight: normal;
    font-size: 18px;
    font-stretch: ultra-condensed;
    text-align: center;
    letter-spacing: -0.5px;
    color: #00435d;
    padding: 10px;
}

.box-infos2 p {
    font-family: 'Open Sans Condensed', sans-serif;
    font-weight: bolder;
    font-size: 12px;
    font-stretch: ultra-condensed;
    text-align: center;
    letter-spacing: -0.5px;
    color: #666666;
    padding-top: -5px;
}

 


.clear2 {
    clear: both;
}

.box-wrap2 {
    margin-right: 30px;
    float: left;
}

.clear3 {
    clear: both;
}

   
/* REVIEWS */

.box1,
.box2,
.box3 {
    margin-left: 35px;
}

#bubblebox {
    height: 940px;
    overflow: hidden;
    background-color: #53c3c5;
    padding-left: 200px;
    padding-right: 200px;
    /* margin-left: 100px; */
}

#bubblecontent {
    max-width: 940px;
    margin: 0 auto;
}


.box-general {
    width: 240px;
    height: 440px;
    background-color: #00435d;
    margin: 40px;
    margin-top: 100px;
    margin-bottom: 100px;
    margin-right: 0px;
    padding: 10px;
    box-sizing: border-box;
    float: left;
}

.box-reviews {
    width: 230px;
    height: 420px;
    background-color: #00435d;
    margin-right: 5px;
    margin-left: -5px;
    padding: 10px;
    box-sizing: border-box;
    border-right: 1px solid #53c3c5;
    border-left: 1px solid #53c3c5;
}

.vl {
    border-left: 5px solid #fff;
    margin-left: 20px;
    position: absolute;
}

.box-reviews h1 {
    font-size: 70px;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    color: #53c3c5;
    margin-top: -10px;
}

.box-reviews p {
    font-size: 12px;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    letter-spacing: 1.5;
    color: #53c3c5;
    text-align: center;
    padding: 10px;
    margin-top: -60px;
    margin-bottom: 20px;
}

.box-reviews h3 {
    font-size: 12px;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    color: #fff;
    text-align: left;
    margin-left: 10px;
    margin-bottom: 10px;
}

.box-reviews {
    display: block;
    float: left;
}


/* box 1 */

.bottom-box1 {
    margin-top: 50px;
}

.bottom-box2 {
    margin-top: 120px;
}

.bottom-box3 {
    margin-top: 85px;
}


/* box 2 */

.bottom-box1a {
    margin-top: 140px;
}

.bottom-box2a {
    margin-top: 140px;
}

.bottom-box3a {
    margin-top: 140px;
}


/* box 3 */

.bottom-box1b {
    margin-top: 100px;
}

.bottom-box2b {
    margin-top: 100px;
}

.bottom-box3b {
    margin-top: 100px;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="../css/multi-slider.css">
    <title>Document</title>
</head>

<body>

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

        <div class="mySlides1">
            <div class="rates-wrapper">

                <div class="box-wrap">
                    <div class="title-rates">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap">
                    <div class="title-rates">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap">
                    <div class="title-rates">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos">
                        <p>Content</p>
                    </div>
                </div>

                <div class="clear3"></div>

                <div class="season-group">
                    <p class="season">Title</p>
                    <img src="/Users/JahCriss/Local Sites/CASA DA MATA/code/images/waves-long-length.png" class="long-waves">
                </div>

                <div class="clear2"></div>

                <div class="box-wrap">
                    <div class="title-rates">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap">
                    <div class="title-rates">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap">
                    <div class="title-rates">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos">
                        <p>Content</p>
                    </div>
                </div>

                <div class="clear3"></div>

                <div class="season-group">
                    <p class="season">Title</p>
                    <img src="/Users/JahCriss/Local Sites/CASA DA MATA/code/images/waves-long-length.png" class="long-waves">
                </div>

                <div class="clear4"></div>

            </div>
        </div>

        <div class="mySlides1">
            <div class="rates-wrapper1">
                <div class="rates-bed1">Title
                    <img src="/Users/JahCriss/Local Sites/CASA DA MATA/code/images/yoga-icon.png" alt="house">
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="clear3"></div>

                <div class="season-group">
                    <p class="season">Title</p>
                    <img src="/Users/JahCriss/Local Sites/CASA DA MATA/code/images/waves-long-length.png" class="long-waves">
                </div>

                <div class="clear2"></div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>

                    <div class="clear3"></div>

                    <div class="season-group">
                        <p class="season">Title</p>
                        <img src="/Users/JahCriss/Local Sites/CASA DA MATA/code/images/waves-long-length.png" class="long-waves">
                    </div>

                    <div class="clear4"></div>

                </div>
            </div>



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

        <div class="mySlides1">
            <div class="rates-wrapper1">
                <div class="rates-bed1">Title
                    <img src="/Users/JahCriss/Local Sites/CASA DA MATA/code/images/yoga-icon.png" alt="house">
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="clear3"></div>

                <div class="season-group">
                    <p class="season">Title</p>
                    <img src="/Users/JahCriss/Local Sites/CASA DA MATA/code/images/waves-long-length.png" class="long-waves">
                </div>

                <div class="clear2"></div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>
                </div>

                <div class="box-wrap1">
                    <div class="title-rates1">
                        <h1>Title</h1>
                    </div>

                    <div class="box-infos1">
                        <p>Content</p>
                    </div>

                    <div class="clear3"></div>

                    <div class="season-group">
                        <p class="season">Title</p>
                        <img src="/Users/JahCriss/Local Sites/CASA DA MATA/code/images/waves-long-length.png" class="long-waves">
                    </div>

                    <div class="clear4"></div>

                </div>
            </div>



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

        <div class="slideshow-container">

            <div class="mySlides2">
                <div class="box1">
                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper1">
                                <div class="vl"></div>
                                <p>Content</p>

                            </div>
                        </div>
                    </div>

                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper2">
                                <p>Content</p>

                            </div>
                        </div>
                    </div>

                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper3">
                                <p>Content</p>
                            </div>
                        </div>
                    </div>

                    <div class="clear"></div>
                </div>
            </div>

            <div class="mySlides2">
                <div class="box2">
                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper1">
                                <p>Content</p>
                            </div>
                        </div>
                    </div>

                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper2">
                                <p>Content</p>
                            </div>
                        </div>
                    </div>

                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper3">
                                <p>Content</p>
                            </div>
                        </div>
                    </div>

                    <div class="clear"></div>
                </div>
            </div>

            <div class="mySlides2">
                <div class="box3">
                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper1">
                                <p>Content</p>

                            </div>
                        </div>
                    </div>

                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper2">
                                <p>Content</p>
                            </div>
                        </div>
                    </div>

                    <div class="box-general">
                        <div class="box-reviews">
                            <div class="box-wrapper3">
                                <p>Content</p>
                            </div>
                        </div>
                    </div>

                    <div class="clear"></div>
                </div>
            </div>

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

        </div>

</body>

</html>

I'm new on code so maybe my question is more simple that I think, but the case is that:

I want to have two slideshows in a page, only with html content, with NO IMAGES, but the script I'm using is not working.

When I try to check the errors, it says:

"Uncaught TypeError: Cannot read property 'style' of undefined"

Any help would be very welcome!

Thanks in advance!

So Here's the code for the javascript:

var slideIndex = [1, 1];
var slideId = ["mySlides1", "mySlides2"]
showSlides(1, 0);
showSlides(1, 1);

function plusSlides(n, no) {
  showSlides(slideIndex[no] += n, no);
}

function showSlides(n, no) {
  var i;
  var x = document.getElementsByClassName(slideId[no]);
  if (n > x.length) {
    slideIndex[no] = 1
  }
  if (n < 1) {
    slideIndex[no] = x.length
  }
  for (i = 0; i < x.length; i++) {
    x[i].style.display = "none";
  }
  x[slideIndex[no] - 1].style.display = "block";
}

0 个答案:

没有答案