Sieen
Sieen

Reputation: 68

Horizontal scrolling UL with buttons and "width:auto" lists

As the title suggests, I am trying to create a horizontal scrollable <ul> with <li> with width:auto and with prev and next buttons, clearly with overflow:hidden. Any suggestions on how to manage it with jquery? This is the code that I have prepared...

JSFiddle

HTML:

<div id="wrapper">
  <span class="arrow">&lt;</span>
  <span class="arrow">&gt;</span>
  <ul id="nav">
    <li>Abbaiare</li>
    <li>Bolle</li>
    <li>Cantante</li>
    <li>Domodossola</li>
    <li>Elefante</li>
    <li>Giovanni</li>
    <li>Hotel</li>
    <li>Inti</li>
    <li>Montagna</li>
    <li>Nave</li>
  </ul>
</div>

CSS:

* {
  margin:0;
  padding:0;
}
#wrapper {
  width:calc(100% - 120px);
  height:60px;
  background-color:#dbdbdb;
  padding:0 60px;
  margin-top:60px;
  font-family:helvetica;
  overflow:hidden;
  position:relative;
  z-index:99; 
}
.arrow {
  display:block;
  width:60px;
  height:60px;
  line-height:60px;
  text-align:center;
  background-color:#cccccc;
  font-weight:bold;
  cursor:pointer;
  position:absolute;
  top:0;
  z-index:101;
}
.arrow:first-of-type {
  left:0;
}
.arrow:nth-of-type(2) {
  right:0; 
}
#nav {
  width:auto;
  height:60px;
  overflow:hidden;
  list-style-type:none;
  white-space:nowrap;
  transition:2.0s;
}
#nav li {
  display:inline-block;
  height:60px;
  line-height:60px;
  font-size:13px;
  padding:0 30px;
}

Upvotes: 1

Views: 4908

Answers (2)

Sieen
Sieen

Reputation: 68

Hoping it will be useful for anyone, This what I needed... (Thank you Praveen Kumar)

JSFiddle

$(".arrow:first-of-type").click(function() {
        var navwidth = $("#nav");
        navwidth.animate( { scrollLeft: '-=200' }, 1000);
    }
);
$(".arrow:nth-of-type(2)").click(function() {
        var navwidth = $("#nav");
        navwidth.animate( { scrollLeft: '+=200' }, 1000);
    }
);

Upvotes: 0

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

You can simply use bxSlider without reinventing the wheel. To make your code work, you just need to add:

$(function () {
  $('#nav').bxSlider({
    pager: false,
    minSlides: 3,
    maxSlides: 3,
    slideWidth: 150,
    slideMargin: 25
  });
});
#wrapper {
  margin: auto;
}
.bx-viewport {
  height: 50px !important;
  padding: 0 30px;
  box-sizing: border-box;
  text-align: center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bxslider/4.2.5/jquery.bxslider.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bxslider/4.2.5/vendor/jquery.easing.1.3.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bxslider/4.2.5/vendor/jquery.fitvids.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bxslider/4.2.5/jquery.bxslider.min.css" />
<div id="wrapper">
  <ul id="nav">
    <li>Abbaiare</li>
    <li>Bolle</li>
    <li>Cantante</li>
    <li>Domodossola</li>
    <li>Elefante</li>
    <li>Giovanni</li>
    <li>Hotel</li>
    <li>Inti</li>
    <li>Montagna</li>
    <li>Nave</li>
  </ul>
</div>

Upvotes: 1

Related Questions