touchend as mouseleave not to enable onClick effects in jQuery

1

I have a problem with the click of touch if it is still on top of the element or not (in this case mouseleave ) I modified a code already posted on the Problem in capturing the position of the element when there is a scroll in jQuery and thanks in the solution, see the modified code:

$(document).ready(function() {

  var $origin = $('.md-tab>li'),
    $activates = $('.md-tab>.slider'),
    whatTab = $origin.outerWidth(),
    howFar;

  if (!$activates.length) {
    return;
  }

  $activates.css("width", whatTab);

  $origin.each(function(index, element) {
    var $element = $(element);
    $element.find('a').addClass('ripple');
  });

  $origin.on('touchend click', function(e) {
    e.stopPropagation();
    e.preventDefault();

    var $this = $(this);

    whatTab = $this.outerWidth();
    howFar = $this.position().left + $this.parent().scrollLeft();

    $activates.css({
      'left': howFar - $(window).scrollLeft(),
      'width': whatTab
    })
    $this.parent().animate({
      'scrollLeft': howFar - ($this.parent().width() / 2)
    }, 500, 'easeInOutExpo');
  });

});
ul.md-tab {
  position: relative;
  padding: 0;
  white-space: nowrap;
  overflow-x: auto;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -o-user-select: none;
  -ms-box-shadow: 0 2px 5px rgba(0, 0, 0, .16), 0 2px 10px rgba(0, 0, 0, .12);
  -o-box-shadow: 0 2px 5px rgba(0, 0, 0, .16), 0 2px 10px rgba(0, 0, 0, .12);
  box-shadow: 0 2px 5px rgba(0, 0, 0, .16), 0 2px 10px rgba(0, 0, 0, .12);
}
ul.md-tab > li {
  position: relative;
  display: inline-block;
  height: 60px;
}
ul.md-tab > li > a {
  position: relative;
  display: inline-block;
  overflow: hidden;
  text-decoration: inherit;
  text-transform: uppercase;
  text-align: center;
  letter-spacing: .01em;
  font-weight: 500;
  height: 60px;
  line-height: 61px;
  margin-bottom: -8px;
  padding-left: 1.3em;
  padding-right: 1.3em;
}
ul.md-tab > li.slider {
  position: absolute;
  display: block;
  bottom: 0;
  left: 0;
  height: 4px;
  pointer-events: none;
  background-color: #ffeb3b;
  -webkit-transition: all 0.5s;
  -moz-transition: all 0.5s;
  -ms-transition: all 0.5s;
  -o-transition: all 0.5s;
  transition: all 0.5s;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="http://gsgd.co.uk/sandbox/jquery/easing/jquery.easing.1.3.mine.js"></script>

<nav class="nav" id="nav" data-color="cyan">
  <ul class="md-tab">
    <li><a href="#!">Início</a>
    </li>
    <li><a href="#!">Card</a>
    </li>
    <li><a href="#!">Typography</a>
    </li>
    <li><a href="#!">Tables</a>
    </li>
    <li><a href="#!">Form</a>
    </li>
    <li><a href="#!">User Interface</a>
    </li>
    <li class="slider"></li>
  </ul>
</nav>
The problem is this: when I use touch of a mobile to scroll scroll it works but it is loose when it goes and it activates the% effect of the element click > and I imagine that $this.parent().animate would be like touchend , in this case I wanted to know if it is possible to apply a test as mouseup but in mouseleave asking if it is on top of the element and if the effect is applied. / p>     
asked by anonymous 18.09.2016 / 06:52

1 answer

1

What you need is a flag that indicates whether there was touchmove between touchstart and touchend . When I used this kind of functionality I remember that I also implemented a simple calculator of distance from touchmove , since it is sometimes fired.

Test this simple version:

var scrolling = false;
$('#nav').on('touchmove', function(){
    scrolling = true;
});

$origin.on('touchend click', function(e) {
    e.stopPropagation();
    e.preventDefault();
    if (scrolling) return;
    scrolling = false;

    var $this = $(this);
    // etc...
    
18.09.2016 / 09:27