jqueryとjquery.cookieを使って、CSSのclassを変更します。文字サイズはCSSでパーセンテージで記述しておきます。
ヘッダーで、jquery.jsとjquery.cookieと後述のfontsize.jsを読み込んでいます。
参考サイト>>>フォントサイズを変える「大・中・小」ボタンを実装する方法
デモページへopen_in_new
JSを読み込みます
< script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" ></ script >
< script src = "common/js/jquery.cookie.js" ></ script >
< script src = "common/js/fontsize.js" ></ script >
|
[fontsize.js]JavaScript
jQuery( function ($){
var history = $.cookie( 'fontSize' );
var elm = $( 'body' );
if (!history){
elm.addClass( 'fontS' );
$( '#fontS' ).addClass( 'active' );
} else {
elm.addClass(history);
$( '#' +history).addClass( 'active' );
}
$( 'li' , '.mod_headerbox_size' ).click( function (){
if (!$( this ).hasClass( 'active' )){
$( '.active' ).removeClass( 'active' );
$( this ).addClass( 'active' );
var setFontSize = this .id;
$.cookie( 'fontSize' , setFontSize);
elm.removeClass().addClass(setFontSize);
}
});
});
|
HTMl
< div class = "mod_headerbox_size" >
< ul >
< li id = "fontS" >< span >< img width = "30" height = "60" alt = "小さいサイズ" src = "common/img/size_s.jpg" ></ span ></ li >
< li id = "fontM" >< span >< img width = "30" height = "60" alt = "少し大きいサイズ" src = "common/img/size_m.jpg" ></ span ></ li >
< li id = "fontL" >< span >< img width = "30" height = "60" alt = "大きいサイズ" src = "common/img/size_b.jpg" ></ span ></ li >
</ ul >
</ div >
|
CSS
.fontS { font-size : 75% }
.fontM { font-size : 85% }
.fontL { font-size : 95% }
.mod_headerbox_size {
width : 90px ;
position : relative ;
overflow : hidden ;
}
.mod_headerbox_size ul {
}
.mod_headerbox_size ul li {
float : left ;
width : 30px ;
height : 30px ;
overflow : hidden ;
}
.mod_headerbox_size ul li span {
display : block ;
cursor : pointer ;
}
.mod_headerbox_size ul li.active span {
margin-top : -30px ;
}
.mod_headerbox_size ul li span:hover{
margin-top : -30px ;
}
|