简体   繁体   English

具有相同ID的元素逐渐消失

[英]Fading in element with the same id

I am having troubles with this code. 我在使用此代码时遇到麻烦。 It's supposed to do this: 应该这样做:

  1. Start the function with clicking on an element with class "portfolioImage". 单击具有“ portfolioImage”类的元素以启动该功能。
  2. Fade out all the elements with class "image". 淡出“ image”类的所有元素。
  3. Fade in element with class "image" and id of the "portfolioImage" (lets say the id of portfolioImage is 3, so fade in the elements that has class image and id 3). 淡入具有类“ image”和“ portfolioImage”的id的元素(可以说PortfolioImage的id为3,因此淡入具有class image和id 3的元素)。
  4. Fade out an element with id "portfolioHolder" and fade in another element with class "details". 淡出ID为“ portfolioHolder”的元素,淡入为类“ details”的另一个元素。

Parenting: #portfolioHolder (visible at the start of the function) .portfolioImage #details (invisible at the start of the function) .image 育儿:#portfolioHolder(在函数开始时可见).portfolioImage #details(在函数开始时不可见).image

My Code: 我的代码:

$(function(){
$("div.portfolioImage").click(function(){
    var id = $(this).id;
    window.print($(this).id);
    $("div#portfolioHolder").fadeOut('slow', function() {
        // Animation complete.
    });
    $("div#details").fadeIn('slow', function() {
        // Animation complete.
    });
    $("div.image").each.fadeOut('slow', function(){/*complete*/});
    $("div.image#"+id.toString()).fadeIn('slow', function(){/*complete*/});
});

}); });

-- David - 大卫

You have couple of issues in your code. 您的代码中有几个问题。

  1. You can retreive the id using this.id or $(this).attr('id') 您可以使用this.id$(this).attr('id')获取ID
  2. $("div.image").each.fadeOut will throw an error because each is a method and you are trying to use it as a property and calling fadeOut method on it. $("div.image").each.fadeOut将引发错误,因为each方法都是一种方法,并且您试图将其用作属性并在其上调用fadeOut方法。 You don't have to use each here because $("div.image").fadeOut() will take care of running fadeOut on all the selected elements. 您不必在这里使用each ,因为$("div.image").fadeOut()将照顾在所有选定元素上运行fadeOut情况。

Try this 尝试这个

$(function(){
   $("div.portfolioImage").click(function(){
      var id = this.id;

      $("div#portfolioHolder").fadeOut('slow', function() {
          // Animation complete.
      });
      $("div#details").fadeIn('slow', function() {
          // Animation complete.
      });
      $("div.image").fadeOut('slow', function(){/*complete*/});
      $("div.image#"+id).fadeIn('slow', function(){/*complete*/});
  });
});

IDs should be unique! ID应该是唯一的! But you can use data-id or any data-X instead... 但是您可以使用data-id或任何data-X代替...

Instead of: 代替:

<div class="portfolioImage" id="3">...</div>
<div class="image" id="3">...</div>

use: 采用:

<div class="portfolioImage" id="image3">...</div>
<div class="image" data-id="image3">...</div>

and the JS: 和JS:

$('.portfolioImage').click(function(){
   var id = $(this).attr('id');

   $('.image').fadeOut('slow');
   $('.image[data-id='+id+']').fadeIn('slow');
   $('#portfolioHolder').fadeOut('slow');
   $('.details').fadeIn('slow');
});

should be right 应该是对的

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

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