JQM horizontal scroll navbar

2019-01-22 14:59发布

问题:

i"ve been hunting the docs and can't seem to find a way to make a scrollable horizontal navbar in jQuery mobile has anybody accomplished this yet ?

here's what i have for navbar so far

<div data-role="header" data-scroll="x">
    <ul>
        <li class="logo"><a href="#"><img src="img/iphoneheader.gif" alt="Penn State Live" /></a></li>
        <li id="link"><a href="#type=colleges">Colleges</a></li>
        <li><a href="#">Campuses</a></li>
        <li><a href="#">Faculty and Staff</a></li>
        <li><a href="#">Of Interest</a></li>
        <li><a href="#">Photos</a></li>
        <li><a href="#">Video</a></li>
        <li><a href="#">Newswire Subscription</a></li>
        <li><a href="#">PSUTXT</a></li>
    </ul>
</div>

回答1:

I guess this is what you want.

HTML.

    <div class="categories">                
            <ul>                    
                <li><span><a href="">ABC</a></span></li>
                <li><span><a href="">DEF</a></span></li>
                <li><span><a href="">GHI</a></span></li>
                <li><span><a href="">JKL</a></span></li>
            </ul>               
        </div>

JQuery

$(function(){           
    var step = 1;
    var current = 0;
    var maximum = $(".categories ul li").size();
    var visible = 2;
    var speed = 500;
    var liSize = 120;
    var height = 60;    
    var ulSize = liSize * maximum;
    var divSize = liSize * visible; 

    $('.categories').css("width", "auto").css("height", height+"px").css("visibility", "visible").css("overflow", "hidden").css("position", "relative");
    $(".categories ul li").css("list-style","none").css("display","inline");
    $(".categories ul").css("width", ulSize+"px").css("left", -(current * liSize)).css("position", "absolute").css("white-space","nowrap").css("margin","0px").css("padding","5px");

    $(".categories").swipeleft(function(event){
        if(current + step < 0 || current + step > maximum - visible) {return; }
        else {
            current = current + step;
            $('.categories ul').animate({left: -(liSize * current)}, speed, null);
        }
        return false;
    });

    $(".categories").swiperight(function(){
        if(current - step < 0 || current - step > maximum - visible) {return; }
        else {
            current = current - step;
            $('.categories ul').animate({left: -(liSize * current)}, speed, null);
        }
        return false;
    });         
});


回答2:

Also, try out: http://kryops.de/jqm/tabs/demo

This looks really promising and performance is great on mobile.



回答3:

I know it's not what you're looking for but:

Live Example: http://jsfiddle.net/9zuxH/10/

<div data-role="page" data-theme="b" id="jqm-home">
    <ul >
        <li data-role="fieldcontain"> 
            <fieldset data-role="controlgroup" data-type="horizontal">
                <div class="logo"><a href="#"><img src="img/iphoneheader.gif" alt="Penn State Live" /></a></div>
                <div id="link"><a href="#type=colleges">Colleges</a></div>
                <a href="#">Campuses</a>
                <a href="#">Faculty and Staff</a>
                <a href="#">Of Interest</a>
                <a href="#">Photos</a>
                <a href="#">Video</a>
                <a href="#">Newswire Subscription</a>
                <a href="#">PSUTXT</a>
            </fieldset>
        </li>
    </ul>
</div>

Documentation: jquerymobile.com/demos/1.0a4.1/docs/lists/lists-forms-inset.html

UPDATED the example: http://jsfiddle.net/9zuxH/21/ A little better



回答4:

This is after ypur lst comment, so I'll be addressing the need to scroll the navbar with a finger.

You pointed to an implementation that has problems. There are others. I have used this once: http://plugins.jquery.com/project/jQclickNScroll for a desktop touchscreen. (not sure how it behaves on mobile) It worked with a hrefs and it has an option allowHiliting:true that turns off preventing other events passing through

And there's this: http://digitalillusion.altervista.org/wordpress/pages/dragscroller/viewport-test.html



回答5:

Stumbled across this while researching the same issue. Haven't tried it yet but it looks pretty handy

http://darsa.in/sly/