touchend as mouseleave not to activate onClick effects in jQuery

Asked

Viewed 38 times

1

I have a problem regarding the click of touch whether he is still on top of the element or not (in this case the mouseleave) I modified a code already posted on the Problem capturing element position when there is a scroll in jQuery and thanks in 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>
<script src="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 the touch of a mobile to roll the scroll works but it is when loose it goes and activates the effect $this.parent().animate of click on the element and I imagine that the touchend would be like the mouseup, in this case I wanted to know if it is possible to apply a test such as mouseleave but in touch asking if he is on top of the element and if he is applying the effect.

1 answer

1


What you need is a flag to indicate if there was touchmove between the touchstart and the touchend. When I used this type of functionality I remember having implemented also a simple calculator away from the touchmove, once it is sometimes shot falsely.

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...
  • Thank you very much for the example but I noticed a bug in which the scrolling = false; was not inserting and I added it into the if, thus: if (scrolling) { scrolling = false; return; } and it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.