简体   繁体   English

Javascript-准确获取元素框阴影详细信息

[英]Javascript - getting element box-shadow details accurately

I have come a bit unstuck.. 我有点不高兴。

Well near enough every css property has seperate parts IE. 每个css属性都具有足够接近的位置,具有独立的部分IE。

margin-left
border-top-color
transition-duration

However, I have searched around a bit and it seem's to me that this is not the case with box-shadow . 但是,我进行了一些搜索,在我看来, box-shadow并非如此。 For example i have 4 <input> 's, the H Spread & V Spread & Blur & Color . 例如,我有4个<input> ,即H Spread V Spread Blur Color and i wan't to change a elements box shadow on change of these fields. 而且我不会在更改这些字段时更改元素框阴影。 After finding out that there isn't any specifics on box shadow, i decided that the best way would be to split the box-shadow into a Array using something like this 在发现盒子阴影没有任何细节之后,我决定最好的方法是使用类似下面的方法将box-shadow分成一个Array

element.style.boxShadow.split(' ');

However the result does not come back in the right order as shown in this Fiddle 但是,此提琴未按正确的顺序返回结果

<div id="test" style="box-shadow: 1px 1px 3px #999;"> Welcome </div>
console.log( element.style.boxShadow.split(' ') );

Is resulting in this output ["rgb(153,", "153,", "153)", "1px", "1px", "3px"] 结果是此输出["rgb(153,", "153,", "153)", "1px", "1px", "3px"]

The color is different and the order has changed which doesn't sound very reliable to me. 颜色不同,顺序已更改,对我来说听起来不太可靠。

Is there a way we can acuratelly get/set the different sections of a box-shadow 有没有一种方法可以我们准确地获取/设置box-shadow的不同部分

I am also using Zepto.js if there is a solution there. 如果那里有解决方案,我也正在使用Zepto.js。

I have tried .css(); 我已经尝试过.css(); = same result. =相同的结果。

I hope this will help you get toward your goal. 我希望这将帮助您实现目标。 I made a regex, that matches what the browser is outputting (the rgb is just the hex as an rgb). 我做了一个正则表达式,它匹配浏览器输出的内容(rgb只是作为rgb的十六进制)。 It may need to be updated since I didn't test it in all browsers, but I think it converts it to rgb so if there is an alpha, that can be handled easily. 由于未在所有浏览器中都进行过测试,因此可能需要对其进行更新,但是我认为它会将其转换为rgb,因此,如果有alpha字母,则可以轻松处理。

Here's the fiddle: http://jsfiddle.net/HQ7NF/2/ 这是小提琴: http : //jsfiddle.net/HQ7NF/2/

And here's the regex I am using: 这是我正在使用的正则表达式:

var reBoxShadow = /(?:rgb\((\d+), ?(\d+), ?(\d+)\)([^,]*))+/g;
while ( style = reBoxShadow.exex(element.style.boxShadow) ) {
    /* this is where stuff happens */
}

I then split the color and the args into separate variables, so you can decide what to do with them. 然后,我将颜色和args拆分为单独的变量,以便您可以决定如何处理它们。 I hope that can at least get you on the right track. 我希望至少可以使您走上正确的道路。

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

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