Problem in capturing the position of the element when there is a scroll in jQuery

0

I have a problem when element is over scroll of the browser and jQuery does not properly capture the position, I created a simple Tab-bar and when the size is utra passed the scroll is placed and at the moment, there is an effect below the tab indicating the navigation, see the 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() {
    $(this).find('a').addClass('ripple');
  });

  origin.click(function() {
    whatTab = $(this).outerWidth();
    howFar = $(this).position().left;

    activates.css({
      'left': howFar,
      'width': whatTab
    });
  });

});
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/1.11.1/jquery.min.js"></script><navclass="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>

When the scroll rolls at the end, jQuery can not capture the position correctly at the beginning of scroll .

    
asked by anonymous 01.09.2016 / 20:27

1 answer

1

In addition to the current position, you need to add the parent's scrollLeft in the parent's UL case.

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

If the problem is occurring with the scroll of the screen itself, add the window scroll:

howFar = $(this).position().left + $(window).scrollLeft();

$(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() {
    $(this).find('a').addClass('ripple');
  });

  origin.click(function() {
    whatTab = $(this).outerWidth();
    howFar = $(this).position().left + $(this).parent().scrollLeft();
    activates.css({
      'left': howFar,
      'width': whatTab
    });
  });

});
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/1.11.1/jquery.min.js"></script><navclass="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>
    
04.09.2016 / 14:29