javascript - 使用 JavaScript 编辑 CSS 渐变

标签 javascript css firefox browser linear-gradients

我正在通过 Firefox 中的 JavaScript 编辑 CSS 渐变。 我有用户可以放置的输入框 1. 定位 2. 第一种颜色 3. 第二种颜色

这是html

<html>
    <head>
        <title>Linear Gradient Control</title>
        <script>
            function renderButton(){ 
            var orientation = document.getElementById("firstValue").value;
            var colorOne = document.getElementById("firstColor").value;
            var colorTwo = document.getElementById("secondColor").value;
            //alert(orientation);
            //alert(colorOne);
            //alert(colorTwo);

            };
        </script>
        <style>
            #mainHolder
            {
            width:500px;
            background: -moz-linear-gradient(left,  green,  red);

            }
        </style>
    </head>
    <body>
        <h1>Gradient Editor</h1>
        <form>
            <input type="text" id="firstValue">orientation</input><br />
            <input type="text" id="firstColor">first color</input><br />
            <input type="text" id="secondColor">second color</input><br />
        </form>
        <button type="button" onclick="renderButton()">Render</button>
        <div id="mainHolder">Content</div>
    </body>
</html>

总而言之,用户将指定他们的 3 个值,这些值将传递给函数“renderButton();”。我可以使用哪一行来更改 CSS3 渐变的 3 个值,以便用户可以制作自己的自定义渐变框?我假设它只需要一两行。

附言我意识到这个例子只适用于 Firefox。在使用不同的浏览器之前,我只是想了解一下这个概念。

最佳答案

从类似下面的内容开始:

var dom = document.getElementById('mainHolder');
dom.style.backgroundImage = '-moz-linear-gradient('
        + orientation + ', ' + colorOne + ', ' + colorTwo + ')';

如果您需要支持比 Firefox 更多的浏览器,这将需要结合浏览器嗅探或一些类似 Modernizr 的功能检测来完成。

下面是如何使用类似于 Modernizr 的功能检测(在 Firefox、Chrome、Safari、Opera 中测试)来完成此操作的示例。

// Detect which browser prefix to use for the specified CSS value
// (e.g., background-image: -moz-linear-gradient(...);
//        background-image:   -o-linear-gradient(...); etc).
//

function getCssValuePrefix()
{
    var rtrnVal = '';//default to standard syntax
    var prefixes = ['-o-', '-ms-', '-moz-', '-webkit-'];

    // Create a temporary DOM object for testing
    var dom = document.createElement('div');

    for (var i = 0; i < prefixes.length; i++)
    {
        // Attempt to set the style
        dom.style.background = prefixes[i] + 'linear-gradient(#000000, #ffffff)';

        // Detect if the style was successfully set
        if (dom.style.background)
        {
            rtrnVal = prefixes[i];
        }
    }

    dom = null;
    delete dom;

    return rtrnVal;
}

// Setting the gradient with the proper prefix
dom.style.backgroundImage = getCssValuePrefix() + 'linear-gradient('
        + orientation + ', ' + colorOne + ', ' + colorTwo + ')';

关于javascript - 使用 JavaScript 编辑 CSS 渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15071062/

相关文章:

javascript - 将 JavaScript 变量传递给 Flash

javascript - 如何在更改 JSON 文件后将其重新加载为模块

javascript - 如何从节点列表访问 HTML 元素的内容?

html - 为什么内联视频 src 媒体查询不起作用?

html - 脚本中的双连字符使 firefox 呈现奇怪

javascript - JSON.解析 : unexpected character at line 1 column 1 of the JSON data in Firefox only

java - webdriver 无法单击 Firefox 中的超链接

javascript - 是否可以让 webpacks System.import 使用 ajax(用于进度事件)?

html - 使下拉停留在:hover is no longer active之后

php - HTML 输入文本框异常行为