Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link
  1. Bind the click to the .btn class and within the click event you ascertain the index position:

    Bind the click to the .btn class and within the click event you ascertain the index position:

    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.

  2. Use the index and multiply it by 100 as to get the desired left value:

    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.

  3. Using event.preventDefault(); to prevent the browser from following the href:

    With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.

  4. Safe to use the minus signal all the time, since 0 and -0 is 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.

  1. Use the index and multiply it by 100 as to get the desired left value:

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.

  1. Using event.preventDefault(); to prevent the browser from following the href:

With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.

  1. Safe to use the minus signal all the time, since 0 and -0 is the same thing.
  1. Bind the click to the .btn class and within the click event you ascertain the index position:

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.

  1. Use the index and multiply it by 100 as to get the desired left value:

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.

  1. Using event.preventDefault(); to prevent the browser from following the href:

With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.

  1. Safe to use the minus signal all the time, since 0 and -0 is the same thing.
  1. Bind the click to the .btn class and within the click event you ascertain the index position:

    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.

  2. Use the index and multiply it by 100 as to get the desired left value:

    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.

  3. Using event.preventDefault(); to prevent the browser from following the href:

    With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.

  4. Safe to use the minus signal all the time, since 0 and -0 is the same thing.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

You are correct, you can have one function have one function binding click events to all of your .btn elements.

Working Fiddle Example:

$('.btn').bind("click", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about index().

Describing what's being suggested:

  1. Bind the click to the .btn class and within the click event you ascertain the index position:

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.

  1. Use the index and multiply it by 100 as to get the desired left value:

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.

  1. Using event.preventDefault(); to prevent the browser from following the href:

With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.

  1. Safe to use the minus signal all the time, since 0 and -0 is the same thing.

In order to further improve the function, and if using the lastest jQuery, I would give an id to the ul element, thus losing the .btn class, having the click event binded to a single DOM element thru delegation:

$('#myUL').on("click", "a", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about on().

Useful reading: Why use jQuery on() instead of click() Why use jQuery on() instead of click().

You are correct, you can have one function binding click events to all of your .btn elements.

Working Fiddle Example:

$('.btn').bind("click", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about index().

Describing what's being suggested:

  1. Bind the click to the .btn class and within the click event you ascertain the index position:

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.

  1. Use the index and multiply it by 100 as to get the desired left value:

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.

  1. Using event.preventDefault(); to prevent the browser from following the href:

With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.

  1. Safe to use the minus signal all the time, since 0 and -0 is the same thing.

In order to further improve the function, and if using the lastest jQuery, I would give an id to the ul element, thus losing the .btn class, having the click event binded to a single DOM element thru delegation:

$('#myUL').on("click", "a", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about on().

Useful reading: Why use jQuery on() instead of click().

You are correct, you can have one function binding click events to all of your .btn elements.

Working Fiddle Example:

$('.btn').bind("click", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about index().

Describing what's being suggested:

  1. Bind the click to the .btn class and within the click event you ascertain the index position:

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.

  1. Use the index and multiply it by 100 as to get the desired left value:

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.

  1. Using event.preventDefault(); to prevent the browser from following the href:

With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.

  1. Safe to use the minus signal all the time, since 0 and -0 is the same thing.

In order to further improve the function, and if using the lastest jQuery, I would give an id to the ul element, thus losing the .btn class, having the click event binded to a single DOM element thru delegation:

$('#myUL').on("click", "a", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about on().

Useful reading: Why use jQuery on() instead of click().

Added Fiddle example, fixed typo on the first code block, improved the code and the answer.
Source Link
Zuul
  • 506
  • 7
  • 20

You are correct, you can have one function binding click events to all of your .btn elements.

Working Fiddle Example:

$('.btn').bind("click", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about index().

NoteDescribing what's being suggested: that

  1. Bind the click to the .btn class and within the click event you ascertain the index position:

I've opted to use bind() since it's performance efficient when compared with click(). Another
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.

  1. Use the index and multiply it by 100 as to get the desired left value:

Describing what's being suggested, This way you bindcan easily ascertain the correct value since you have a direct relation between the anchor's parent index and the desired clickleft value.

  1. Using event.preventDefault(); to prevent the browser from following the href:

With this, you tell the browser to leave the .btnhref class and within the click event you ascertain the index positionattribute alone, thus preventing it to bubble up.

  1. Safe to use the minus signal all the time, since 0 and -0 is the same thing.

In order to further improve the function, and if using the lastest jQuery, I would give an id to the ul element, thus losing the .btn class, having the click event binded to a single DOM element thru delegation:

$('#myUL').on("click", "a", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about on().

Useful reading: Why use jQuery on() instead of click().

You are correct, you can have one function binding click events to all of your .btn elements:

$('.btn').bind("click", function(event) {
 var index = $(this).index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about index().

Note that 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.

Describing what's being suggested, you bind the click to the .btn class and within the click event you ascertain the index position.


In order to further improve the function, and if using the lastest jQuery, I would give an id to the ul element, thus losing the .btn class, having the click event binded to a single DOM element thru delegation:

$('#myUL').on("click", "a", function(event) {
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about on().

Useful reading: Why use jQuery on() instead of click().

You are correct, you can have one function binding click events to all of your .btn elements.

Working Fiddle Example:

$('.btn').bind("click", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about index().

Describing what's being suggested:

  1. Bind the click to the .btn class and within the click event you ascertain the index position:

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.

  1. Use the index and multiply it by 100 as to get the desired left value:

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.

  1. Using event.preventDefault(); to prevent the browser from following the href:

With this, you tell the browser to leave the href attribute alone, thus preventing it to bubble up.

  1. Safe to use the minus signal all the time, since 0 and -0 is the same thing.

In order to further improve the function, and if using the lastest jQuery, I would give an id to the ul element, thus losing the .btn class, having the click event binded to a single DOM element thru delegation:

$('#myUL').on("click", "a", function(event) {
 event.preventDefault();
 var index = $(this).parent().index();
 $('.carousel').css({ 'left': '-' + (index*100) + '%' });
});

jQuery documentation about on().

Useful reading: Why use jQuery on() instead of click().

Updated the code suggested for the ".on()" to the anchor, as it should have been in the first place.
Source Link
Zuul
  • 506
  • 7
  • 20
Loading
Source Link
Zuul
  • 506
  • 7
  • 20
Loading
default

AltStyle によって変換されたページ (->オリジナル) /