先日紹介した「jQueryでボタンなどのロールオーバー時、背景画像にエフェクトを掛ける方法」の
マウスオーバー時の背景画像切り替えパターンを
別バージョンでいくつか作ってみたので加えて紹介してみます。
HTMLソースとCSSなどについては全く変更なしですが
念のため、記載しておくと
◆HTML
<a href="#">BUTTON A</a> <a href="#">BUTTON B</a> <a href="#">BUTTON C</a>
◆CSS
a {
width: 130px;
height: 50px;
line-height: 50px;
font-size: 15px;
font-weight: bold;
color: #fff;
text-align: center;
position: relative;
overflow: hidden;
display: block;
}
こんな感じです。
このテンプレートパターン(あくまでテンプレなのでもちろん変更することも可能)で
ロールオーバー時にアニメーションエフェクトを付けます。
(先日のエントリーでは単純なフェードとスライドダウンの2パターン)
◆パターン1
ボタン全体がスロットマシーンのように動くアニメーション。
この動作SCRIPTは以下のようになります。
$(function(){
var linkWidth = $('a').width();
var linkHeight = $('a').height();
$("a").each(function(){
var txt = $(this).text();
$(this).prepend('<span class="ov">' + txt + '</span>');
$(this).prepend('<span class="out">' + txt + '</span>');
$('a span').css({
width:(linkWidth),
height:(linkHeight),
display:'block',
cursor:'pointer',
backgroundImage:'url(img/button.jpg)'
});
$('a span.ov').css({backgroundImage:'url(img/button_ov.jpg)'});
});
$("a").hover(function(){
$(this).children('span.out').stop().animate({marginTop:'-' + linkHeight + 'px'},300);
}, function(){
$(this).children('span.out').stop().animate({marginTop:'0'},300);
});
});
前回ではボタンの幅や高さは「%」で扱っていましたが
今回は変数に入れています。
アニメーションで動かす値をSCRIPT側では持たず、
CSS側で指定する値を見て制御できる方が、
使うたびにわざわざSCRIPTをいじらなくてもすむように。
そして続いて…
◆パターン2
パターン1の横バージョンといったところ。
あまり使いどころはないかもしれませんが、
ちょっと試してみたかったパターン。
このパターン2のSCRIPTは以下のような感じに。
$(function(){
var linkWidth = $('a').width();
var linkHeight = $('a').height();
$("a").each(function(){
var txt = $(this).text();
$(this).prepend('<span class="ov">' + txt + '</span>');
$(this).prepend('<span class="out">' + txt + '</span>');
$('a span').css({
top:'0',
left:'0',
width:(linkWidth),
height:(linkHeight),
display:'block',
position:'absolute',
cursor:'pointer',
backgroundImage:'url(img/button.jpg)'
});
$('a span.ov').css({
left:(linkWidth),
backgroundImage:'url(img/button_ov.jpg)'
});
});
$("a").hover(function(){
$(this).children('span.out').stop().animate({left:'-' + linkWidth + 'px'},300);
$(this).children('span.ov').stop().animate({left:'0'},300);
}, function(){
$(this).children('span.out').stop().animate({left:'0'},300);
$(this).children('span.ov').stop().animate({left:linkWidth + 'px'},300);
});
});
最後は…
◆パターン3
マウスオーバー画像をブラインドの様に上からスライドインさせています。
このパターン3のSCRIPTは以下のような感じに。
$(function(){
var linkWidth = $('a').width();
var linkHeight = $('a').height();
$("a").each(function(){
var txt = $(this).text();
$(this).prepend('<span class="ov">' + txt + '</span>');
$(this).prepend('<span class="out">' + txt + '</span>');
$('a span').css({
top:'0',
left:'0',
width:(linkWidth),
height:(linkHeight),
display:'block',
position:'absolute',
cursor:'pointer',
backgroundImage:'url(img/button.jpg)'
});
$('a span.ov').css({
height:'0',
backgroundImage:'url(img/button_ov.jpg)'
});
});
$("a").hover(function(){
$(this).children('span.ov').stop().animate({height:(linkHeight)},300);
}, function(){
$(this).children('span.ov').stop().animate({height:'0'},300);
});
});
ひとまずはこんな感じです。
CSSとjQueryを組み合わせば単純なマウスオーバーでも様々な演出を加えることが可能になり、
今回紹介した他にも応用すればまだまだいろんなパターンを実現することができます。
jquery.easing.jsとかを使えばまた違った演出を加えることも。
やっぱjQueryはおもしろい。
ご参考までに。。。
