简体   繁体   English

如何使jQuery动画在主要浏览器上正常工作?

[英]How do I make a jQuery animation work across major browsers?

I have a bit of code that works in all major browsers save internet explorer and a animation effect that works only in Firefox. 我有一些适用于所有主要浏览器的代码,除了Internet Explorer以外,还具有仅在Firefox中有效的动画效果。 I am not real sure on how to even begin fixing this problem as it is fairly basic code. 我不太确定如何开始解决此问题,因为它是相当基本的代码。 I will post the code snippets, first the html in the original rendering and then the jquery code used to modify the html dynamically. 我将发布代码片段,首先发布原始渲染中的html,然后发布用于动态修改html的jquery代码。

The html on load starts with visibility hidden. 加载的html以隐藏的可见性开始。

<h3 class="infobox" name="infobox"><p>text</p><p>text</p></h3>

These next 2 pieces of jquery from hover (enter and leave) for mouse related behavior. 悬停(进入和离开)的这两个接下来的jquery用于鼠标相关行为。

Enter: 输入:

$(".infobox").css("visibility", "initial");
$(".infobox").html("<p>Location: " + 
        "<span style=\"color: " + (possiblemove[x] ? "green" : "red") +     "\">" + indexcoords[x] + "</span></p>" + 
        "<p>Number of possible moves: " + possiblemoves + "&nbsp;&nbsp;&nbsp;Move count: " + nummoves + "</p>");

Leave: 离开:

$(".infobox").html("<p>text</p><p>text</p>");
$(".infobox").css("visibility", "hidden");

Animate: 动画:

for (var x = 0; x < 8; x++)
    if (x % 2 == 0)
        $(square).animate({backgroundColor: color}, 50);
    else
        $(square).animate({backgroundColor: 'red'}, 50);

$(square).animate({backgroundColor: color}, 50);

The web page with the questionable behavior: 具有可疑行为的网页:

http://freethecube.com/kt/ http://freethecube.com/kt/

As you can see the highlighting effect of the board bits works in all major browsers. 如您所见,板位的突出显示效果在所有主流浏览器中均有效。 The info box at the top works in all but internet explorer. 除Internet Explorer之外,顶部的信息框均适用。 And the animate flashing for bad moves only works in firefox (have not tested this specifically in safari). 动画闪烁的不良动作仅在Firefox中有效(尚未在野生动物园中进行过专门测试)。

The general thing in common between these elements are the css modifications explicitly. 这些元素之间的共同点是显式的CSS修改。 The full css and jquery being used can be easily viewed by viewing the source of the page and clicking on the css file at the top and the js file at the bottom. 通过查看页面的源代码并单击顶部的CSS文件和底部的js文件,可以轻松查看正在使用的完整CSS和jQuery。

So what do I need to do to get these seemingly small css issues to work across all major browsers? 那么,我要怎么做才能使这些看似很小的CSS问题在所有主要浏览器上都能正常工作?

ps I wrote this specifically simple in html for 2 reasons: ps我用html写这个特别简单的原因有两个:

It is trust worthy code that anyone can see. It is actually a game and nothing else.
And easy portability into my wordpress site.

Jquery files being used: 使用的jQuery文件:

jquery.js 的jquery.js

jquery-animate.js jQuery的animate.js

If there is a newer version of the animate, please let me know. 如果有较新版本的动画,请告诉我。

Thx 谢谢


Addendum 附录

The problems that I was having above did not stem from the CSS manipulation as I originally thought. 我上面遇到的问题并非源于我最初的想法。 It was indeed the CSS hook I was using. 确实是我正在使用的CSS挂钩。 It was an older hack to fix the various versions of backgroundcolor there were. 修复存在的backgroundcolor的各种版本是一种较老的技巧。 Such as: var bg = elem.currentStyle["backgroundColor"]; 如: var bg = elem.currentStyle["backgroundColor"]; and var bg = document.defaultView.getComputedStyle(elem, null).getPropertyValue("background-color"); var bg = document.defaultView.getComputedStyle(elem, null).getPropertyValue("background-color"); . My whole issue was simply the difference in different browsers handly of RGB and RGBA color formats. 我的整个问题只是RGBRGBA颜色格式在不同浏览器中的区别。 My function would have been fine before RGBA , but not after. 我的功能在RGBA之前可以正常使用,但之后则可以。

The full css hook function I was using: 我正在使用的完整的css hook功能:

$.cssHooks.backgroundColor = 
{
    get: function(elem) 
    {
        if (elem.currentStyle)
            var bg = elem.currentStyle["backgroundColor"];        
        else if (window.getComputedStyle)
            var bg = document.defaultView.getComputedStyle(elem, null).getPropertyValue("background-color");

        if (bg.search("rgb") === -1)
            return bg;
        else 
        {
            bg = bg.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);

            function hex(x) 
            {
                return ("0" + parseInt(x).toString(16)).slice(-2);
            }
            return "#" + hex(bg[1]) + hex(bg[2]) + hex(bg[3]);
        }
    }
};

The new and improved version: May still not be perfect but it is a lot closer. 新的和改进的版本: May still not be perfect but it is a lot closer.

$.cssHooks.backgroundColor = 
{
    get: function(elem) 
    {
        if (elem.currentStyle)
            var bg = elem.currentStyle["backgroundColor"];        
        else if (window.getComputedStyle)
            var bg = document.defaultView.getComputedStyle(elem, null).getPropertyValue("background-color");

        if (bg.search("rgb") === -1)
            return bg;
        else 
        {
            bg = bg.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i);

            return (bg && bg.length === 4) ? "#" +
                ("0" + parseInt(bg[1],10).toString(16)).slice(-2) +
                ("0" + parseInt(bg[2],10).toString(16)).slice(-2) +
                ("0" + parseInt(bg[3],10).toString(16)).slice(-2) : '';
        }
    }
};

Please up vote Strelok's answer because he hit the nail on the head. 请斯特雷洛克(Strelok)的答案投赞成票,因为他的头很钉子。 :) :)

A couple of things: 几件事情:

  1. include jQuery before bootstrap 引导程序之前包含jQuery
  2. IE (any version) does not support initial value for the visibility css property. IE(任何版本)不支持visibility CSS属性的initial值。 Just use visible . 只需使用visible
  3. At least in Chrome you have a problem in line 209 of game.js where the regex doesn't match anything and bg is null, so you need to handle this case and return the color or handle the case properly. 至少在Chrome中,您在game.js的第209行遇到问题,其中正则表达式不匹配任何内容,并且bg为null,因此您需要处理这种情况并返回颜色或正确处理情况。 It happens when the property value is rgba(0, 0, 0, 0) you need to handle rgba. 当属性值为rgba(0, 0, 0, 0)您需要处理rgba。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM