Skip to content

Instantly share code, notes, and snippets.

@JamsMendez
Created August 15, 2014 16:47
Show Gist options
  • Save JamsMendez/90389765d9aade807a6f to your computer and use it in GitHub Desktop.
Save JamsMendez/90389765d9aade807a6f to your computer and use it in GitHub Desktop.
Directive Carousel, simulates a form component, Select
/*
Dependencies
http://sorgalla.com/jcarousel/
Implementation
<carousel ng-model="Object.property" elements="elements"></carousel>
<script type="text/ng-template" id="carousel.html">
<div class="wrapper">
<div class="jcarousel-wrapper" style="width:100px;">
<div class="jcarousel">
<ul>
<li class="element" id="li{{ element.id }}" ng-repeat="element in elements"><img ng-src="{{ element.src }}" width="100" height="90" alt=""></li>
</ul>
</div>
<a href="" class="jcarousel-control-prev">&lsaquo;</a>
<a href="" class="jcarousel-control-next">&rsaquo;</a>
</div>
</div>
</script>
*/
// Directive
directives.carousel = function () {
return {
restrict: 'E',
require: '?ngModel',
scope: '=',
templateUrl: 'carousel.html',
link: function (scope, element, attrs, ngModel){
if(!ngModel){
return;
}
$('.jcarousel').jcarousel();
$('.jcarousel-control-prev')
.on('jcarouselcontrol:active', function() {
$(this).removeClass('inactive');
})
.on('jcarouselcontrol:inactive', function() {
$(this).addClass('inactive');
})
.jcarouselControl({
target: '-=1'
});
$('.jcarousel-control-next')
.on('jcarouselcontrol:active', function() {
$(this).removeClass('inactive');
})
.on('jcarouselcontrol:inactive', function() {
$(this).addClass('inactive');
})
.jcarouselControl({
target: '+=1'
});
$('.jcarousel-pagination')
.on('jcarouselpagination:active', 'a', function() {
$(this).addClass('active');
})
.on('jcarouselpagination:inactive', 'a', function() {
$(this).removeClass('active');
})
.jcarouselPagination();
$('.jcarousel').on('jcarousel:visiblein', 'li', function(event, carousel) {
var elementId = $(event.target).attr('id');
var _id = elementId.replace('li', '');
console.log(_id);
ngModel.$setViewValue(_id);
});
}
}
}
/* CSS
.jcarousel-wrapper {
margin: 20px auto;
position: relative;
border: 0px solid #fff;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 0 2px #999;
-moz-box-shadow: 0 0 2px #999;
box-shadow: 0 0 2px #999;
}
.jcarousel-wrapper .photo-credits {
position: absolute;
right: 15px;
bottom: 0;
font-size: 13px;
color: #fff;
text-shadow: 0 0 1px rgba(0, 0, 0, 0.85);
opacity: .66;
}
.jcarousel-wrapper .photo-credits a {
color: #fff;
}
.jcarousel {
position: relative;
overflow: hidden;
width: 100px;
height: 90px;
}
.jcarousel ul {
width: 20000em;
position: relative;
list-style: none;
margin: 0;
padding: 0;
}
.jcarousel li {
float: left;
}
.jcarousel-control-prev,
.jcarousel-control-next {
position: absolute;
top: 25px;
width: 30px;
height: 30px;
text-align: center;
background: #4E443C;
color: #fff;
text-decoration: none;
text-shadow: 0 0 1px #000;
font: 24px/27px Arial, sans-serif;
-webkit-border-radius: 30px;
-moz-border-radius: 30px;
border-radius: 30px;
-webkit-box-shadow: 0 0 2px #999;
-moz-box-shadow: 0 0 2px #999;
box-shadow: 0 0 2px #999;
}
.jcarousel-control-prev {
left: -50px;
}
.jcarousel-control-next {
right: -50px;
}
.jcarousel-control-prev:hover span,
.jcarousel-control-next:hover span {
display: block;
}
.jcarousel-control-prev.inactive,
.jcarousel-control-next.inactive {
opacity: .5;
cursor: default;
}
.jcarousel-pagination {
position: absolute;
bottom: 0;
left: 15px;
}
.jcarousel-pagination a {
text-decoration: none;
display: inline-block;
font-size: 11px;
line-height: 14px;
min-width: 14px;
background: #fff;
color: #4E443C;
border-radius: 14px;
padding: 3px;
text-align: center;
margin-right: 2px;
opacity: .75;
}
.jcarousel-pagination a.active {
background: #4E443C;
color: #fff;
opacity: 1;
text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.75);
}
.element{
border-bottom: 0px;
background: none;
padding: 0px;
}
.element:last-child {
border: 0;
padding: 0px;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment