我有以下函数,当仅旋转图像的一个实例时,该函数可以正常工作:
// Next Angle Variable
nextAngle = 0;
$( ".portfolioDropLink" ).click(function() {
// Icon Variable
var currentIcon = $(this).find(".fa-angle-down");
// Rotate Icon
currentIcon.rotate(getNextAngle());
function getNextAngle() {
nextAngle += 180;
if(nextAngle >= 360) {
nextAngle = 0;
}
return nextAngle;
}
});
当 .portfolioDropLink
类的两个实例出现 nextAngle
变量冲突时,如何防止这种情况发生?
最佳答案
一种解决方案是 retrieve the angle by getting its CSS value
另一种解决方案可能是将 Angular 与元素数据一起存储:
$( ".portfolioDropLink" ).click(function() {
// Icon Variable
var currentIcon = $(this).find(".fa-angle-down");
// Rotate Icon
currentIcon.rotate(getNextAngle(this));
function getNextAngle(el) {
//Get Value and Parse
var currentAngle = el.getAttribute('data-angle');
if (parseInt(currentAngle) == NaN) currentAngle = 0;
//Get Next Value
nextAngle = parseInt(currentAngle) + 180;
if(nextAngle >= 360) {
nextAngle = 0;
}
//Set Value and Return
el.setAttribute('data-angle', nextAngle)
return nextAngle;
}
});
关于javascript - Jquery 旋转不适用于类的多个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25609516/