2

一連の画像をアニメーション化して、最初のクリックで右に 300 ピクセル移動し、もう一度クリックすると左に 300 ピクセル移動して開始位置に戻そうとしています。

私が今持っているものは機能していません。つまり、A) 構文に問題があるか、B) 画像が実際に 5px でレンダリングされないかのいずれかです。

編集:このフィドルは正しいコードで更新されました

Jsfiddle デモ

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <style type="text/css">
        #wrapper {
            width: 80em;
        }

        .image {
            width: 10em;
            height: 10em;   
            display: block;
            position: relative;
            left:5px;
        }
    </style>

    <script type="text/javascript">
    $(document).ready(function() {
        $(".image").click(function() {
            if ($('this').css('left') =='5px') {                    
                $(".image").animate({"right": "300px"},3000);
            else
                $(".image").animate({"left": "300px"},3000);    
            }
        });     
    });     
    </script>
</head>
<body>
    <div id="wrapper">
        <section id="gallery">          
            <img id="avery" class="image" src='images/OAI.jpg' alt= "On Avery Island Album Art">
            <img id="overthesea" class="image" src='images/ITAOTS.jpg' alt="In the Aeroplane Over the Sea album art">   
            <img id="beauty" class="image" src='images/beauty.jpg' alt="Beauty Demo Album Art">
            <img id="everthingIs" class="image" src='images/everythingis.jpg' alt="Eveything Is EP Album Art">
        </section>
    </div>  
</body> 
</html>                 
4

2 に答える 2

1

$('this')forを変更する必要があります。$(this)要素thisには常に引用符がありません。

ご挨拶。

于 2013-11-04T08:25:14.533 に答える
1

これを試して:

$(document).ready(function () {
    $(".image").click(function () {
        var th = $(this);
        if ($(th).css('left') == '5px') {
            console.log("ret");
            $(th).animate({
                "left": "300px"
            }, 3000);
        } else {
            console.log("sdffsdsff");
            $(th).animate({
                "left": "5px"
            }, 3000);
        }
    });
});

ここでフィドル。

于 2013-11-04T08:17:27.837 に答える