【问题标题】:jQuery .animate issuejQuery .animate 问题
【发布时间】:2013-03-30 20:21:26
【问题描述】:

我有以下代码。

           if ($(checkboxID)[0].checked == false) {
                $(this).animate({
                    'background-position-x': '0%',
                    'background-position-y': '0%'
                }, 200, 'linear');

                $(checkboxID)[0].checked = true;
                $(this).removeClass('off').addClass('on');

            } else {
                $(this).animate({
                    'background-position-x': '100%',
                    'background-position-y': '0%'
                }, 200, 'linear');

                $(checkboxID)[0].checked = false;
                $(this).removeClass('on').addClass('off');

            }

如您所见,if 两边的代码是相同的,但如果为 true,则无论我增加多少持续时间,jquery 都不会为过渡设置动画。 你们中的任何人都可以看到我错过的错误吗?

编辑:这里也是 css:

    .checkbox-style  {
    display: block;
    width: 87px;
    height: 28px;
    background: url('images/check-square.png') no-repeat;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    overflow: hidden;
    cursor: pointer;
   }
   .on {
    background-position: 0% 0%;
   }
   .off {
    background-position: 100% 0%;
   }

【问题讨论】:

标签: jquery jquery-animate background-position


【解决方案1】:

我会猜测一下,但我认为 off 类的元素是不可见的。由于类更改立即执行,您没有时间看动画。

您可能希望在动画结束时更改类。为此,将回调传递给animate,动画完成时会调用它:

           $(this).animate({
                'background-position-x': '100%',
                'background-position-y': '0%'
            }, 200, 'linear', function(){
               $(checkboxID)[0].checked = false;
               $(this).removeClass('on').addClass('off');
            });

【讨论】:

  • 这是一个 div 替换复选框。这是那些 .checkbox-style { display: block; 的 css宽度:87px;高度:28px;背景: url('images/check-square.png') 无重复; -webkit-border-radius:5px; -moz-边界半径:5px;边框半径:5px;溢出:隐藏;光标:指针; } .on { 背景位置:0% 0%; } .off { 背景位置:100% 0%; }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多