【问题标题】:Jquery animate + if css positionJquery animate + if css position
【发布时间】:2013-06-01 05:50:08
【问题描述】:

我实际上是在尝试使用 jquery animate 函数检查我正在移动的 div 元素的顶部位置。

$('#nss .nss-pager-up').click(function() {
    $('.nss-stream-inner-wrap').animate({
        top: '+=55'
    }, 300, function() {
        if($('.nss-stream-inner-wrap').css('top') == '55') {
            alert('hey');
        }
 });
});

有人知道为什么这不起作用?

【问题讨论】:

  • 你检查过css属性top的值吗?它可能是“55px”。

标签: jquery if-statement jquery-animate position


【解决方案1】:

返回值包含单位,像素:

if ( $('.nss-stream-inner-wrap').css('top') == '55px' ) {

http://jsfiddle.net/cY5vw/

【讨论】:

    【解决方案2】:

    可能是“顶部”样式尚未设置。

    我会选择以下方式

    $('#nss .nss-pager-up').click(function() {
        $('.nss-stream-inner-wrap').animate({
            top: $(this).position().top + 55
     }, 300, function() {
            if($('.nss-stream-inner-wrap').css('top') == '55') {
                alert('hey');
            }
     });
    });
    

    【讨论】:

      【解决方案3】:

      使用位置()

      http://jsfiddle.net/ySYWB/1/

      $('#nss .nss-pager-up').click(function() {
          $('.nss-stream-inner-wrap').animate({
              top: '+=50'
          }, 300, function() {
          if($('.nss-stream-inner-wrap').position().top > 55) {
              alert('hey');
          }
        });
      });
      

      【讨论】:

      • > 通常更安全,但你可以坚持 ==
      猜你喜欢
      • 2014-06-08
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      相关资源
      最近更新 更多