- Bind the
clickto the.btnclass and within the click event you ascertain the index position:Bind the
clickto the.btnclass and within the click event you ascertain the index position:I've opted to use
bind()since it's performance efficient when compared withclick().
Another reason is the fact that all your elements share the same class, so, you only need to bind a click event to the class instead of attaching a click handler to each.btn. Use the index and multiply it by 100 as to get the desired
leftvalue:This way you can easily ascertain the correct value since you have a direct relation between the anchor's parent index and the desired
leftvalue.Using
event.preventDefault();to prevent the browser from following thehref:With this, you tell the browser to leave the
hrefattribute alone, thus preventing it to bubble up.Safe to use the minus signal all the time, since
0and-0is the same thing.
I've opted to use bind() since it's performance efficient when compared with click().
Another reason is the fact that all your elements share the same class, so, you only need to bind a click event to the class instead of attaching a click handler to each .btn.
- Use the index and multiply it by 100 as to get the desired
leftvalue:
This way you can easily ascertain the correct value since you have a direct relation between the anchor's parent index and the desired left value.
- Using
event.preventDefault();to prevent the browser from following thehref:
With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.
- Safe to use the minus signal all the time, since
0and-0is the same thing.