【问题标题】:Jquery Animate Image right on click event then back to the left on second click eventJquery Animate Image 在点击事件上向右然后在第二次点击事件上回到左边
【发布时间】:2013-11-04 08:08:24
【问题描述】:

我正在尝试为一组图像设置动画,以在第一次单击时单独向右移动 300 像素,再次单击时将 300 像素向左移动回到起始位置。

我现在拥有的东西不起作用。所以要么是 A)我的语法有问题,要么是 B)我的图像实际上没有以 5px 渲染。

编辑:这个小提琴现在更新了正确的代码

Jsfiddle Demo

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

【问题讨论】:

    标签: javascript jquery html css jquery-animate


    【解决方案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);
            }
        });
    });
    

    Fiddle here.

    【讨论】:

    • 哦,完美,为什么将 (this) 放在 var 中而不是像我一样正常调用它??
    • 为了安全起见..你也可以继续不输入变量
    【解决方案2】:

    您必须将$('this') 更改为$(this),元素this 始终不带引号。

    您好。

    【讨论】:

      猜你喜欢
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 2017-07-03
      • 1970-01-01
      • 2013-12-20
      • 2022-01-24
      • 2012-05-17
      • 1970-01-01
      相关资源
      最近更新 更多