【问题标题】:Text doesn't start centered when calling jquery's show('slow') and hide('slow')调用 jquery 的 show('slow') 和 hide('slow') 时,文本不会开始居中
【发布时间】:2013-07-31 08:30:29
【问题描述】:

我对这个 jquery show('slow') 和 hide('slow') 有点麻烦。我有一个 div,里面有一个 ajax 加载 gif,当我进行一些处理时会出现,这是一个简单的例子:

       <input type="button" id="btnShow" value="Show"/>
       <input type="button" id="btnHide" value="Hide"/>

       <div id="loading"><img src="~/Content/ajax-loader.gif" /></div>

       <script type="text/javascript">
          $(function () {
              $("#loading").hide();

               $("#btnShow").click(function() {
                  $("#loading").show('slow');
               });
               $("#btnHide").click(function () {
                  $("#loading").hide('slow');
               });
          })
       </script>

      <style type="text/css">
          #loading{
             text-align: center;

          }
      </style>

问题是,内容应该以 div 为中心,但是当我单击“btnShow”时,它从左侧开始,当它完全打开时转到中间......当我隐藏它时会发生同样的事情。 .. 加载 gif 在中间,当它关闭时,它会向左移动……我怎样才能防止这种情况发生?

【问题讨论】:

    标签: jquery html loading center text-align


    【解决方案1】:

    您需要fadeIn 和fadeOut 函数,而不是show 和hide。这应该为你做。只需替换相关代码即可。 =]

    $("#btnShow").click(function() {
        $("#loading").fadeIn('slow');
    });
    $("#btnHide").click(function () {
        $("#loading").fadeOut('slow');
    });
    

    http://jsfiddle.net/cpWaa/

    【讨论】:

    • 是的,成功了!谢谢!唯一的问题是它不再像 show 和 hide 那样平滑过渡
    • @Victor Franchi Zeclhynscki:“平滑过渡”是什么意思。我确信它可以被弄清楚。 =]如果答案解决了你的问题,请记得接受。
    • 好的!只需为此添加一些动画!你的回答很有帮助!谢谢!
    【解决方案2】:

    您应该尝试显式声明丢失 div 的高度和宽度。

    (#)loading{ width:100%; height:50px; }

    【讨论】:

      【解决方案3】:

      如果有人正在寻找这个答案,我找到了解决方案,使用 @AlienHoboken 所说的淡入和淡出并调用 jquery '动画'。这是完整的代码

      <input type="button" id="btnShow" value="Show" />
      <input type="button" id="btnHide" value="Hide" />
      
      <div id="loading"><img src="~/Content/ajax-loader.gif" /></div>
      
      
      <script type="text/javascript">
          $(function () {
      
              $("#btnShow").click(function () {
                  $("#loading").animate({
                      left: '+=50',
                      height: 'toggle'
                  }, 500, function() {
                      $("#loading").fadeIn(300);
                  });
              });
              $("#btnHide").click(function () {
                  $("#loading").animate({
                      left: '+=50',
                      height: 'toggle'
                  }, 500, function () {
                      $("#loading").fadeOut(300);
                  });
              });
          })
      
      </script>
      
      <style type="text/css">
          #loading
          {
              text-align: center;
          }
      </style>
      

      【讨论】:

        【解决方案4】:

        来自JQuery doc:

        当提供持续时间、普通对象或“完整”函数时,.show() 成为 >animation 方法。 .show() 方法同时为匹配的 > 元素的宽度、高度和不透明度设置动画。

        当您在 show() 中指定持续时间时,这是默认行为,即从一侧滑动直到到达其位置。

        我想你正在寻找一个淡入淡出效果,它在 JQuery 中有自己的功能:

           <script>
              $(function () {
                  $("#loading").hide(); #this one you can keep it, is not animated
        
                   $("#btnShow").click(function() {
                      $("#loading").fadein('slow');
                   });
                   $("#btnHide").click(function () {
                      $("#loading").fadeout('slow');
                   });
              })
           </script>
        

        不过,我宁愿建议使用animate() 函数,因为它更完整,您可以更好地控制实际变化:

         <script>
              $(function () {
                  $("#loading").hide());
        
                   $("#btnShow").click(function() {
                      $("#loading").animate({
                          opacity:1, //same as fading in
                          display:"block" //hide() has set display:"none"
                      }, 'slow');
                   });
        
                   $("#btnHide").click(function () {
                      $("#loading").animate({
                          opacity:0, //same as fading out
                          display:"none" //same as hide()
                      }, 'slow');
                   });
              })
           </script>
        

        希望这会有所帮助!

        【讨论】:

        • 帮助了 =) 谢谢
        猜你喜欢
        • 2011-10-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多