javascript - 如何获取每个效果的过滤值?

标签 javascript css function dom css-filters

如何使用 %px 获取每个效果的过滤器值,并且仅使用没有 %px 的数字.

在编解码器中我列出了 3 个过滤器(当然可能还有更多)。

最好的方法是什么?

<button onclick="myFunction()">Try it</button><br><br>

<img id="myImg" 
     src="https://www.roadrunnerrecords.com/sites/g/files/g2000005056/f/sample-4.jpg"  
     width="300"
     height="300"
     style="filter:grayscale(100%) blur(5px) brightness(150%)" />
function myFunction() {
  var grayscale = document.getElementById("myImg").style.filter;
  var blur = document.getElementById("myImg").style.filter;
  var brightness = document.getElementById("myImg").style.filter;
  alert("grayscale value = , blur value= , brightness value= "); //without % and px
  alert("grayscale value = , blur value= , brightness value= "); //with % and px
}
function myFunction() {
var effects = document.getElementById("myImg").style.filter.split(" ");
var imgFilter = {};
for (var i = 0; i < effects.length; ++i) {
    var split = effects[i].split("(");
    imgFilter[split[0]] = split[1].substring(0,split[1].length-1);
}
alert("grayscale value = "+imgFilter.grayscale+" , blur value=  "+imgFilter.blur+", brightness value=  "+imgFilter.brightness);//with % and px
alert("grayscale value = "+imgFilter.grayscale.replace(/[^\d.]/g,"")+" , blur value=  "+imgFilter.blur.replace(/[^\d.]/g,"")+", brightness value=  "+imgFilter.brightness.replace(/[^\d.]/g,""));//without % and px
} // How to add Hue-rotate???

最佳答案

使用字符串解析技术分离出所需的部分并创建一个对象:

function myFunction() {
    var element = document.getElementById("myImg");
    
    // split filter string into an array of effects
    var effects = element.style.filter.split(" ");
    var imgFilter = {};
    for (var i = 0; i < effects.length; ++i) {
        // use regex to match value before parenthesis and value inside
        var matches = effects[i].match(/(.*)\((.*)\)/);
        // create a key with the effect name (ex. "grayscale")
        imgFilter[matches[1]] = {};
        // set the withUnits value to the number that is in the parenthesis
        imgFilter[matches[1]]["withUnits"] = matches[2];
        // remove characters that are not digits or periods using regex
        imgFilter[matches[1]]["withoutUnits"] = matches[2].replace(/[^\d.]/g,"");
    }
    
    //with % and px
    for (var i = 0, log = ""; i < Object.keys(imgFilter).length; ++i) {
      log += Object.keys(imgFilter)[i] + " value = " + imgFilter[Object.keys(imgFilter)[i]].withUnits + ", ";
    }
    alert(log);
    
    //without % and px
    for (var i = 0, log = ""; i < Object.keys(imgFilter).length; ++i) {
      log += Object.keys(imgFilter)[i] + " value = " + imgFilter[Object.keys(imgFilter)[i]].withoutUnits + ", ";
    }
    alert(log);
}
<button onclick="myFunction()">Try it</button><br><br>

<img id="myImg" 
     src="https://www.roadrunnerrecords.com/sites/g/files/g2000005056/f/sample-4.jpg"  
     width="300"
     height="300"
     style="filter:grayscale(100%) blur(5px) brightness(150%) hue-rotate(180deg)" />

对于效果:“灰度(100%)模糊(5px)亮度(150%)”,创建的对象imgFilter具有以下值:

{
    "grayscale": {
        "withUnits": "100%",
        "withoutUnits": "100"
    },
    "blur": {
        "withUnits": "5px",
        "withoutUnits": "5"
    },
    "brightness": {
        "withUnits": "150%",
        "withoutUnits": "150"
    }
}

您可以通过使用例如 imgFilter.grayscale.withUnits 来获取 "100%"imgFilter.blur.withoutUnits 来访问任何特定值code> 得到 "5"

要访问包含连字符的效果(例如 hue-rotate),您需要使用引号和方括号访问值,例如 imgFilter["hue-rotate"] .withUnits.

将色调旋转添加到您在编辑中使用的版本:

function myFunction() {
var effects = document.getElementById("myImg").style.filter.split(" ");
var imgFilter = {};
for (var i = 0; i < effects.length; ++i) {
    var split = effects[i].split("(");
    imgFilter[split[0]] = split[1].substring(0,split[1].length-1);
}
alert("hue-rotate value = "+imgFilter["hue-rotate"]+" , grayscale value = "+imgFilter.grayscale+" , blur value=  "+imgFilter.blur+", brightness value=  "+imgFilter.brightness);//with % and px
alert("hue-rotate value = "+imgFilter["hue-rotate"].replace(/[^\d.]/g,"")+" , grayscale value = "+imgFilter.grayscale.replace(/[^\d.]/g,"")+" , blur value=  "+imgFilter.blur.replace(/[^\d.]/g,"")+", brightness value=  "+imgFilter.brightness.replace(/[^\d.]/g,""));//without % and px
}
<button onclick="myFunction()">Try it</button><br><br>

<img id="myImg" 
     src="https://www.roadrunnerrecords.com/sites/g/files/g2000005056/f/sample-4.jpg"  
     width="300"
     height="300"
     style="filter:grayscale(100%) blur(5px) brightness(150%) hue-rotate(180deg)" />

关于javascript - 如何获取每个效果的过滤值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61078137/

相关文章:

javascript计算器到小数位和图像作为按钮

javascript - 如果第 4 个父级 hasClass,则将类添加到 DIV

html - Bootstrap 将头部卡住在 table 上

python - 具有自动换行功能的 Python 文字处理函数

javascript - 这是命名约定还是函数声明

javascript - ng-disabled 设置正确,但不影响 HTML 禁用属性

javascript 解除绑定(bind) dom 事件

css - 如何在&lt;header&gt;元素前显示div

jquery - 宽度 : calc(auto + 50px); doesn't work

python - 如何有效地按元素评估整个 Python 函数列表?