Skip to content
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions jquery.bgswitcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
16 changes: 16 additions & 0 deletions test/bg_switcher_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']});
Expand Down