Responsive Horizontal Scrolling Menu

后端 未结 4 1902
走了就别回头了
走了就别回头了 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:22

    Check out that fiddle: http://jsfiddle.net/zEPQ5/15/

    It's not perfect in meaning of design, but it shows off the concept.

    I used jQuery with that.

    $(function(){
        var state = 0;
        $('#up').click(function(){
            state += 1;
            $('ul.wrapper').animate({marginTop:(15-state*35)+'px'},400);
        });
        $('#down').click(function(){
            state -= 1;
            $('ul.wrapper').animate({marginTop:(15-state*35)+'px'},400);
        });
    });
    
    0 讨论(0)
  • 2021-01-14 16:34

    So, finally I think I have what you are looking for:

    Fiddle: http://jsfiddle.net/fzXMg/2/

    CSS and HTML is in the Fiddle...

    JS:

    $(function(){
        var state = 0;
        var maxState = 6;
        var winWidth = $(window).width();
        $(window).resize(function(){
            winWidth = $(window).width();
            $('.box,.container_element').width(winWidth-100);
        }).trigger('resize');
        $('#lefty').click(function(){
            if (state==0) {
               state = maxState;
            } else {
               state--;
            }
            $('.container_element').animate({scrollLeft:((winWidth-100)*state)+'px'}, 800);
        });
        $('#righty').click(function(){
            if (state==maxState) {
               state = 0;
            } else {
               state++;
            }
            $('.container_element').animate({scrollLeft:((winWidth-100)*state)+'px'}, 800);
        });
    });
    

    This uses jQuery again.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-14 16:46

    Check out this jsfiddle: http://jsfiddle.net/7vvdB/

    Basically, create an outer container with a max-width of 100% and a overflow-x:scroll, then create an inner container with a fixed width large enough to fit all of your elements, then put all of your elements in the inner container.

    .container_element
    { white-space:nowrap
        min-width:100%;
        overflow-x:scroll;
        overflow-y:hide;
    
    }
    
    .inner_container
    {
        width:5000px;
    }
    }
    
    0 讨论(0)
提交回复
热议问题