Search code examples
jqueryjscrollpanejquery-jscrollpane

JQuery Jscrollpane Horizontal Scroll With inline HTML List


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;}

Solution

  • 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>