Problem capturing element position when there is a scroll in jQuery

Asked

Viewed 40 times

0

I’m having a problem when element is on the scroll browser and jQuery does not correctly capture the position, I created a simple Tab-bar and when the size is other past is placed the scroll and at this point, there is an effect below the tab indicating 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>

<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>

When the scroll in the end jQuery cannot capture the position correctly only at the beginning of the scroll.

1 answer

1


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

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

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

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>

<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>

  • Wow I thought the jQuery could capture the position of the element but by its code has to show to the jQuery that there is a scroll and in my case is on the browser screen or in other words the window of scrollLeft().

Browser other questions tagged

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