2013年6月21日

JSでお手軽ホバー(hover)

CSSだけでも出来るのだけど面倒な時は、これでいいかな?

hoverする画像に「xxxx_off.gif」と「xxxx_on.gif」 の2種類の画像を準備する
あとJSを読み込み普通にリンクするだけ




html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="jquery.min.js"></script>
<script src="hover.js"></script>

<title>JSでホバー</title>
</head>

<body>
<p><a href="#"><img src="画像名_off.gif" alt="button" /></a></p>
</body>
</html>

JS
// JavaScript Document
$(function(){
    $('a img').hover(function(){
        $(this).attr('src', $(this).attr('src').replace('_off', '_on'));
            }, function(){
               if (!$(this).hasClass('current')) {
               $(this).attr('src', $(this).attr('src').replace('_on', '_off'));
        }
    });
});

0 件のコメント:

コメントを投稿

ドメインって、早い者勝ちですよ!