如何在单击时替换8个图像的部分src?

oxiaedzo  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(317)

我有一个带有8个图像的滑块,其源代码中有一个公用文件夹( img/banana/#.png , img/strawberry/#.pngimg/apple/#.png )每次单击滑块外的另一个图像时,我都需要进行更改;它还有一个文件夹,上面有一个水果名(香蕉、草莓或苹果)。要增加一个更高的难度,我需要的图像,作为一个不断变化的滑块图像工程,也改变了它的一部分 src 因为它改变了。
因此,如果我单击的更改图像具有src img/strawberry/1.png 并更改已创建的8个图像的src img/banana/#.png 作为src,它必须由 img/banana/1.png 8张图片中有 img/banana/#.png 必须换成 img/strawberry/#.png . 问题是,我总是会有两个不断变化的图像,我需要检测滑块的8个图像的来源,以便它们可以被单击的一个不断变化的图像替换。
这是滑块有8个香蕉图像时的代码。

<div class="row">
    <div class="col">
        <div class="swiper-container">
            <div class="swiper-wrapper">
                <div class="swiper-slide">
                    <img src="img/banana/1.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/banana/2.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/banana/3.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/banana/4.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/banana/5.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/banana/6.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/banana/7.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/banana/8.png" alt="">
                </div>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-4">
        <img src="img/frutilla/1.png" alt="">
    </div>
    <div class="col-4">
        <img src="img/apple/1.png" alt="">
    </div>
</div>

这是我想要得到的结果,如果我点击两个变化的图像中的一个。

<div class="row">
    <div class="col">
        <div class="swiper-container">
            <div class="swiper-wrapper">
                <div class="swiper-slide">
                    <img src="img/strawberry/1.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/strawberry/2.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/strawberry/3.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/strawberry/4.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/strawberry/5.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/strawberry/6.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/strawberry/7.png" alt="">
                </div>
                <div class="swiper-slide">
                    <img src="img/strawberry/8.png" alt="">
                </div>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-4">
        <img src="img/banana/1.png" alt="">
    </div>
    <div class="col-4">
        <img src="img/apple/1.png" alt="">
    </div>
</div>

如您所见,图像上的数字始终保留,只需更改文件夹的名称(草莓、香蕉或苹果)。我在图中留下了一个例子供参考,以便更好地理解。

顺便说一句,我使用的滑块是swiperjs。

vdzxcuhz

vdzxcuhz1#

试图重新创建您的项目,因此这里有一个可能会对您有所帮助的示例!单击swiper下的小图像,根据之前单击的图像更改swiper图像url。
小提琴演示
html

<head>
  <link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css" />
  <script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
</head>

<!-- https://i.imgur.com/8nKVc0s.jpg ## strawberry-->
<!-- https://i.imgur.com/GBDX2ps.jpg ## banana-->

<body>
  <div class="swiper-container mySwiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
      <div class="swiper-slide"><img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'></div>
    </div>
    <div class="swiper-pagination"></div>
  </div>

  <div class='selector'>
    <img data-type="strawberry" src='https://i.imgur.com/8nKVc0s.jpg'>
    <img data-type="banana" src='https://i.imgur.com/GBDX2ps.jpg'>
  </div>
</body>

jquery

$().ready(() => {
    var swiper = new Swiper(".mySwiper", {
    pagination: {
        el: ".swiper-pagination",
    },
    });
    $(document).ready(function() {
        $('.selector img').click(function() {
            var iType = $(this).data("type") 
          //I don't have strawberry and banana in URL so in your case you can use the following to get the type
          /*
            var iType = getTypeFromURL($(this).attr("src"))
          */

          //Now you go through the images to check their type (or in your case, the URL)
          var curr_selection = $(".swiper-container .swiper-wrapper .swiper-slide img").data("type")
              if (curr_selection != iType) 
          {
            //if the clicked type AND the current images are not fit (by type) then let's change to swiper img urls
            var selectedURL = $(this).attr("src");
            $(".swiper-container .swiper-wrapper .swiper-slide img").each(function(){
                $(this).attr("src", selectedURL).data("type", iType) //you don't need the .data()
            })
          }
        });
    });
})

//In order not to write the logic again and again here is a function that will tell you the type from the URL you give as parameter
function getTypeFromURL(url)
{
    if (url.contains("strawberry")) return "strawberry";
  else if (url.contains("banana")) return "banana";
}

相关问题