【问题标题】:Preventing animation based on boolean防止基于布尔值的动画
【发布时间】:2012-11-11 20:09:27
【问题描述】:

你们中的许多人都知道,在<IE8 中使用 alpha 透明度为 PNG 制作动画效果并不好。因此,我只想在 <IE8 中禁用动画不透明度。

我能看到这样做的唯一方法是这样做:

HTML

<!--[if lt IE 8]>
    <script type="text/javascript">
        var browserUnsupported = true;
    </script>
<![endif]-->

JAVASCRIPT

// Test if the browser is supported
if(browserUnsupported){
    // Hide the element
    $('#test').hide();
}else{
    // Fade out the element and then hide it
    $('#test').animate({
        opacity:0   
    },200,function(){
        $(this).hide();
    })
}

// Once the animation has completed, continue
setTimeout(function(){
    // This will execute once the animation has completed
    // We cannot use the callback in case the browser is unsupported
    // and the animate function has not been used
},200);

但这是一个相当冗长的修复,特别是如果您认为每次我想制作动画时,我都必须这样做。

谁能想出更好的选择?

您能否在 Internet Explorer 8 及更低版本中关闭动画不透明度?如果有黑客攻击,请考虑我没有在本地存储我的 jQuery,我从 Google CDN 加载它。即使这是唯一的选择,我也不愿意更改 jQuery 源代码。

更新

我不是在寻找更好的方法来检测浏览器版本。上面的代码仅用于说明目的。我想知道的是是否有一种方法可以控制 jQuery 动画的不透明度?或者,是否有更好的方法来为 if 条件设置动画,如果不是,请不要设置动画?

更新

类似这样的东西(未测试):

function myAnimate(styles,duration,easing,callback){
    // Get the selected element
    var element = this.elements;
    if($.browser.msie && $.browser.version<8){
        delete options.opacity;
    }
    // Call the animation method
    element.animate(options,duration,easing,callback);
}

【问题讨论】:

  • 这个问题已经得到解答,显然 IE 确实支持在 PNG 中设置 alpha 透明度动画。查看stackoverflow.com/a/4126528/861940
  • @Bruno 再次,请详细阅读问题。这不是正确的解决方法。 CSS背景图片呢?我已经确定背景图像无法在 IE 中正确设置动画,无论是否有修复。我只是想禁用它

标签: javascript jquery internet-explorer animation opacity


【解决方案1】:

例如,使用 jQuery 中的 $.browser$.browser.version()

if($.browser.msie && $.browser.version < 8) {
 //fallback
} else {
 //real code
}

编辑

您可以编写自己的执行函数,扩展 jQuery 以根据条件运行函数。如果你的条件不是 Internet Explorer,那么你可以确保在 IE 中不会调用此类函数。执行函数可能看起来像这样(虽然没有测试过)

$.fn.execute = function(a) {
    return this[a].apply(this, [].slice.call(arguments, 1));
}

condition = $.browser.msie;
$('div').execute(condition ? 'true' : 'false', function() {...});

【讨论】:

  • 检测浏览器是否支持不透明度不是这里的问题!如果我知道浏览器不受支持,我正在尝试提出一种防止浏览器动画的好方法
  • 请看我的更新。目前这个答案与我的实际问题无关,无论信息是否有帮助。如果您能够用问题的答案更新它,我将删除反对票并接受答案
  • 仍然不确定我的问题是否正确,但我已经编辑了答案。
  • 已经删除了downvote :-),它与我正在寻找的类似,但我想我已经做到了。将在一分钟内发布
  • jQuery 中的 browser 对象是否完全受支持且可靠,还是最近的插件?
【解决方案2】:

$().fadeOut 完全符合您的要求 - 为不透明度设置动画然后隐藏:

if(ie){
  $("#test").hide()
}else{
  $("#test").fadeOut(200)
}

如果你喜欢简洁的代码,你可以这样做

$("#test").fadeOut(supported && 200)

或者,您可以disable all animations globally

if(ie) $.fx.off = true;

或者将你的逻辑封装在一个你甚至可以添加到 jQuery 中的函数中:

$.fn.fadeIfSupported = function(){
  if(unsupported){
    this.hide()
  }else{
    this.fadeOut.apply(this, arguments)
  }
}
...
$("#test").fadeIfSupported(200);

【讨论】:

  • 我不只是为不透明度设置动画,这只是一个例子。否则,修复将非常简单,如您所示。 FadeOut 不适合。但是你的建议给了我一个想法来重写动画方法。这可能就是我会做的:-)
  • @BenCarey 您仍然可以使用您的实现扩展 jQuery,因此您只需编写一次
  • 我把我的意思贴出来,我不是说完全重写 animate 方法。
猜你喜欢
  • 2014-10-22
  • 1970-01-01
  • 2023-01-02
  • 2022-12-13
  • 2015-12-21
  • 2023-04-05
  • 2012-07-08
  • 2021-11-07
  • 1970-01-01
相关资源
最近更新 更多