使用Jquery在单击另一个对象时为对象添加动画效果

3

我编写了一个声音板。当鼠标悬停在按钮上并点击它时,会播放特殊的声音。当鼠标悬停在按钮上时,每个按钮的图片都会改变以显示按下状态。我的问题是,我不知道如何在第5个按钮(中心位置的按钮)被点击后,将图片动画移动到屏幕左侧超过500像素的右侧。该图像只是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个回答

1

CSS

#cat { position : relative; }

0

由于源图像未包含且不太清楚您想要实现什么,因此测试会有些困难,但我会尝试一下。你的javascript代码的第一部分有语法错误,有一个额外的“});”。另外,点击处理程序应放置在$(document).ready()中。css的最后一行中漏掉了一个括号,虽然我相信那只是在复制和粘贴时被遗忘了。另外,Atif是正确的 - position:relative应该应用于动画的目标。以下是重新编写的脚本:

<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接