jQuery - if always returns true



  • I'm doing jQuery and I want to make the following condition. If the first element of the carousel is displayed, the position will move to the right button right to the width of one unit, and if the latter, the position will move from the left to the first bloc when applied to the same button. So I'd like to run the cycling of the roll. I sold it. .position()There. if Always returning. true♪ Decided to create a variable that would contain information on which block is now active but faced the same problem. Here's the code:

    if ($ActiveElement === 1) {
        $carouselNextButton.on('click', function(){
            $carouselInner.animate({left: '-=2560px'}, 'fast');
            $ActiveElement = 2;
            console.log($ActiveElement);
        });
    } else if ($ActiveElement === 2) {
        $carouselPrevButton.on('click', function(){
            $carouselInner.animate({left: '+=2560px'}, 'fast');
        });
    }
    

    First, the first condition shall be met and the console shall be informed that $ActiveElement === 2but then it doesn't work second.

    Can you tell me where the mistake is?



  • function http://api.jquery.com/on/ Only adds a processor who's gonna be called by the click, but doesn't do it. The condition is thus fulfilled. One Once the processor is installed, and the control is not returned.

    We need to move the logic to the solution. inside. Processor:

    1. Determine what element is now shown
    2. depending on this one element or all.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2