gpt4 book ai didi

javascript - 如何正确指向 slider #

转载 作者:行者123 更新时间:2023-11-28 00:36:12 25 4
gpt4 key购买 nike

我在左侧有这个菜单,有 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,但也做错了。

最佳答案

因此,我最终稍微接触了 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>

关于javascript - 如何正确指向 slider #,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56073344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com