Responsive Horizontal Scrolling Menu

后端 未结 4 1941
走了就别回头了
走了就别回头了 2021-01-14 15:50

http://healthunit.com has a clean horizontal scrolling menu at the top of the screen once you view it from a mobile phone device. I\'m trying to mimic that same exact functi

4条回答
  •  佛祖请我去吃肉
    2021-01-14 16:36

    Now that the healthunit site has changed the original question is not completely clear.

    To make a nav menu that scrolls horizontally uses arrow buttons (instead of scrollbar) can be implemented with a little jQuery and easily converted to pure JavaScript.

    var $bar = $('.nav');
    var $container = $('#outer');
    var widths = {};
    var scrollOffset = 0;
    
    var container = document.getElementById("outer");
    var bar = document.getElementById("bar");
    
    function setMetrics() {
        metrics = {
            bar: bar.scrollWidth||0,
            container: container.clientWidth||0,
            left: parseInt(bar.offsetLeft),
            getHidden() {
                return (this.bar+this.left)-this.container
            }
        }
    
        updateArrows();
    }
    
    function doSlide(direction){
        setMetrics();
        var pos = metrics.left;
        if (direction==="right") {
            amountToScroll = -(Math.abs(pos) + Math.min(metrics.getHidden(), metrics.container));
        }
        else {
            amountToScroll = Math.min(0, (metrics.container + pos));
        }
        $bar.css("left", amountToScroll);
        setTimeout(function(){
            setMetrics();
        },400)
    }
    
    function updateArrows() {
        if (metrics.getHidden() === 0) {
            $(".toggleRight").addClass("text-light");
        }
        else {
            $(".toggleRight").removeClass("text-light");
        }
    
        if (metrics.left === 0) {
            $(".toggleLeft").addClass("text-light");
        }
        else {
            $(".toggleLeft").removeClass("text-light");
        }
    }
    
    function adjust(){
        $bar.css("left", 0);
        setMetrics();
    }
    
    $(".toggleRight").click(function(){
        doSlide("right");
    });
    
    $(".toggleLeft").click(function(){
        doSlide("left");
    });
    
    $(window).on("resize",function(){
        // reset to left pos 0 on window resize
        adjust();
    });
    
    setMetrics();
    

    Demo: https://www.codeply.com/go/HgAVBVfQFY

提交回复
热议问题