Search code examples
javascriptjqueryhtmlhref

How to change href of img on slide change


Im using the slick slider slideshow. Im trying to make the href of a logo image change when the slides advance. Ive tried two different scripts but none seem to work.

<main class="Site-content">
<div class="main slider" id="wrapper">
    <div><img src="#" alt="slider image"></div>
    <div><img src="#" alt="slider image"></div>
    <div><img src="#" alt="slider image"></div>
    <div><img src="#" alt="slider image"></div>
    </div>


<audio class='player slider' src='audio/06. Family Feud.mp3' controls> </audio>



<div class="music slider" id="titles">

  <div><p>#</p>
  </div>

  <div><p>#</p>
  </div>

  <div><p>#</p>
  </div>

  <div><p>#</p>
  </div>

  </div>

</main>



<a href="#" id="newlink">
<picture>
<source media="(min-width: 650px)" srcset="images/cloud-logo-small.png">
<source media="(min-width: 465px)" srcset="images/type-logo-big.png">
<img src="images/type-logo-big.png" style="width:auto;">
</picture>
</a>

the slideshows within the main tag are synced together


  <script>
    $(document).ready(function(){
    $('.main').slick({
        dots: true,
        arrows: false,
        autoplay: false,
        centerMode: true,
        asNavFor: '.music',
        slidesToShow: 3,
  });
  });
  </script>

<script>
  $(document).ready(function(){
    $('.music').slick({
        asNavFor:'.main',
        mobileFirst: true
  });
  });
  </script>

<script>
$(document).ready(function(){
$('.music').on('beforeChange', playlist);

   function playlist(e, s, current, next) {

     var list = [
      'audio.mp3',
      'audio.mp3',
      'audio.mp3',
      'audio.mp3',

     ];

     $('.player')[0].src = list[next];
     $('.player')[0].load();
     $('.player')[0].play();

   }
 });
</script>


<script>
$(document).ready(function(){
  $("#newlink").on('beforeChange'){
    function links(e, s, current, next) {
      var lnk = [
        'https://youtube.com',
        'https://google.com',
      ];
      $('.blah')[0].href = lnk[next];
    }
  });
</script>

the other script ive used involved

$('.your-element').on('swipe', function(event, slick, direction){
    console.log(direction);     
});

I expect the href of the logo image to change slide by slide. Each slide needs a unique href.


Solution

  • If I understand correctly, your 'slick events' aren't firing. And I see why.

    You should reorder events binding by putting them before calling slick on the element. Let me know if I was right.

    Had the same problem and solved it via the mentioned method. That's a demand from the documentation of the slick-carousel