How to Make Scrollbar Custom Arrows Work On Mobile Devices

Viewed 37

I want to show horizontal arrow buttons that disappear when the container is not scrollable. I used the answer here. It works on PC but not on mobile.

How do I make it scroll on mobile devices when clicked?

My needs are:

  1. I want it to scroll on mobile when clicking the arrows.
  2. I want the arrow button to auto show when the overflow is active.

// Show gradient and left/right arrows only if scrollable
$(".scroll-area").each((i, el) => {
  $(el).parent().find(".right")[el.scrollWidth > el.clientWidth ? "show" : "hide"]();
});

// Show/hide gradient and arrows on scroll
$('.scroll-area').scroll((e) => {
  const fullWidth = $(e.target)[0].scrollWidth - $(e.target)[0].offsetWidth - 1;
  const left = $(e.target).scrollLeft()

  $(e.target).parent().find(".left, .left-arrow")[left > 0 ? "show" : "hide"]();
  $(e.target).parent().find(".right, .right-arrow")[left < fullWidth ? "show" : "hide"]();
});

// Scroll on left/right arrow mouse down
let intervalId;
$(".left-arrow, .right-arrow").on("mousedown", (e) => {
  const scroll = $(e.target).closest(".sc-head-menu-wrap").find(".scroll-area");
  intervalId = setInterval(() => {
    const left = scroll.scrollLeft();
    scroll.scrollLeft(e.target.classList.contains("left-arrow") ? left - 10 : left + 10);
  }, 50);
}).on("mouseup mouseleave", () => {
  clearInterval(intervalId);
});
.sc-head {
  position: relative;
  left: 0;
  width: 100%;
}

@media (min-width: 768px) {
  .sc-head-wrap {
    height: 58px !important;
  }
}

.sc-head-wrap {
  background-color: white;
  z-index: 499;
  height: 48px;
  overflow: hidden;
  transition: height 0.2s ease-in-out;
  border-bottom-width: 1px;
  border-bottom-style: solid;
  border-bottom-color: #dcdbdb;
}

@media (min-width: 990px) {
  .sc-head-inner {
    max-width: 1100px !important;
    width: calc(100vw - 90px) !important;
  }
}

@media (min-width: 768px) {
  .sc-head-inner {
    max-width: 989px !important;
    width: calc(100vw - 110px) !important;
  }
}

.sc-head-inner {
  margin-left: auto;
  margin-right: auto;
  max-width: 550px;
  width: calc(100vw - 40px);
}

@media (min-width: 1190px) {
  .sc-head-nav {
    max-width: 1100px !important;
  }
}

@media (min-width: 990px) {
  .sc-head-nav {
    max-width: 990px !important;
  }
}

@media (min-width: 768px) {
  .sc-head-nav {
    padding-top: 16px !important;
    padding-bottom: 16px !important;
  }
}

.sc-head-nav {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  padding-top: 11px;
  padding-bottom: 11px;
  width: 100%;
}

@media (min-width: 768px) {
  .sc-head-title {
    height: 26px !important;
  }
}

.sc-head-title {
  padding-right: 12px;
  border-right-width: 1px;
  border-right-style: solid;
  border-right-color: #dcdbdb;
  display: flex;
  align-items: center;
  flex: 0 0 auto;
  height: 18px;
}

.css-1xsuv1n {
  cursor: pointer;
  text-decoration: none;
  border-color: currentColor;
  color: #02838d;
  line-height: 1;
}

@media (min-width: 768px) {
  .css-ads168 {
    line-height: 22px !important;
  }
}

.css-ads168 {
  font-family: "Proxima Nova Condensed", "Proxima Nova Condensed Fallback", sans-serif;
  font-weight: 700;
  text-transform: uppercase;
  font-size: 16px;
  line-height: 18px;
  margin: 0;
  display: inline-block;
  color: #01ADB9;
}

.sc-head-menu-wrap {
  display: flex;
  flex-direction: row;
  align-items: center;
  flex: 1;
  overflow: hidden;
  position: relative;
}

@media (min-width: 768px) .css-eid8cj {
  height: 26px !important;
}

.css-eid8cj {
  min-width: 28px;
  position: relative;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sc-head-menu-inner {
  flex: 1;
  overflow-x: auto;
}

.sc-head-menu-inner::-webkit-scrollbar {
  display: none;
}

.scroll-area {
  overflow-x: auto;
  height: 100%;
  -ms-overflow-style: none;
  -webkit-scrollbar-width: none;
  -moz-scrollbar-width: none;
  scrollbar-width: none;
}

.scroll-area::-webkit-scrollbar {
  display: none;
}

.left {
  display: none;
}

.left-arrow {
  display: none;
}

@media (min-width: 768px) {
  .menu-arrow-left {
    height: 18px !important;
    width: 18px !important;
  }
}

.menu-arrow-left {
  position: absolute;
  height: 15px;
  border-style: none;
  width: 15px;
  padding: 0;
  background-color: white;
  cursor: pointer;
  left: 0;
}

@media (min-width: 768px) {
  .menu-arrow-right {
    height: 18px !important;
    width: 18px !important;
  }
}

.menu-arrow-right {
  position: absolute;
  height: 15px;
  border-style: none;
  width: 15px;
  padding: 0;
  background-color: white;
  cursor: pointer;
  right: 0;
}

@media (min-width: 768px) {
  .sc-head-scroll {
    height: 26px !important;
  }
}

.sc-head-scroll {
  min-width: 28px;
  position: relative;
  height: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}

@media (min-width: 768px) {
  .sc-head-menu-inner ul.menu {
    min-height: 26px !important;
  }
}

.sc-head-menu-inner ul.menu {
  display: flex;
  flex: 1;
  flex-direction: row;
  flex-wrap: nowrap;
  list-style: none;
  margin: 0;
  min-height: 18px;
  padding: 0;
}

.sc-head-menu-inner .menu-item {
  font-family: "Proxima Nova Condensed", "Proxima Nova Condensed Fallback", sans-serif;
  font-weight: 500 !important;
  display: flex !important;
  flex-wrap: nowrap !important;
  font-size: 16px !important;
  line-height: 26px !important;
  margin-bottom: 0;
  margin-right: 25px;
  position: relative;
  white-space: nowrap;
}

.sc-head-menu-inner .menu-item a {
  color: black;
  transition: color 0.2s;
  cursor: pointer;
  text-decoration: none;
  border-color: currentColor;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="sc-head-wrap">
  <div class="sc-head-inner">
    <nav class="sc-head-nav">
      <div class="sc-head-title">
        <a class="css-1xsuv1n" href="/">
          <h4 class="css-ads168">HOME</h4>
        </a>
      </div>
      <div class="sc-head-menu-wrap">
        <div class="sc-head-scroll"><button class="menu-arrow-left left"><svg class="left-arrow" xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 19 19"><path fill="#231F20" fill-rule="evenodd" d="M14.95 2.222 13.729 1l-.611.611L5.339 9.39l-.611.61.611.611 7.778 7.779.61.61 1.223-1.222-.611-.611L7.173 10l7.777-7.778z"></path></svg></button></div>
        <div class="sc-head-menu-inner scroll-area">
          <ul id="menu-main-menu-1" class="menu clearfix">
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
            <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-215"><a href="#">Education</a></li>
          </ul>
        </div>
        <div class="sc-head-scroll">
          <button class="menu-arrow-right right" style="display: none;">
            <svg class="right-arrow" xmlns="http://www.w3.org/2000/svg" width="19" height="19" viewBox="0 0 19 19">
              <path fill="#231F20" fill-rule="evenodd" d="M4.05 1.222 5.271.001l.611.611 7.779 7.777.611.61-.611.612-7.778 7.778-.61.61-1.223-1.221.611-.611L11.827 9 4.05 1.222z"></path>
            </svg>
          </button>
        </div>
      </div>
    </nav>
  </div>
</div>

0 Answers
Related