Reputation: 1
const style1=['50%','3.5vw','3.5vw','2.5vw','2.5vw']
const style2=['0%','22.5%','95%','2.5vw','2.5vw']
function addbuttonAnimation() {
let applied = ( document.activeElement.id === 'add-order') ? [].concat(style2) : [].concat(style1)
let x = document.querySelector('#add-order')
function goStyle(...applied) {
x.style.borderRadius = applied[0]
x.style.width =applied[1]
x.style.height =applied[2]
x.style.bottom =applied[3]
x.style.right =applied[4]
}
goStyle(...applied)
}
document.querySelector('#add-order').addEventListener("mouseup", addbuttonAnimation, false);
document.querySelector('main-wrap').addEventListener("mouseup", addbuttonAnimation, false);
How do I know that my "applied" array value has changed (have same value or not from previous invoke)? So whenever addbuttonAnimation()
invoked, it does not have to re-assign any value once again if the "applied" array have the same value from previous invoke.
Upvotes: 0
Views: 72
Reputation: 15292
You can use underscore.js#intersection
to avoid extra pain of writing
let prevApplied = [];
function addbuttonAnimation() {
let applied = (document.activeElement.id === 'add-order') ? [].concat(style2) : [].concat(style1)
var boolStyleDiff = _.intersection(prevApplied, applied);
if (!boolStyleDiff) {
return false; //to avoid call to goStyle()
}
prevApplied = applied; //saved previously applied
let x = document.querySelector('#add-order')
function goStyle(...applied) {
//code
}
goStyle(...applied)
}
Upvotes: 0
Reputation: 834
The indexOf() method searches the array for the specified item and returns its position.
The search will start at the specified position, or at the beginning if no start position is specified, and end the search at the end of the array.
Returns -1 if the item is not found.
If the item is present more than once, the indexOf method returns the position of the first occurrence.
Note: The first item has position 0, the second item has position 1, and so on.
Tip: If you want to search from end to start, use the lastIndexOf() method
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var a = fruits.indexOf("Apple");
The result of a will be: 2
Upvotes: 1