代码之家  ›  专栏  ›  技术社区  ›  Alex

使用jquery的.animate()从左到右、从右到左对一个div进行动画时遇到问题吗?

  •  0
  • Alex  · 技术社区  · 14 年前

    我使用jquery似乎有困难 .animate() 若要在按钮上从右到左对绝对定位的DIV设置动画,请单击,然后在另一个按钮上从左到右单击。我想知道你是否愿意帮助我理解我做错了什么?谢谢。下面是我的相关css、html和jquery代码。我可以点击 #moveLeft 按钮,它确实会在左侧设置动画,但当我单击 #moveRight 巴顿,什么都没发生。我哪里出错了?

    谢谢!!

    CSS

          #scorecardTwo {
           position:absolute;
           padding:5px;
           width: 300px;
           background-color:#E1E1E1;
           right:0px;
           top:0px;
           display:none;
            } 
    

    HTML

    <div id = "scorecardTwo"> 
       <span id = "dealHolder"><a href="link/to/stuff">text</a></span>
       <br />
       <span id = "scoreHolder">text</span>
       <br />
       <button type="button" id="moveLeft">Left</button>&nbsp;<button type="button" id="moveRight">Right</button>
     </div>
    

    JQuery

    $("#scorecardTwo").fadeIn("slow");
    
    $("#moveLeft").bind("click", function() {
    
     var config = {
       "left" : function() { 
         return $(this).offset().left;
         },
        "right" : function() {
            return $("body").innerWidth() - $K("#scorecardTwo").width();
          }
      };
    
      $("#scorecardTwo").css(config).animate({"left": "0px"}, "slow");
      $(this).attr("disabled", "disabled");
      $("#moveRight").attr("disabled", "");
     });
    
     $("#moveRight").bind("click", function() {
    
       var config = {
         "left" : function() { 
           return $(this).offset().left;
          },
          "right" : function() {
           return $("body").innerWidth() - $K("#scorecardTwo").width();
          }
       };
    
       $("#scorecardTwo").css(config).animate({"right" : "0px"}, "slow");
       $(this).attr("disabled", "disabled");
       $("#moveLeft").attr("disabled", "");
     }); 
    
    1 回复  |  直到 14 年前
        1
  •  4
  •   SLaks    14 年前

    右移时,应设置CSS left null .

    Demo

    $("#moveRight").click(function() {
        var config = {
            left: null,
            right: $("body").innerWidth() - $("#scorecardTwo").width()
        }
    
        $("#scorecardTwo").css(config).animate({right : "0px"}, "slow");
        $(this).attr("disabled", "disabled");
        $("#moveLeft").attr("disabled", "");
    });