我正在编写图像旋转功能。我创建了一个数组来存储变换函数的度数。
但是,我需要知道在 rotations
数组之间移动下一个和上一个,而不是递增数组。
左右切换无法按预期工作。
https://jsfiddle.net/andreas20/v94zLg8b/
rotations = ['90deg', '180deg', '270deg', '360deg']
let img = document.getElementById('img_blob');
let array_increment = 0
$('#left').click(() => {
if (array_increment > 3)
array_increment = 0
img.style.transform = 'rotate(-' + rotations[array_increment] + ')'
array_increment++
console.log(array_increment)
})
$('#right').click(() => {
if (array_increment > 3)
array_increment = 0
img.style.transform = 'rotate(' + rotations[array_increment] + ')'
array_increment++
console.log(array_increment)
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="left">Left</button>
<button id="right">Right</button><br>
<img id="img_blob" src="https://i.imgur.com/vgD5ycf.jpg">
最佳答案
你可以在不使用数组的情况下进行类似的旋转
let img = document.getElementById('img_blob');
let rotationValue = 0;
$('#left').click(() => rotate(-1));
$('#right').click(() => rotate(1));
function rotate(direction) {
rotationValue += 90 * direction;
rotationValue = rotationValue % 360;
img.style.transform = `rotate(${rotationValue}deg)`;
// console.log(rotationValue);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img id="img_blob" src="https://dummyimage.com/150x150/000/fff" />
<button id="left">Left</button>
<button id="right">Right</button>
关于javascript - JavaScript 中使用数组进行图像旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60667596/