実際にスタイルシートをロードするコードを清掃し、ボタンをクリックする代わりにsetInterval呼び出しからトリガーすることができます。スタイルシートのURLを指定する必要があります。これはjavascript配列に格納でき、間隔タイマーによってトリガーされる関数で配列の要素を単純に回転させる(またはランダムに1つ選択する)ことができます。次に、インデックス(mod array size)を進めて、次のスタイルを表示します。
var styles = [ '/example.com/css/style1.css', '/example.com/css/style2.css' ];
var currentStyle = 0;
setInterval( function() {
currentStyle = (currentStyle + 1) % styles.length;
loadStylesheet( currentStyle );
}, 5000 );
更新:私は今日、例をselectで機能するプラグインに変換するのに時間を費やしました。コードは私のブログhttp://farm-fresh-code.blogspot.com/2009/08/jquery-theme-manager-plugin.htmlにあります。これが私がおそらくそれを使用する方法です。これは、theme1.css、theme2.cssなどで機能します。これは、URLのスタイルです:/example.com/styles/theme1.css、..。
脚本:
var currentTheme = 0;
var themes = $('.theme');
themes.retheme({
baseUrl: '/example.com/styles',
loadingImg: '/example.com/images/image.gif'
});
setInterval( function() {
currentTheme = (currentTheme + 1) % themes.length;
themes.eq(currentTheme).trigger('click');
});
HTML:
<input type='hidden' class='theme' value='theme1' />
<input type='hidden' class='theme' value='theme2' />
<input type='hidden' class='theme' value='theme3' />
デモ:
選択とリンクの両方を使用したコードのデモは、http://myweb.uiowa.edu/timv/retheme-demoにあります。