<div id="dateCarousel">
<a href="#" id="dateCarouselPrev" class="prev"></a>
<a href="#" id="dateCarouselNext" class="next"></a>
<div class="wrapper">
<div class="viewport">
<a href="#">Week 1</a>
<a href="#">Week 2</a>
<a href="#">Week 3</a>
<a href="#">Week 4</a>
<a href="#">Week 5</a>
<a href="#">Week 6</a>
<a href="#">Week 7</a>
<a href="#">Week 8</a>
<a href="#">Week 9</a>
<a href="#">Week 10</a>
<a href="#">Week 11</a>
<a href="#">Week 12</a>
<a href="#">Week 13</a>
<a href="#">Week 14</a>
<a href="#">Week 15</a>
<a href="#">Week 16</a>
<a href="#">Week 17</a>
<a href="#">Week 18</a>
<a href="#">Week 19</a>
<a href="#">Week 20</a>
<a href="#">Week 21</a>
<a href="#">Week 22</a>
<a href="#">Week 23</a>
<a href="#">Week 24</a>
<a href="#">Week 25</a>
<a href="#">Week 26</a>
<a href="#">Week 27</a>
<a href="#" class="dateCarouselSelected">Week 28</a>
<a href="#">Week 29</a>
<a href="#">Week 30</a>
<a href="#">Week 31</a>
<a href="#">Week 32</a>
<a href="#">Week 33</a>
<a href="#">Week 34</a>
<a href="#">Week 35</a>
<a href="#">Week 36</a>
<a href="#">Week 37</a>
<a href="#">Week 38</a>
<a href="#">Week 39</a>
<a href="#">Week 40</a>
<a href="#">Birth</a>
</div>
</div>
</div>
@import "compass/css3";
$white: #fff;
$light-gray: #faf9f8;
$light-blue: #b2deed;
$dark-blue: #0091c2;
$dark-pink: #ef005b;
$light-pink: #f9c8d3;
body {
font-family: Arial, sans-serif;
}
a:focus { outline: none }
$height: 50px;
#dateCarousel {
position: relative;
.wrapper {
overflow: hidden;
height: $height;
position: absolute;
top: 0;
left: 0;
width: 100%;
}
.prev, .next {
position: absolute;
height: $height;
width: 30px;
top:0;
z-index: 1;
background-color: $dark-blue;
&:hover {
background-color: $light-blue;
}
&:before, &:after{
z-index:2;
content: "";
display: block;
position: absolute;
}
}
.prev {
left: 0;
border-right: 10px solid $light-gray;
&:before {
width: 0;
left:6px;
bottom:17px;
border-width: 8px 8px 8px 0;
border-style: solid;
border-color: transparent $white;
}
&:after {
width: 8px;
height:8px;
left:13px;
bottom:21px;
background-color: $white;
}
}
.next {
right: 0;
border-left: 10px solid $light-gray;
&:before {
width: 0;
right:6px;
bottom:17px;
border-width: 8px 0 8px 8px;
border-style: solid;
border-color: transparent $white;
}
&:after {
width: 8px;
height:8px;
right:14px;
bottom:21px;
background-color: $white;
}
}
.viewport {
position: absolute;
top: 0;
left: 0;
right: 0;
height: 200px;
white-space: nowrap;
background: $light-gray;
padding: 1rem;
overflow: auto;
overflow-x: scroll; /* has to be scroll, not auto */
-webkit-overflow-scrolling: touch;
a {
padding: 0 0.5rem 0 0;
white-space: normal;
display: inline-block;
&:not(:last-of-type) {
border-right: 1px solid $light-pink;
}
&:not(:first-of-type) {
margin-left: 0.4rem;
}
&.dateCarouselSelected {
color: $dark-pink;
}
}
}
&.first {
.prev {
display: none;
}
}
&.last {
.next {
display: none;
}
}
}
View Compiled
//http://pure-essence.net/2013/10/25/how-i-made-tiny-carousel-swipeable/
(function ($) {
var opts = {
viewport: '.viewport'
, selected: '.dateCarouselSelected'
, prev: '.prev'
, next: '.next'
, defaultScrollDistance: 200
, scrollAnimationDuration: 500 // ms
, itemSelector: 'a'
, offset: 50
, firstClass: 'first'
, lastClass: 'last'
};
$.fn.carouselSwipeable = function (options) {
var options = $.extend({}, opts, options);
this.each(function () {
new CarouselSwipeable($(this), options);
});
return this;
};
function CarouselSwipeable(root, options) {
/* init date carousel */
var $wrapper = root.find(options.viewport);
var $selectedElement = root.find(options.selected);
var $minItem = $wrapper.find(options.itemSelector).first();
var $maxItem = $wrapper.find(options.itemSelector).last();
root.removeClass(options.firstClass + ' ' + options.lastClass);
if ($selectedElement && $selectedElement.length === 1) {
if ($minItem[0] !== $selectedElement[0]) {
var childPos = $selectedElement.offset();
var parentPos = $wrapper.offset();
var childOffset = {
left: childPos.left - parentPos.left
};
var startPos = childOffset.left;
if ($maxItem[0] !== $selectedElement[0]) {
startPos = startPos - options.offset;
}
else {
root.addClass(options.lastClass);
}
scroll('right', $wrapper, startPos);
}
else {
root.addClass(options.firstClass);
}
}
var scrollDone;
$wrapper.on('scroll', function() {
clearTimeout(scrollDone);
scrollDone = setTimeout(checkEnds, 100);
});
var resizeDone;
$(window).on('resize', function() {
clearTimeout(resizeDone);
resizeDone = setTimeout(checkEnds, 500);
});
root.find(options.prev).on('click', function (e) {
e.preventDefault();
e.stopPropagation();
scroll('left', $wrapper, getDistance());
});
root.find(options.next).on('click', function (e) {
e.preventDefault();
e.stopPropagation();
scroll('right', $wrapper, getDistance());
});
function scroll(direction, $innerWrapper, distance) {
if (distance === undefined) {
distance = options.defaultScrollDistance;
}
var leftPos = $innerWrapper.scrollLeft();
if ('left' === direction) {
leftPos = leftPos - distance;
} else {
leftPos = leftPos + distance;
}
$innerWrapper.animate({scrollLeft: leftPos},
options.scrollAnimationDuration, checkEnds);
}
function checkEnds() {
root.removeClass(options.firstClass + ' ' + options.lastClass);
markFirst();
markLast();
}
function markFirst() {
var $wrapper = root.find(options.viewport);
var minItemOffsetLeft = $minItem.offset().left;
var minItemPositionLeft = minItemOffsetLeft - $wrapper.offset().left;
if (minItemPositionLeft >= 0) {
root.addClass(options.firstClass);
}
}
function markLast() {
var $wrapper = root.find(options.viewport);
var maxItemOffsetLeft = $maxItem.offset().left;
var maxItemPositionLeft = maxItemOffsetLeft - $wrapper.offset().left;
if (maxItemPositionLeft <= $wrapper.outerWidth()) {
root.addClass(options.lastClass);
}
}
function getDistance() {
var $wrapper = root.find(options.viewport);
return $wrapper.outerWidth() - options.offset;
}
}
})(jQuery);
$('#dateCarousel').carouselSwipeable();
This Pen doesn't use any external CSS resources.