简体   繁体   English

基于水平滚动隐藏和显示导航箭头 position 纯 javascript

[英]Hiding and Showing navigation arrows based on horizontal scroll position pure javascript

I am using horizontal scroll with left and right navigation button.我正在使用带有左右导航按钮的水平滚动。 While the scroll is in initial state the left arrow shouldn't be visible.当滚动处于初始 state 时,左箭头不应该可见。 On clikicng right arrow when it moves half the way then the left arrow should appear.当右箭头移动到一半时,它会出现左箭头。

Similary when its reaches the right end the right arrow should diaappear.同样,当它到达右端时,右箭头应该会出现。

 function move_right() { var element = document.getElementById('scrollmenu'); // x = element.clientWidth; x="50"; element.scrollBy({ top: 0, left: x, behavior: 'smooth' }); } function move_left() { var element = document.getElementById('scrollmenu'); // x = element.clientWidth; x="50"; element.scrollBy({ top: 0, left: -x, behavior: 'smooth' }); }

How to detect the scroll and hide left & right arrows.如何检测滚动并隐藏左右箭头。

document.getElementById('scrollmenu').addEventListener("scroll", function (e) {
        horizontal = e.currentTarget.scrollLeft;
        scrollWidth = e.currentTarger.scrollWidth;
        if(horizontal < (scrollWidth/2)) {
           // add `display: none` to left button
           // add `display: inline-block` to right button
         } else {
          // add `display: inline-block` to left
          // add `display: none` to right button
         }
        });

From what I could understand from the description you gave, this should work.从您给出的描述中我可以理解,这应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM