1

I'm learning JavaScript and I've got a simple problem that I don't know how to deal with it.

What I want to achieve is to 'hold' or 'freeze' a slide disabling all kind of possible navigation (keyboard arrows, click and drag, touch interaction) by only clicking a button when the user desires. By clicking this button the user enables or disables the navigation controls with the slider.

So, once clicked this button the user can't navigate on the slides. The user has to click again on the button to enable the controls.

This is what I got so far:

jQuery(document).ready(function($) {
  $('.slider').slick({
    infinite: true,
    fade: false,
    centerMode: true,
    variableWidth: true,
    slidesToShow: 3,
    slidesToScroll: 1,
    arrows: false,
    autoplay: false
  });
});

  var hold = false;

  $('#hold').click(function() {
      $(this).attr("class", "active disabled");

      if (hold == false) {
          $('.slider').slick({
            accesibility: false,
            draggable: false,
            swipe: false,
            touchMove: false
          });
          hold = true;

      } else {
        $('.slider').slick({
            accesibility: true,
            draggable: true,
            swipe: true,
            touchMove: true
          });
        hold = false;
        $("#hold").attr("class", "disabled");
      }

  });
.card {
  margin: 10px;
  padding: 50px 100px;
  background-color: silver;
  color: white;
}

.active {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src='//cdn.jsdelivr.net/jquery.slick/1.5.7/slick.min.js' type='text/javascript'></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.9/slick.css" />

<div class="slider">
  <h1 class="card">1</h1>
  <h1 class="card">2</h1>
  <h1 class="card">3</h1>
  <h1 class="card">4</h1>
  <h1 class="card">5</h1>
</div>

<button id="hold" class="disabled">HOLD</button>

What could I've been missing?

Your help is really appreciated!

1 Answer 1

6

Use slickSetOption to set an individual value live. See the Methods section at http://kenwheeler.github.io/slick/

Below is a working example.

var slider = $("#slider");
slider.slick({
  arrows: false,
  centerMode: true,
  infinite: true,
  slidesToShow: 3,
  slidesToScroll: 1,
  variableWidth: true
});

var hold = false;
$("#hold").click(function() {
  slider.slick("slickSetOption", "accessibility", hold);
  slider.slick("slickSetOption", "draggable", hold);
  slider.slick("slickSetOption", "swipe", hold);
  slider.slick("slickSetOption", "touchMove", hold);
  
  hold = !hold;
  
  $(this).toggleClass("disabled");
});
h1 {
  background-color: silver;
  color: white;
  margin: 10px;
  padding: 50px 100px;
}

.disabled {
  color: red;
}
<link href="https://cdn.jsdelivr.net/jquery.slick/1.6.0/slick.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.slick/1.6.0/slick.min.js"></script>
<div id="slider">
  <h1>1</h1>
  <h1>2</h1>
  <h1>3</h1>
  <h1>4</h1>
  <h1>5</h1>
</div>
<button id="hold">HOLD</button>

2
  • The accesibility option still runs, what could it be? Anything else works well, thank you for your answer!
    – ermac
    Commented Oct 27, 2016 at 3:36
  • @Ermac I fixed the example. There was a spelling mistake... oops.
    – coderfin
    Commented Oct 27, 2016 at 4:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.