简体   繁体   English

如何从元素的样式中提取单个 CSS 过滤器函数?

[英]How to extract individual CSS filter functions from an element's style?

I have an image on my HTML page with some filters applied to it.我的 HTML 页面上有一张图像,上面应用了一些过滤器。 Its style attribute might look like this:它的style属性可能如下所示:

filter: brightness(85%) contrast(136%) saturate(122%) drop-shadow(0 0 5px #000000) grayscale(25%)

What I want to do is to extract individual style functions from this string, and store them into an object or an array, like:我想做的是从这个字符串中提取单个样式函数,并将它们存储到 object 或数组中,例如:

{ "brightness": "80%", "contrast": "136%" /* and so on ... */ }

or:或者:

[["brightness", "80%"], ["contrast", "136%"] /* and so on ... */ ]

Is there an easy way of doing this?有没有一种简单的方法可以做到这一点?

  1. get the value of the filter in css as a string以字符串形式获取 css 中过滤器的值
  2. get the position of every filter in the styles string获取 styles 字符串中每个过滤器的 position
  3. get the array of filters and the position in the styles string获取过滤器数组和 styles 字符串中的 position
  4. order the previous array by the index (the position)按索引(位置)对前一个数组进行排序
  5. build the pairs filter, value.构建对过滤器,值。

I hope this is what you are asking for.我希望这是你所要求的。

 let para = document.querySelector('p');// the filtered element let s = window.getComputedStyle(para);//get the style for the filtered element let theFilter = s.getPropertyValue("filter");//get the value of the filter // the array of all the filters in css let filters = ["blur","brightness","contrast","drop-shadow","grayscale","hue-rotate","invert","opacity","saturate","sepia","url"]; // an empty array let ry = []; filters.forEach((f,i)=>{ let oF = theFilter.match(f); if(oF){ ry.push({prop:oF[0],index:oF.index}) } }) // ry is the array of the filters and the position in theFilter string [{prop: "brightness", index: 0},{prop: "contrast", index: 17}... function compareNumbers(a, b) { return a.index - b.index; } // order the ry array by index let sortedry = ry.sort(compareNumbers); // the object with the filters let oFilters = {} for(let i = 0; i < sortedry.length; i++){ let sbstr = (i+1 < sortedry.length)? theFilter.substring(sortedry[i].index,sortedry[i+1].index).trim(): theFilter.substring(sortedry[i].index).trim() let value = sbstr.substring(sbstr.indexOf("(")+1, sbstr.length-1); oFilters[sortedry[i].prop] = value; } console.log(oFilters)
 p{filter: brightness(85%) contrast(136%) saturate(122%) drop-shadow(0 0 5px #000000) grayscale(25%)}
 <p>The filtered element</p>

You might do something like this.你可能会做这样的事情。

    var element = document.getElementById("myElement");
    var out = "";

    var elementStyle = element.style;
    var computedStyle = window.getComputedStyle(element, null);

    for (prop in elementStyle) {
      if (elementStyle.hasOwnProperty(prop)) {
        out += "  " + prop + " = '" + elementStyle[prop] + "' > '" + computedStyle[prop] + "'\n";
      }
    }

then store only those that the prop is filter然后只存储那些道具是过滤器的

the code come directly from MDN代码直接来自MDN

https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style

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

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