Search code examples
htmljquerycssslidersplidejs

Click event not working after slider changes


The actual task was - on click image/div in slider

  1. (Slider) image margin change
  2. (Slider) image top text opacity change

At start(when page loaded, for first slider) the below code works fine as I intended. But when I drag or click next/previous button on click event, text opacity working but image margin not changes. Even after drag/click next/previous button the initial slider(1st & 2nd slider in example) work fine, but its next sliders (3rd & 4th in example) not. I have tried multiple way like on click - add class, remove class, toggle class, adding css using jquery. Below code example used splide. I also tried with slider from bootstrap, but same output.

I don't know if I made things confusing, I'm putting full example code below for better understanding.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Slider</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@splidejs/splide@4.1.2/dist/css/splide.min.css">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
    <style>

        .slide-margin{
            position: absolute;
            z-index: 0;
            transition: all 0.5s ease-in;
        }
        .slide-margin-left{
            margin-left: 500px;
            z-index: 15;
        }

    </style>
</head>
<body>
<div class="">
    <div class="splide" role="group">
        <div class="splide__track">
              <ul class="splide__list">
                  <li class="splide__slide mx-2" style="min-height: 800px;">
                    <div class="position-relative slider-click" data-id="1">
                        <img src="slider4.jpg" class="d-block w-100 slide-margin" alt="..." id="slider1">
                        <div  class="position-absolute" style="top: 40vh;">
                            <div style="width: 500px; opacity: 0;" class="p-4 slider-text1">
                                <h3>Tower C</h3>
                                <h5>2020</h5>
                                <h3>PROJECT INFO</h3>
                                <p>Competition winning project. ZHA will build the ambitious development of interlinked towers in the heart of a buzzing district in Shenzhen, China. Tower C will include several facilities, such as conference and exhibition venues, cultural hotspots, while incorporating residential developments and transportation hubs.</p>
                                <h4>CLIENT</h4>
                                <p>Zaha Hadid Architects</p>
                            </div>
                        </div>
                    </div>
                  </li>
                  <li class="splide__slide mx-2" style="min-height: 800px;">
                    <div class="position-relative slider-click" data-id="2">
                        <img src="slider5.jpg" class="d-block w-100 slide-margin" alt="..." id="slider2">
                        <div  class="position-absolute" style="top: 40vh;">
                            <div style="width: 500px; opacity: 0;" class="p-4 slider-text2">
                                <h3>Tower C</h3>
                                <h5>2020</h5>
                                <h3>PROJECT INFO</h3>
                                <p>Competition winning project. ZHA will build the ambitious development of interlinked towers in the heart of a buzzing district in Shenzhen, China. Tower C will include several facilities, such as conference and exhibition venues, cultural hotspots, while incorporating residential developments and transportation hubs.</p>
                                <h4>CLIENT</h4>
                                <p>Zaha Hadid Architects</p>
                            </div>
                        </div>
                    </div>
                  </li>
                  <li class="splide__slide mx-2" style="min-height: 800px;">
                    <div class="position-relative slider-click" data-id="3">
                        <img src="slider6.jpg" class="d-block w-100 slide-margin" alt="..." id="slider3">
                        <div  class="position-absolute" style="top: 40vh;">
                            <div style="width: 500px; opacity: 0;" class="p-4 slider-text3">
                                <h3>Tower C</h3>
                                <h5>2020</h5>
                                <h3>PROJECT INFO</h3>
                                <p>Competition winning project. ZHA will build the ambitious development of interlinked towers in the heart of a buzzing district in Shenzhen, China. Tower C will include several facilities, such as conference and exhibition venues, cultural hotspots, while incorporating residential developments and transportation hubs.</p>
                                <h4>CLIENT</h4>
                                <p>Zaha Hadid Architects</p>
                            </div>
                        </div>
                    </div>
                  </li>
                  <li class="splide__slide mx-2" style="min-height: 800px;">
                    <div class="position-relative slider-click" data-id="4">
                        <img src="slider3.jpg" class="d-block w-100 slide-margin" alt="..." id="slider4">
                        <div  class="position-absolute" style="top: 40vh;">
                            <div style="width: 500px; opacity: 0;" class="p-4 slider-text4">
                                <h3>Tower C</h3>
                                <h5>2020</h5>
                                <h3>PROJECT INFO</h3>
                                <p>Competition winning project. ZHA will build the ambitious development of interlinked towers in the heart of a buzzing district in Shenzhen, China. Tower C will include several facilities, such as conference and exhibition venues, cultural hotspots, while incorporating residential developments and transportation hubs.</p>
                                <h4>CLIENT</h4>
                                <p>Zaha Hadid Architects</p>
                            </div>
                        </div>
                    </div>
                  </li>
              </ul>
        </div>
      </div>
</div>



    <script src="https://code.jquery.com/jquery-3.6.1.min.js" integrity="sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/@splidejs/splide@4.1.2/dist/js/splide.min.js"></script>
    <script>
        $(function(){
            var i = 1
            $(document).on('click', '.slider-click', function(){
                var id = $(this).data("id");
                $('#slider'+id).toggleClass('slide-margin-left');
                if(i == 1){
                    $(".slider-text"+id).css({"opacity": "100", "transition": "all 1s ease-in"});
                    i = 0;
                }else{
                    $(".slider-text"+id).css({"opacity": "0", "transition": "all 0.25s ease-in"});
                    i = 1;
                }
                // console.log(i+" - "+id);
            });
            
        });
    </script>
    <script>
        var splide = new Splide( '.splide',{
            type   : 'loop',
            padding: '15rem'
        });
        splide.mount();
    </script>
</body>
</html>

Greatly appreciate any help or hints. Thank you


Solution

  • Just made minor changes

    <script>
        jQuery(function($){
            $('.slider-click').on('click', function(){
                let id = $(this).attr('data-id');
                if($(this).children('img').hasClass('slide-margin-left')){
                    $(this).children('img').removeClass('slide-margin-left');
                    $(".slider-text"+id).css({"transition": "all 0.25s ease-in","opacity": "0"});
                }else{
                    $(this).children('img').addClass('slide-margin-left');
                    $(".slider-text"+id).css({"transition": "all 1s ease-in","opacity": "100"});
                }
            })
    
        })
    </script>