简体   繁体   English

jQuery hide()然后是animate()然后是show()

[英]jQuery hide() then animate() then show()

Why doesnt this work 为什么不起作用

$("#right").click(function(){
  $("#sliderWindow").find("img").each(function(i) {
    var left = $(this).css("left");
    $("#imageContainer" + i).animate({"left": "+=220px"}, "slow");
    left = parseInt(left);
    left += 220;
    left = left + "px"; 
    $(this).css("left",left);
    left = $(this).css("left");

    if(left === "1220px") {
      //this is what doesnt work
      $(this).css("left", "-320px");
    }

I am sliding a row of divs with animate. 我正在滑动带有动画的div。 Once the last div gets to absolute position left:-1220px, move it back to the start position left:-320px. 一旦最后一个div到达左侧的绝对位置:-1220px,请将其移回到左侧的起始位置:-320px。 It is moving to the correct position but I am having trouble hiding it. 它正在移动到正确的位置,但是我无法隐藏它。

EDIT: The reason I am animating a hidden div is because the animation doesn't seem to be changing the css. 编辑:我为一个隐藏的div设置动画的原因是因为该动画似乎并没有改变css。 Because the css isnt changing I cant roll the last objects back to the front of the line. 因为css没有改变,所以我无法将最后一个对象回滚到行的前面。 Since I can get animate() to accomplish this I am trying to hide the last div and have it appear at the front of the line. 由于我可以获取animate()来完成此操作,因此我试图隐藏最后一个div并将其显示在该行的开头。

SOLVED: 解决了:

$("#right").click(function() {
  $("#sliderWindow").find("img").each(function() {
     if (this.offsetLeft >= 1220) {
        $(this).css("left", "-320px");
    }
    $(this).animate({left: "+=220px"}, "slow");
  });
});

First of all, it's not going to do any good to animate something after you hide it. 首先,隐藏某物后对其进行动画处理不会有任何好处。 :) :)

Secondly, I believe what you're looking for is animate's callback function. 其次,我相信您正在寻找的是动画的回调函数。 If you want something to happen only after the animate is complete you do it like this... 如果您希望仅在动画完成后才发生某事,您可以这样做...

$(this).animate({"left": "-320px"}, "slow", function(){ do_something; });

Let's say <div id="obj"> already has a "position:absolute;", and you want it to move, then disappear... 假设<div id="obj">已经有一个“ position:absolute;”,并且您希望它移动然后消失...

$('#obj').animate({"left": "-320px"}, "slow", function(){ $(this).hide(); });

Solved this with the following: 解决了以下问题:

$("#right").click(function() {
$("#sliderWindow").find("img").each(function() {
  if (this.offsetLeft >= 1220) {
    $(this).css("left", "-320px");
  }
  $(this).animate({left: "+=220px"}, "slow");
});
});

all animations all executed instantly - > you need to use jQuery 所有动画全部立即执行->您需要使用jQuery

   delay()

or 要么

 setTimeout(function(){},timeInMillis);

for example 例如

if(left === "1220px") {
  var $this = $(this);
  $this.hide(200).delay(200).animate({"left": "-320px"}, 300,function(){
   $this.show();
  });
}

Why not just set the new position, since that's what it sounds like you're trying to do. 为什么不只是设置新位置,因为这听起来就像您要尝试的那样。

  if(left === "1220px") {
    $(this).css("left", "-320px");
  }

Try using ... 尝试使用...

if( parseInt(left) > 1219 ) {
   $(this).css("left", "-320px");
}

... in case it's not exactly 1220px ? ...如果不完全是1220px

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM