在Jquery中单击另一个对象时动画对象

时间:2012-11-22 07:17:31

标签: jquery animation button click move

我编写了一个音板。鼠标悬停在按钮上,单击它,然后播放特殊声音。当鼠标悬停在按钮上时,earch按钮的图片会变为按下状态。我的问题是我无法想象如何动画一个图片动画从屏幕左侧移动500px到右边一次按钮#5(中间的那个)点击。图像是这个Facebook图标,就像下面的占位符代码一样!

 <!DOCTYPE html>
    <html>
     <head>
     <link rel="stylesheet" type="text/css" href="styles.css" />
 <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">


    $('#button5').click(function() {
            $('#cat').animate({
                    right: '+=500'
                    }
                    , 5000 );
                });
});
    $(document).ready(function ()
    {
        var audioon = false;

        $('.button').click(function () 
        { 
            var audioObj = $(this).find("audio");
            var audioTag = audioObj[0];
            audioTag.volume = 0.90;

            if (audioon)
            {
                audioTag.pause();
                audioon = false;

            } else {
                audioTag.play();
                audioon = true;
            }

    });
});
</script>

    <title>Square 205 Soundboard</title>
</head>
<body>
<div class="container">
<div="buttons">
<div id="cat">
<img src="http://www.avca.org/includes/media/images/facebook-icon-small.jpg">
</div>
    <div class="row">
        <div id="button1" class="button">
            <audio preload="none">
                <source src="sounds/DBZ.mp3" type="audio/mpeg">
            </audio>
        </div>
        <div id="button2" class="button">
            <audio preload="none">
                  <source src="sounds/GD.mp3" type="audio/mpeg">
            </audio>
        </div>
        <div id="button3" class="button">
            <audio preload="none">
                <source src="sounds/LeanneBoops.mp3" type="audio/mpeg">
            </audio>
        </div>
    </div>
    <div class="row">
        <div id="button4" class="button">
            <audio preload="none">
                <source src="sounds/MakeMeMoney.mp3" type="audio/mpeg">
            </audio>
        </div>
        <div id="button5" class="button">
            <audio preload="none">
                <source src="sounds/Meow.mp3" type="audio/mpeg">
            </audio>
        </div>
        <div id="button6" class="button">
            <audio preload="none">
                <source src="sounds/Yeah.mp3" type="audio/mpeg">
            </audio>
        </div>
    </div>
    <div class="row">
        <div id="button7" class="button">
            <audio preload="none">
                <source src="sounds/Dang.mp3" type="audio/mpeg">
            </audio>
        </div>
        <div id="button8" class="button">
            <audio preload="none">
                <source src="sounds/Benny.mp3" type="audio/mpeg">
            </audio>
        </div>
        <div id="button9" class="button">
            <audio preload="none">
                <source src="sound.mp3" type="audio/mpeg">
            </audio>
        </div>
    </div>
    <div class="row">
        <div id="button10" class="button">
            <audio preload="none">
                <source src="sound.mp3" type="audio/mpeg">
            </audio>
        </div>
    </div>

    </div>
</div>

</body>

</html>

**CSS**

    body {
    background-color: white;
}

audio {
    display: none;
}


.container {
    width:960px;
    margin:0 auto;
    background:url(buttons/frame.png) no-repeat center top;
    padding-bottom: 110px;
    padding-top: 20px;
}

.row {
    width:100%;
    text-align:center;
}

.button:hover {
    cursor:pointer;
}

.row div{
    display: inline-block;
    margin-right: 2px;
}

.button {
    height:143px;
    width:143px;
}

#button1 {
    background:url(buttons/button1.png) no-repeat;
}

#button1:hover
{
    background:url(buttons/button1p.png) no-repeat;
}

#button2 {
    background:url(buttons/button2.png) no-repeat;
}

#button2:hover
{
    background:url(buttons/button2p.png) no-repeat;
}

#button3 {
    background:url(buttons/button3.png) no-repeat;
}

#button3:hover
{
    background:url(buttons/button3p.png) no-repeat;
}

#button4 {
    background:url(buttons/button4.png) no-repeat;
}

#button4:hover
{
    background:url(buttons/button4p.png) no-repeat;
}

#button5 {
    background:url(buttons/button5.png) no-repeat;
}

#button5:hover
{
    background:url(buttons/button5p.png) no-repeat;
}

#button6 {
    background:url(buttons/button6.png) no-repeat;
}

#button6:hover
{
    background:url(buttons/button6p.png) no-repeat;
}

#button7 {
    background:url(buttons/button7.png) no-repeat;
}

#button7:hover
{
    background:url(buttons/button7p.png) no-repeat;
}

#button8 {
    background:url(buttons/button8.png) no-repeat;
}

#button8:hover
{
    background:url(buttons/button8p.png) no-repeat;
}

#button9 {
    background:url(buttons/button9.png) no-repeat;
}

#button9:hover
{
    background:url(buttons/button9p.png) no-repeat;
}

#button10 {
    background:url(buttons/button1.png) no-repeat;
}

#button10:hover
{
    background:url(buttons/button1p.png) no-repeat;

2 个答案:

答案 0 :(得分:1)

CSS

#cat { position : relative; }

答案 1 :(得分:0)

测试这个有点困难,因为源图像不包括在内,并且你想要完成的内容并不完全清楚,但我会试一试。你的javascript的第一部分有一个语法错误,一个额外的});.此外,单击处理程序应放在$(document).ready()内。从CSS的最后一行遗漏了一个括号,但我确信在复制和粘贴过程中忘记了这一点。此外,Atif是正确的 - 位置:相对应该应用于动画的目标。这是重写的脚本:

<script type="text/javascript">
$(document).ready(function() 
{
    var audioon = false;

    $('.button').click(function() 
    {
        var audioObj = $(this).find("audio");
        var audioTag = audioObj[0];
        audioTag.volume = 0.90;

        if (audioon) 
        {
            audioTag.pause();
            audioon = false;

        } else 
        {
            audioTag.play();
            audioon = true;
        }

    });
    $('#button5').click(function() 
    {
        $('#cat').animate(
        {
            right : '+=500'
        }, 5000);
    });
});
</script>