Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Added slideTo, slidePrev and slideNext public methods #367

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,22 @@
<strong>This</strong> is an example of a <em>HTML</em> caption with <a href="#">a link</a>.
</div>
</div>

<div>
<a href="#" id="slideto">Slide to 2</a>
</div>

</div>
<script type="text/javascript" src="scripts/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="../jquery.nivo.slider.js"></script>
<script type="text/javascript">
$(window).load(function() {
$('#slider').nivoSlider();
$('#slideto').on('click', function(e) {
e.preventDefault();
$('#slider').data('nivoslider').slideTo(2, 0);
return false;
});
});
</script>
</body>
Expand Down
83 changes: 66 additions & 17 deletions jquery.nivo.slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* http://www.opensource.org/licenses/mit-license.php
*/

/* Edited by Fabio Borella */

(function($) {
var NivoSlider = function(element, options){
// Defaults are below
Expand All @@ -26,7 +28,55 @@
// Get this slider
var slider = $(element);
slider.data('nivo:vars', vars).addClass('nivoSlider');


// Add listeners

slider.on('slideto.nivoslider', function(e, index, animSpeed) {
if(vars.running) return false;
if($(this).hasClass('active')) return false;
clearInterval(timer);
timer = '';
sliderImg.attr('src', vars.currentImage.attr('src'));
vars.currentSlide = index - 1;
if (!isNaN(animSpeed)) {
originalSpeed = settings.animSpeed;
settings.animSpeed = animSpeed;
}
nivoRun(slider, kids, settings, 'control');
if (!isNaN(animSpeed)) {
settings.animSpeed = originalSpeed;
}
});

slider.on('slideprev.nivoslider', function(e, animSpeed) {
if(vars.running) { return false; }
clearInterval(timer);
timer = '';
vars.currentSlide -= 2;
if (!isNaN(animSpeed)) {
originalSpeed = settings.animSpeed;
settings.animSpeed = animSpeed;
}
nivoRun(slider, kids, settings, 'prev');
if (!isNaN(animSpeed)) {
settings.animSpeed = originalSpeed;
}
});

slider.on('slidenext.nivoslider', function(e, animSpeed) {
if(vars.running) { return false; }
clearInterval(timer);
timer = '';
if (!isNaN(animSpeed)) {
originalSpeed = settings.animSpeed;
settings.animSpeed = animSpeed;
}
nivoRun(slider, kids, settings, 'next');
if (!isNaN(animSpeed)) {
settings.animSpeed = originalSpeed;
}
});

// Find our slider children
var kids = slider.children();
kids.each(function() {
Expand Down Expand Up @@ -124,18 +174,11 @@
slider.append('<div class="nivo-directionNav"><a class="nivo-prevNav">'+ settings.prevText +'</a><a class="nivo-nextNav">'+ settings.nextText +'</a></div>');

$(slider).on('click', 'a.nivo-prevNav', function(){
if(vars.running) { return false; }
clearInterval(timer);
timer = '';
vars.currentSlide -= 2;
nivoRun(slider, kids, settings, 'prev');
slider.trigger('slideprev.nivoslider');
});

$(slider).on('click', 'a.nivo-nextNav', function(){
if(vars.running) { return false; }
clearInterval(timer);
timer = '';
nivoRun(slider, kids, settings, 'next');
slider.trigger('slidenext.nivoslider');
});
}

Expand All @@ -160,13 +203,7 @@
$('a:eq('+ vars.currentSlide +')', vars.controlNavEl).addClass('active');

$('a', vars.controlNavEl).bind('click', function(){
if(vars.running) return false;
if($(this).hasClass('active')) return false;
clearInterval(timer);
timer = '';
sliderImg.attr('src', vars.currentImage.attr('src'));
vars.currentSlide = $(this).attr('rel') - 1;
nivoRun(slider, kids, settings, 'control');
slider.trigger('slideto.nivoslider', [ $(this).attr('rel') ]);
});
}

Expand Down Expand Up @@ -615,6 +652,18 @@
}
};

this.slideTo = function(index, animSpeed){
$(element).trigger('slideto.nivoslider', [ index, animSpeed ]);
};

this.slidePrev = function(animSpeed) {
$(element).trigger('slideprev.nivoslider', [ animSpeed ]);
};

this.slideNext = function(animSpeed) {
$(element).trigger('slidenext.nivoslider', [ animSpeed ]);
};

// Trigger the afterLoad callback
settings.afterLoad.call(this);

Expand Down
Loading