admin管理员组文章数量:1335138
When asking jQuery for a duration in CSS, it returns this value in seconds, e.g. 1.2s
. Example here.
var d = $("div"),
propDur = d.css("animation-duration"),
propDurArr = propDur.replace(" ","").split(","),
propDel = d.css("animation-delay"),
propDelArr = propDel.replace(" ","").split(",");
console.log(propDurArr[propDurArr.length - 1]);
console.log(propDelArr[propDelArr.length - 1]);
Two questions for this:
- Is this cross-browser? Does jQuery (or better: the browser) always return the value in seconds, or do some browsers prefer a ms representation?
- How do I convert this value in jQuery/JS to ms (without
ms
)? For example:1.2s
->1200
.
For 2.
you can easily remove the dot, like so:
propDel.replace(/(\s|.)/g,"").split(",")
But how do I make a condition like this:
if string contains dot
ADD 00
if string does NOT contain dot
ADD 000
Please reply to 1 and 2.
When asking jQuery for a duration in CSS, it returns this value in seconds, e.g. 1.2s
. Example here.
var d = $("div"),
propDur = d.css("animation-duration"),
propDurArr = propDur.replace(" ","").split(","),
propDel = d.css("animation-delay"),
propDelArr = propDel.replace(" ","").split(",");
console.log(propDurArr[propDurArr.length - 1]);
console.log(propDelArr[propDelArr.length - 1]);
Two questions for this:
- Is this cross-browser? Does jQuery (or better: the browser) always return the value in seconds, or do some browsers prefer a ms representation?
- How do I convert this value in jQuery/JS to ms (without
ms
)? For example:1.2s
->1200
.
For 2.
you can easily remove the dot, like so:
propDel.replace(/(\s|.)/g,"").split(",")
But how do I make a condition like this:
if string contains dot
ADD 00
if string does NOT contain dot
ADD 000
Please reply to 1 and 2.
Share Improve this question edited May 25, 2015 at 15:30 Bram Vanroy asked May 25, 2015 at 13:36 Bram VanroyBram Vanroy 28.6k27 gold badges147 silver badges265 bronze badges4 Answers
Reset to default 7I decided to go with @RobG's ment that he hasn't posted as answer yet, so here goes. Quite simple and effective:
function toMS(s) {
return parseFloat(s) * (/\ds$/.test(s) ? 1000 : 1);
}
This is only an answer to question #2. You can use parseFloat()
to get the numerical representation, up to the first non-number (or dot) character. So, parseFloat("1.2s")*1000
returns 1200, while parseFloat("1s")*1000
returns 1000.
Check this solution to convert css time to millisecond:
function css_time_to_milliseconds(time_string) {
var num = parseFloat(time_string, 10),
unit = time_string.match(/m?s/),
milliseconds;
if (unit) {
unit = unit[0];
}
switch (unit) {
case "s": // seconds
milliseconds = num * 1000;
break;
case "ms": // milliseconds
milliseconds = num;
break;
default:
milliseconds = 0;
break;
}
return milliseconds;
}
1) The animation can either be in seconds or milliseconds - do not expect it to always be one or the other. The suffix will either be s
or ms
.
2) As for converting:
// provided `value` contains what you got from jQuery
var multiply = value.indexOf('ms') !== -1 ? 1 : 1000;
var converted = parseInt(parseFloat(value) * multiply, 10);
// converted now contains the value in millseconds
本文标签: javascriptConverting jQuery39s CSS timing to msStack Overflow
版权声明:本文标题:javascript - Converting jQuery's CSS timing to ms - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742342442a2456873.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论