1

I've built a carousel for mobile devices, where you can drag a card and when you release it, it goes behind the others (see snippet), using jquery-ui draggable.

I works fine when I drag on the left or on the right, but here is the problem: when a user want to scroll down or up, the draggable is triggered and he can't scroll down or up.

How can I solve it? I tried with the option "axis: 'x'" but it doesn't work because the user will never scroll "perfectly" vertical.

Maybe detecting the angle of the touch event?

Thanks in advance

var $draggable = $('.card').draggable({
        revert: true,
        revertDuration: 0,
        axis: 'x'
      });

 $draggable.on("dragstop", function(event, ui) {
     var self = $(this);
     var container = self.closest('.container');
     
     container.append(self);
     self.removeAttr('style');
  });
.container {
  position: relative;
  max-width: 350px;
  padding-top: 60%;
  margin: 100px auto; 
}

.card {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: center;
  transition: top 0.3s;
  transition: left 0.3s;
  width: 250px;
  height: 300px;
  border-radius: 5px;
}

.card1 {
  background: rgb(216, 120, 47);
}
.card2 {
  background: rgb(230, 249, 54);
}
.card3 {
  background: rgb(73, 249, 54);
}


.card:nth-child(1) {
  z-index: 10;
  top: 0px;
  transform-origin: top;
  transform: scale(1);
  opacity: 1;
  transition: all 0.3s;
}

.card:nth-child(2) {
  z-index: 9;
  top: -15px !important;
  transform-origin: top;
  transform: scale(0.9);
  opacity: 0.9;
  transition: all 0.3s;
}

.card:nth-child(3) {
  z-index: 8;
  top: -30px !important;
  transform-origin: top;
  transform: scale(0.8);
  opacity: 0.8;
  transition: all 0.3s;s
}

.card:nth-of-type(1n+4) {
  z-index: 7;
  top: -45px;
  transform-origin: top;
  -webkit-transform: scale(0.7);
  transform: scale(0.7);
  opacity: 0;
}


li {
  display: inline-block;
}
<body>
    <ul class="container">

      <li class="card card1"></li>
      <li class="card card2"></li>
      <li class="card card3"></li>
    </ul>
  

  <script src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
  <script src="main.js"></script>
  </body>
Matteo
  • 53
  • 1
  • 5
  • The $.ui library traditionally doesn't work great on mobile, it's commonly used with an addition (there are several) specifically for this. Related question: http://stackoverflow.com/questions/3026915/how-can-i-make-a-jquery-ui-draggable-div-draggable-for-touchscreen – blgt Feb 27 '17 at 11:01
  • actually my goal is to do like Slick: http://kenwheeler.github.io/slick/... if I drag horizontally it works, if I drag vertically it scrolls down or up. – Matteo Feb 27 '17 at 14:40
  • I suspect you need to also include the `containment: "parent"` option with parent or another element for your draggable. – Twisty Feb 27 '17 at 16:23

0 Answers0