jquery - 将 "180 - 360"的数据范围标准化为 "0 - 100"范围 - jQuery/JS

标签 jquery html css

我有一个正在旋转的图像,但它只能在 180 度和 360 度之间旋转。但我需要输入的 targetValue 介于 0 - 100 之间。

因此,如果您输入的 targetValue 为 20,它会将图像旋转 180 度 - 360 度范围内的 20%。

我已附上我已经拥有的代码片段 -

如有任何帮助,我们将不胜感激!

代码-

var targetValue = 100; //CAN ONLY BE BETWEEN 0 & 100

console.log(targetValue);

function targetChange(){
    $('#target').css('transform', 'rotate(' + targetValue + 'deg' + ')');
    console.log('hello');
}
targetChange();
.target {
    position: absolute;
	width: 100%;
    height: 100%;
    z-index: 2;
    transform: rotate(180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<img class="target" id="target" src="http://i65.tinypic.com/oarptf.png">

最佳答案

这很简单

  • 取输入值(0-100)
  • 求 180 度的比例
  • 将其与 180 相加,得到 180-360 范围内的值

这在下面进行了演示

$('#input').on("change", function(){
    var val = +$(this).val();
    var rotAmount = 180 + ((val/100) * 180) 
    $('#output').val(rotAmount);
    $('#target').css('transform', 'rotate(' + rotAmount + 'deg' + ')');
});
.target {
    position: relative;
	width: 100%;
    height: 100%;
    z-index: 2;
    transform: rotate(180deg);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Enter value between 0-100 <input id="input" type="number" min="0" max="100">
<br>
Rotation: <input type="text" id="output">
<hr>
<img class="target" id="target" src="http://i65.tinypic.com/oarptf.png">

关于jquery - 将 "180 - 360"的数据范围标准化为 "0 - 100"范围 - jQuery/JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44946136/

相关文章:

Javascript var 到 php var

html - 移动设备上的表格被切断?

javascript - 使用 instafeed.js - 空白页面,不起作用

html - 覆盖 CSS 类以删除填充

jquery - 尝试将 Google Places API 与 JQuery 的 getJSON 函数一起使用

javascript - 在点击时保留标记状态 -Treemap - Highchart

html - 将 div 底部的元素与动态边框对齐(绝对位置 + 边距)

css - Beautifyrc SCSS 在关闭花括号后添加新的白线

css - 是否有任何网络应用程序可以将小图像合并为一个图像?

jquery - 如何防止从 select2 中删除标签