2

我在这里有这个测试: //jsbin.com/epihis/2/edit

我有三个段落在单击时一个接一个地向上滑动。一旦所有的段落都被隐藏了,我想显示一个按钮来滑动它们。

我怎么做?

<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
   $(document).ready(function(){ 

     //jQuery mouseover function
       $("p#hovercolor").mouseover(function(){
        $("p#hovercolor").css("background-color","yellow");
       });

     //jQuery mouseout function
      $("p#hovercolor").mouseout(function(){
        $("p#hovercolor").css("background-color","green");
      });

     //jQuery click function
    $('input[name="Colorbtn"]').click(function(){
        $('.myBox').css('background', '#00aeef');
        $('.myBox').css('color', '#fff');
     });

     $('input[name="discolorbtn"]').click(function(){
        $('.myBox').css('background', '#fff');
        $('.myBox').css('color', '#000');
     });

     //Hide the matched elements with a sliding motion.
     $('p.sliderhider').click(function(){
       $(this).slideUp(); 
     });


  });    

</script>
</head>
<body>

  <p id="hovercolor">Move the mouse pointer over this paragraph.</p>

  <div class="myBox" style="border:1px solid blue; width:150px; height:150px; margin-bottom:20px;">
    BOX 1
  </div>

  <input type="button" value="Color myBox" name="Colorbtn"/>
  <input type="button" value="Dis-Color myBox" name="discolorbtn"/><br /><br />
  <hr>
  <p class="sliderhider" style="display:block; background: yellow; width:200px">Slide this up one at a time</p>
  <p class="sliderhider" style="display:block; background: yellow; width:200px">Slide this up one at a time</p>
  <p class="sliderhider" style="display:block; background: yellow; width:200px">Slide this up one at a time</p>

</body>
</html>
4

3 回答 3

3

尝试:

 $('p.sliderhider').click(function () {
     $(this).slideUp(function () {
         if ($('p.sliderhider:visible').length === 0) {
             $('hr').after('<button id="show">Button</button>')
         };
     });
 });
 $(document).on('click', '#show', function () {
     $('p.sliderhider').show();
 })

jsFiddle 示例

在 slideUp 的回调中,您可以检查可见段落的长度,当没有剩余时,生成按钮以再次显示它们。

于 2013-05-31T19:25:50.350 回答
1

jQuery 允许你传递一个回调,一旦动画完成就会触发它:

$('p.sliderhider').click(function(){
    $(this).slideUp({'complete': function() {
        // show "slide down" button here
    }}); 
});
于 2013-05-31T19:23:08.460 回答
1

slideUp回调中写一个条件来检查是否全部p.sliderhider隐藏。

//Hide the matched elements with a sliding motion.
 $('p.sliderhider').click(function(){
   $(this).slideUp({'complete': function () {
     if ($('p.sliderhider:visible').length == 0) {
       $('#showPara').show();
     }        
   }}); 
 });

 $('#showPara').click(function (){
    $('p.sliderhider').slideDown();
 });

如果是,请显示slideDown所有可能的按钮.sliderhider

演示:http: //jsbin.com/epihis/5

于 2013-05-31T19:29:22.517 回答