【问题标题】:jquery .animate a display in switch mode whith the ? characteresjquery .animate a display in switch mode with the ?人物
【发布时间】:2014-07-17 18:20:49
【问题描述】:

我需要在一个块上做一个过渡动画,我需要我的块在第一次点击时移动和display:none,然后在第二次点击时向后移动和display:block。

我用这个来做移动效果:

$('#bthplus').click(function() {
    var $lefty = $('#plushome');

    $lefty
    .animate({
       right: parseInt($lefty.css('right'),10) == 0 ?
  -$lefty.outerWidth() :
  0 

});

效果很好。对于可访问性,我需要 display:none 块 #plushome 当不在我的视野中时

我试过这个:

  $('#bthplus').click(function() {
    var $lefty = $('#plushome');

    $lefty
    .animate({
       right: parseInt($lefty.css('right'),10) == 0 ?
  -$lefty.outerWidth() :
  0 

})
    .animate({
    display: $lefty.css('display') == "none" ?
  $lefty.css('display'):
  "block"
});
  });

方块仍在移动,但显示屏没有任何变化。有人知道为什么吗?


更新

我读到不能在这个动画函数中调用显示,只能调用不透明度,但这并不能解决我的问题,因为屏幕阅读器仍然可以看到隐藏的元素,所以我认为我需要使用show() 和hide() 函数,但是我怎样才能进行相同的测试:

  if( $lefty.css("display") == "none" ){

     $lefty.show();
}else{
$lefty.hide();
};

更新

好的,所以我的最后一次编辑成功了,我自己找到了答案。我还有一个小错误:

新代码是

 $('#bthplus').click(function() {
    var $lefty = $('#plushome');

    $lefty
    .animate({
       right: parseInt($lefty.css('right'),10) == 0 ?
  -$lefty.outerWidth() :
  0 

});
 if( $lefty.css("display") == "none" ){

 $lefty.show();

}else{

  $lefty.hide();
};
  }); 

因为我的元素在开始时是display:none,但由于位置好(right:0),他在就位时被隐藏,而在他离开屏幕时display:block。我会尝试解决这个问题:)

【问题讨论】:

  • “MAJ”(法语中的 Mise à Jour)的翻译是“更新”

标签: jquery animation jquery-animate accessibility


【解决方案1】:

您可以使用 jQuery is 和 visible selector

这应该适合你:

function _toggleLefty() {
    var _isVisible = $lefty.is(":visible");
    var _right = parseInt( $lefty.css('right'), 10 );
    // Show element to do animation
    if (!_isVisible) {
        $lefty.css({
            "display": "block",
            "opacity": 0
        });
    }
    // Do animation
    $lefty.animate({
        "opacity": _isVisible ? "0" : "1",
        "right": _isVisible ? "-"+$lefty.outerWidth()+"px" : 0
    });
    // Element is visible so we hide it after animation
    if (_isVisible) {
        // We promise this action after it's complete (could use complete option too)
        $lefty.promise().done(function(){
            $lefty.css("display", "none");
        });
    }
}

您不能使用动画将显示更改为无,但您可以伪造它。

您的代码有一些小问题:

  • -$lefty.outerWidth() 不起作用,你必须这样做:"-"+$lefty.outerWidth()
  • 需要将整数改为px:"-"+$lefty.outerWidth()+"px" : 0)

现在您可以拨打_toggleLefty(); 来制作动画了。

您可能还需要检查以确保 _right 不是 NAN(不是数字)

稍微解释一下:

// Show element to do animation 部分允许元素可见,但不透明度保持为 0,因此用户不会看到它。这允许您伪造动画以显示无,因为这会为不透明度和正确的运动设置动画。

我检查if (_isVisible) 的最后一部分是动画完成后强制显示为无的出口。

你可以看到它在工作here

请注意,您无需担心 _right 我把它留在那里以防万一您想使用它,但它很容易影响可见性。

更新了演示以复制您的效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-27
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2022-12-27
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    相关资源
    最近更新 更多