How to scroll to element in a horizontall scroll?

Viewed 56

I have a bootstrap nav which is scrollable horizontally in my website, when i change the nav i would the scroller to follow the selected item so i'm trying to scrollLeft to the left position of the .active nav...

But once the selected nav is out of the scroll width so it's overflowed all breaks and the nav become buggy like this

enter image description here

Here is my code:

const nav = document.querySelector(".menu .nav-scroller .nav");
const tasti = document.querySelector(".tasti");
tasti.addEventListener("touchstart", handleTouchStart, {
  passive: true,
});
tasti.addEventListener("touchmove", handleTouchMove, {
  passive: true
});

var xDown = null;
var yDown = null;

function getTouches(evt) {
  return (
    evt.touches || // browser API
    evt.originalEvent.touches
  ); // jQuery
}

function handleTouchStart(evt) {
  const firstTouch = getTouches(evt)[0];
  xDown = firstTouch.clientX;
  yDown = firstTouch.clientY;
}

function handleTouchMove(evt) {
  if (!xDown || !yDown) {
    return;
  }

  var xUp = evt.touches[0].clientX;
  var yUp = evt.touches[0].clientY;

  var xDiff = xDown - xUp;
  var yDiff = yDown - yUp;

  if (Math.abs(xDiff) > Math.abs(yDiff)) {
    /*most significant*/
    if (xDiff > 0) {
      var active = $(nav).find(".active");
      if (active.is(":last-child")) {
        return;
      }
      var next = active.next();
      $(next).tab("show");
      /* left swipe */
    } else {
      var active = $(nav).find(".active");
      if (active.is(":first-child")) {
        return;
      }
      var next = active.prev();
      $(next).tab("show");
      /* right swipe */
    }
    $(nav).animate({
      scrollLeft: $(nav).find(".active").position().left
    }, 500); // scrolling here
  }
  /* reset values */
  xDown = null;
  yDown = null;
}
.articoli .menu {
  height: 66px;
}

.nav-scroller {
  position: relative;
  top: 0;
  z-index: 2;
  overflow-y: hidden;
}

.nav-scroller .nav {
  overflow-y: hidden;
  overflow-x: auto;
  flex-wrap: nowrap;
}

.nav-link-search {
  min-width: 125px;
}

.nav-link {
  white-space: nowrap;
}

.tasti {
  height: 200px;
  background: #fafafa;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-Piv4xVNRyMGpqkS2by6br4gNJ7DXjqk09RmUpJ8jgGtD7zP9yug3goQfGII0yAns" crossorigin="anonymous"></script>
<div class="menu">
  <nav class="nav-scroller mb-2">
    <div class="nav nav-pills flex-column flex-sm-row" role="tablist"><a class="flex-sm-fill text-sm-center nav-link nav-link-search" data-toggle="pill" href="#cerca" aria-controls="cerca" role="tab" aria-selected="false"><i class="fi-rs-search"></i></a><a class="flex-sm-fill text-sm-center nav-link active" data-toggle="pill"
        href="#tab_1" aria-controls="tab_1" role="tab" aria-selected="true" data-to-trans="true">CAFFETTERIA</a><a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_2" aria-controls="tab_2" role="tab" aria-selected="false" data-to-trans="true">PREFERITI</a>
      <a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_3" aria-controls="tab_3" role="tab" aria-selected="false" data-to-trans="true">RINFRESCHI</a><a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_4"
        aria-controls="tab_4" role="tab" aria-selected="false" data-to-trans="true">PRALINERIA</a><a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_5" aria-controls="tab_5" role="tab" aria-selected="false" data-to-trans="true">CONFEZIONI</a>
      <a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_6" aria-controls="tab_6" role="tab" aria-selected="false" data-to-trans="true">TESSERE</a>
      <a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_7" aria-controls="tab_7" role="tab" aria-selected="false" data-to-trans="true">STAGIONALI</a><a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_8"
        aria-controls="tab_8" role="tab" aria-selected="false" data-to-trans="true">ACCONTO </a><a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_9" aria-controls="tab_9" role="tab" aria-selected="false" data-to-trans="true">INGROSSO</a>
      <a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_10" aria-controls="tab_10" role="tab" aria-selected="false" data-to-trans="true">VINI</a>
      <a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_11" aria-controls="tab_11" role="tab" aria-selected="false" data-to-trans="true">PIZZA</a><a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_12"
        aria-controls="tab_12" role="tab" aria-selected="false" data-to-trans="true">PIZZA</a><a class="flex-sm-fill text-sm-center nav-link" data-toggle="pill" href="#tab_13" aria-controls="tab_13" role="tab" aria-selected="false" data-to-trans="true">PIZZA</a></div>
  </nav>
</div>
<div class="tasti">
  SWIPE HERE</div>

So to select another nav the user actually can just press on a nav link or just swipe on the div belowe it, i've added the code only for the swipe...

0 Answers
Related