简单淡入 jQuery

simple fadeIn jQuery

我正在尝试使用 jQuery 制作一个简单的 fadeIn fadeOut,但它似乎不起作用..

$("h1").append("<p>This is new.</p>");

// With the element initially hidden, we can show it slowly:
$("#clickme").click(function() {
  $("#square").fadeIn("slow", function() {
    // Animation complete
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="clickme">
  Click me
</div>
<img id="square" src="https://cdn.glitch.com/4963558f-bbaf-419b-9695-abdd14691841%2Fsquare.png?1544000277571" alt="" width="100" height="123">

我从http://api.jquery.com/fadein/得到这个(我用方形图片替换了书的图片,因为我没有书的图片)

我开始 JavaScript 和 HTML 并且我不太理解其他 fadeIn jQuery 问题的答案,这就是我问自己的问题的原因。

方块已经褪色了怎么fadeIn

Use FadeIn when the square is non visible and FadeOut when square is visible.

在这个例子中,正方形是 display:none 并且当 "click me" 正方形 fadeIn.

$("h1").append("<p>This is new.</p>");

// With the element initially hidden, we can show it slowly:
$("#clickme").click(function() {
  $("#square").fadeIn("slow", function() {
    // Animation complete
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="clickme">
  Click me
</div>
<img id="square" style="display:none" src="https://cdn.glitch.com/4963558f-bbaf-419b-9695-abdd14691841%2Fsquare.png?1544000277571" alt="" width="100" height="123">

你应该从 display:none 开始 #square 然后你可以 fadeIn

$("h1").append("<p>This is new.</p>");
    
// With the element initially hidden, we can show it slowly:
$( "#clickme" ).click(function() {
  $( "#square" ).fadeIn( "slow", function() {
    // Animation complete
  });
});
#square {display:none}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="clickme">
  Click me
</div>
<img id="square" src="https://cdn.glitch.com/4963558f-bbaf-419b-9695-abdd14691841%2Fsquare.png?1544000277571" alt="" width="100" height="123">

你 fadeIn 只有元素是不可见的。看看下面的脚本

$("h1").append("<p>This is new.</p>");

// With the element initially hidden, we can show it slowly:
$("#clickme").click(function() {
  $element = $("#square");
  if ($element.is(':visible')) {
    // Hide if already visible.
    $element.fadeOut("slow", function() {});
  } else {
    // Show if not yet visible.
    $element.fadeIn("slow", function() {});
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="clickme">
  Click me
</div>
<div id="square">
  <img src="https://cdn.glitch.com/4963558f-bbaf-419b-9695-abdd14691841%2Fsquare.png?1544000277571" alt="" width="100" height="123">
</div>