MAC風DOCメニューをjQueryで作るチュートリアル
MAC風DOCメニューをjQueryのanimateを使えば簡単に実装できるであろうと思い作成したのですが、なかなか大変でした。
HTMLソース
<ul id="doc"> <li><a href="#"><img src="../img/icon01.png" alt="Photoshop" width="28" height="26" w /></a></li> <li><a href="#"><img src="../img/icon02.png" alt="Illustrator" width="28" height="26" /></a></li> <li><a href="#"><img src="../img/icon03.png" alt="Dreamweaber" width="28" height="26" /></a></li> <li><a href="#"><img src="../img/icon04.png" alt="Fireworks" width="28" height="26" /></a></li> <li><a href="#"><img src="../img/icon05.png" alt="Flash" width="28" height="26" /></a></li> </ul>
注意点(width属性とheight属性の指定)
このimg要素にはすべてwidth属性とheight属性を明記しています。
その理由としては、JavaScriptでwidth();とheight();と値を取得する際に、読み込み速度等できっちり値を取得してくれない場合があるためです。
さらにsafariでは、img要素にwidth属性とheight属性を指定しないと、JavaScriptでは値を取得してくれません。
ですので、widthとheightの値はを明記しているのです。
Javascriptソース
$(function(){
var DOC = "#doc";
$("li" , DOC).each(function(){
var imgWidth =$("img" , this).width();
var imgHeight =$("img" , this).height();
$(this).css({"position": "relative", "width": imgWidth + "px", "height": imgHeight + "px"});
$("a" , this).css({"display": "block"});
$("img" , this).css({"width": imgWidth + "px", "height": imgHeight + "px"});
$(this).hover(function(){
$("img" , DOC).css({"zIndex": "1"});
$("img" , this).css({"position": "absolute", "zIndex": "100"});
$("img" , this).stop().animate({
"top": "-" + imgHeight / 2 + "px",
"left": "-" + imgWidth / 2 + "px",
"width": imgWidth * 2 + "px",
"height": imgHeight * 2 + "px"
}, 300);
}, function(){
$(this).css({"width": imgWidth + "px", "height": imgHeight + "px"});
$("img" , this).stop().animate({
"top": 0,
"left": 0,
"width": imgWidth + "px",
"height": imgHeight + "px"
}, 300);
});
});
});
Javascriptの解説
- 4-9行目:変数と初期設定
- 4-5行目の変数で画像の幅、高さを取得します。
次にcssの設定をします。Javascriptで指定することで、どんな幅、高さの画像でも対応できるようにしています。 - 11-28行目:ホバー設定
- 12-13行目でz-indexを全て1にし、ホバーした要素のz-indexを100にしています。
14-19行目にanimate設定が記述されています。
ここでポイントとなるのが、top、leftの値に「-」が付いていることです。
画像を2倍にするのは「*2」で実現できますが、それだけですと、左上が基点となり大きくなります。
そのため、画像サイズの半分だけマイナスに移動させれば、真ん中から拡大しているようになります。
サンプル
サンプルはこちらになります
- Web-Parkのホーム >
- Javascript >
- MAC風DOCメニューをjQueryで作るチュートリアル
Warning: count(): Parameter must be an array or an object that implements Countable in /home/markcrest/web-park.org/public_html/wordpress/wp-includes/class-wp-comment-query.php on line 405
COMMENT PLEASE!!
トラックバック
- 「MAC風DOCメニューをjQueryで作るチュートリアル」のトラックバックURL
Warning: Use of undefined constant display - assumed 'display' (this will throw an Error in a future version of PHP) in /home/markcrest/web-park.org/public_html/wordpress/wp-content/themes/default/single.php on line 105
http://web-park.org/javascript/macdoc100301.html/trackback