JQuery Jscrollpane Horizontal Scroll With inline HTML List

5.1k views Asked by At

I would like to have a scrollpane with a html list that will only scroll horizontally. When I use the code below, my list items clear each other after a full row and the scroll pane only scrolls vertically.

<div class="scrolling-list">
<ul>
<li> List item 1</li>
<li> List item 2</li>
<li> List item 3</li>
<li> List item 4</li>
<li> List item 5</li>
</ul>    
</div>

JS

<script type="text/javascript">
        $(function()
        {
            $('.scrolling-list').jScrollPane();
        });
</script>

CSS

.scrolling-list{
height:auto;
max-height:200px;
width: 640px;
}
li{
float:left;
width:200px;}
2

There are 2 answers

1
vitch On BEST ANSWER

If the inline-block approach doesn't work for you then you could use javascript to set the width of the UL to the total width of all its children e.g. something like:

<script type="text/javascript">
    $(function()
    {
        var list = $('.scrolling-list');
        list.find('ul').each(
            function()
            {
                var w = 0;
                $(this).find('li').each(
                    function()
                    {
                        w += $(this).outerWidth();
                    }
                ).css('width', w + 'px');
            }
        )
        list.jScrollPane();
    });
</script>
0
mVChr On

Instead of using floats you'll need to use inline-block display and add whitespace:nowrap to your CSS. Note that this might not be compatible with some IE browsers.

.scrolling-list {
    height:auto;
    max-height:200px;
    width:640px;
    white-space:nowrap;
}
.scrolling-list li {
    display:inline-block;
    width:200px;
}