如何正确指向滑块#

时间:2019-05-10 08:12:13

标签: javascript html slider

我在左侧有8个选择的菜单,全部和7个不同的班级名称。在右边,我有8张幻灯片。左侧的8个选项中的每个选项都应更改为右侧的相关幻灯片。 This is how it looks like

这是我的代码:

var slideIndex = 0;
showSlides(slideIndex);

function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {

  var slides = document.getElementsByClassName("mySlides");
  slideIndex = n

  slides[slideIndex].style.display = "block";
}
<div style="display:flex;">
  <div style="flex:1;padding-right:5px;padding-left:40px;"><img src="https://lh3.googleusercontent.com/DsmSNoqWSSXzEve4aAy3vxlHdCWLqZjLIbC_rhNY7xSU9HHqsBmDCX1QiveQ1Y18B1dVqe4PTSkhLjfMa_sL=s1600" usemap="#image-map" class="fr-fil fr-dib">
    <map name="image-map">
			<area target="" alt="Finance" title="Finance" href="#slide1" coords="245,5,348,123" shape="rect">
			<area target="" alt="Customer Care" title="Customer Care" href="#slide2" coords="99,47,212,131" shape="rect">
			<area target="" alt="People and Culture" title="People and Culture" href="#slide3" coords="13,170,123,298" shape="rect">
</map>
  </div>


  <div class="slideshow-container" style="flex:1;padding-left:5px;">
    <div class="mySlides fade" id="slide1" style="width: default;"><img src="https://lh3.googleusercontent.com/6OiY01hC8ZkojpDQy3GFq447c1d31VIEG2Ur36CrazhJgRUAbe6u0XX_DCqXp94CP1xfCS-rDhVtqAJhEg55=s1600" class="mySlides fr-fic fr-dii"></div>
    <div class="mySlides fade" id="slide2" style="width: default;"><img src="https://lh3.googleusercontent.com/agOhlVPvTSSFbpb2Gs2W1xQmqoEPLxbxrAtvtkcbQSACL-6hDsAzZsvZ3iEy3Iu9h-Te4belDEDzSP-ANQ=s1600" class="mySlides fr-fic fr-dii"></div>
    <div class="mySlides fade" id="slide3" style="width: default;"><img src="https://lh3.googleusercontent.com/x6JNN51TlsKNnJuOmOvGg41rx3JJkovcAZN2T2CKjAQU8nJe1hQPe_SXk8HyqPAI3w4_I-25pkdZNRJ3cak=s1600" class="mySlides fr-fic fr-dii"></div>

  </div>

我在这里做错了什么?我已经尝试过使用onclick和一些javascript,但是也做错了。

1 个答案:

答案 0 :(得分:1)

所以我最终介入了JavaScript。 html中唯一真正的更改是为JS添加了事件处理程序,并删除了<\ img>标记中对mySlides类的第二个引用。

无论如何,代码在这里

<div style="display:flex;">
    <div style="flex:1;padding-right:5px;padding-left:40px;"><img src="https://lh3.googleusercontent.com/DsmSNoqWSSXzEve4aAy3vxlHdCWLqZjLIbC_rhNY7xSU9HHqsBmDCX1QiveQ1Y18B1dVqe4PTSkhLjfMa_sL=s1600" usemap="#image-map" class="fr-fil fr-dib">
        <map name="image-map">
            <area target="" alt="Finance" title="Finance" href="#slide1" coords="245,5,348,123" shape="rect" onclick="showSlides(1);">
            <area target="" alt="Customer Care" title="Customer Care" href="#slide2" coords="99,47,212,131" shape="rect" onclick="showSlides(2);">
            <area target="" alt="People and Culture" title="People and Culture" href="#slide3" coords="13,170,123,298" shape="rect" onclick="showSlides(3);">
        </map>
</div>


<div class="slideshow-container" style="flex:1;padding-left:5px;">
        <div class="mySlides fade" id="slide1" style="width: default; display: none"><img src="https://lh3.googleusercontent.com/6OiY01hC8ZkojpDQy3GFq447c1d31VIEG2Ur36CrazhJgRUAbe6u0XX_DCqXp94CP1xfCS-rDhVtqAJhEg55=s1600" class="fr-fic fr-dii"></div>
        <div class="mySlides fade" id="slide2" style="width: default; display: none"><img src="https://lh3.googleusercontent.com/agOhlVPvTSSFbpb2Gs2W1xQmqoEPLxbxrAtvtkcbQSACL-6hDsAzZsvZ3iEy3Iu9h-Te4belDEDzSP-ANQ=s1600" class="fr-fic fr-dii"></div>
        <div class="mySlides fade" id="slide3" style="width: default; display: none"><img src="https://lh3.googleusercontent.com/x6JNN51TlsKNnJuOmOvGg41rx3JJkovcAZN2T2CKjAQU8nJe1hQPe_SXk8HyqPAI3w4_I-25pkdZNRJ3cak=s1600" class="fr-fic fr-dii"></div>
</div>

<script>
    var slideIndex = -1;

    function currentSlide(n)
    {
        showSlides(slideIndex = n);
    }

    function showSlides(n)
    {
        var slides = document.getElementsByClassName("mySlides");
        var x;

        if (slideIndex == n - 1){
            slideIndex = -1;
        }else{
            slideIndex = n - 1
        }

        for (x = 0; x < slides.length; x++){
            if (slideIndex == x){
                slides[slideIndex].style.display = "block";
            }else{
                slides[x].style.display = "none";
            }
        }

    }

</script>