最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Closest value (snapping) - Stack Overflow

programmeradmin2浏览0评论

if I have a range slider that uses steps, for example from 0 to 100 with a step of 5, and then I have a random value between that range, let's say 56, how can I determine what's the closest snap value (55) ?

I was thinking to loop forwards and backwards until I find the closest number divisible by 5, but there has to be a better approach ?

if I have a range slider that uses steps, for example from 0 to 100 with a step of 5, and then I have a random value between that range, let's say 56, how can I determine what's the closest snap value (55) ?

I was thinking to loop forwards and backwards until I find the closest number divisible by 5, but there has to be a better approach ?

Share Improve this question asked Aug 29, 2015 at 6:58 katiekatie 1,0512 gold badges12 silver badges19 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 19

You can just use

snapped = Math.round(x / step) * step
发布评论

评论列表(0)

  1. 暂无评论