diff --git a/README.md b/README.md index 6b45306..c7eb111 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ reset | Return to the first switching select | Select the switching at index next | Go to the next switching prev | Go to the previous switching +random | Go to the random switching destroy | !!Destroy BgSwitcher!! Example for `select` with button: diff --git a/jquery.bgswitcher.js b/jquery.bgswitcher.js index b5d200c..1c1ce25 100644 --- a/jquery.bgswitcher.js +++ b/jquery.bgswitcher.js @@ -203,6 +203,19 @@ this.switching(); }, + /** + * Go to random switching + */ + random: function() { + var index; + do { + index = Math.floor(Math.random() * this.imageList.count()) + } while (this.index === index); + + this.index = index; + this.switching(); + }, + /** * Select the switching at index * diff --git a/test/bg_switcher_spec.js b/test/bg_switcher_spec.js index 8ccb418..fd06632 100644 --- a/test/bg_switcher_spec.js +++ b/test/bg_switcher_spec.js @@ -247,6 +247,22 @@ describe('jQuery.BgSwitcher', function() { }); }); + describe('#random', function() { + beforeEach(function() { + bs.setConfig({images: ['foo', 'bar', 'baz']}); + }); + + it('go to random switching', function() { + bs.select(0); + expect(bs.$bg.css('backgroundImage').split('/').pop()).to.have.contain('foo'); + bs.random(); + var randomImage = bs.$bg.css('backgroundImage').split('/').pop() + expect(randomImage).not.to.have.contain('foo'); + bs.random(); + expect(bs.$bg.css('backgroundImage').split('/').pop()).not.to.have.contain(randomImage); + }); + }); + describe('#select', function() { beforeEach(function() { bs.setConfig({images: ['foo', 'bar', 'baz']});