0

I've just finished building my slider and currently trying to refactor some redundant if/else code. Here is a preview of the code that I have built. Inside the event handlers. I have some if/else that are redundant, the purpose of the condition is to detect if slider reaches the last slide it will hide the next button and if the slider goes to the first slide. It will hide the previous button.

The slider I have built has no looping function.

    var programSliderContainer = $('.programs-slider'),
        programSliderWidth     = $('.programs-slider').outerWidth(),
        sliderContainer        = $('.programs-slider .view-programs-description .view-content'),
        totalSlides            = $('.programs-slider .view-programs-description .view-content .views-row').length,
        slides                 = $('.programs-slider .view-programs-description .view-content .views-row'),
        position               = 0,
        move                   = 0;


    //Reset Slider Position when resizing
    $('.programs-slider .view-programs-description .view-content').css({'right' : 0 });

    //Apply the Maximum width based on the total number of slides
    sliderContainer.width(programSliderWidth * totalSlides);

    //Apply the width of the slides based on the width of programSliderWidth
    slides.outerWidth(programSliderWidth);


    //Add Class active on Start
    $('.program-controls .program-list ul li:first-child').addClass('active');


    //Hide Previous Arrow on Start
    if( position == 0 ) {
        $('.arrow-controls .prev').hide();
    }

    $('.program-controls .program-list ul li').on('click', function() {
        position = $(this).index();
        $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });

        $('.program-controls .program-list ul li.active').removeClass('active');
        $(this).addClass('active');


        //Show Hide Directional Navigation based on Slider Position
        if( position == 0 ) {
            $('.arrow-controls .prev').hide(); 
        }

        else {
            $('.arrow-controls .prev').show(); 
        }

        if( position == totalSlides - 1  ) {
            $('.arrow-controls .next').hide(); 
        }

        else {
            $('.arrow-controls .next').show(); 
        }

    });


    $('.arrow-controls .prev').on('click', function() {
        if( position > 0 ) {
            position--;
            $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });

            $('.program-controls .program-list ul li.active').removeClass('active');
        }

        if( position == 0 ) {
            $('.arrow-controls .prev').hide(); 
        }

        else {
            $('.arrow-controls .prev').show(); 
        }


        if( position == totalSlides - 1 ) {
            $('.arrow-controls .next').hide(); 
        }

        else {
            $('.arrow-controls .next').show(); 
        }

    });

    $('.arrow-controls .next').on('click', function() {
        if( position < totalSlides - 1 ) {
            position++;
            $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });

            $('.program-controls .program-list ul li.active').removeClass('active');
        }


        if( position == 0 ) {
            $('.arrow-controls .prev').hide(); 
        }

        else {
            $('.arrow-controls .prev').show(); 
        }


        if( position == totalSlides - 1 ) {
            $('.arrow-controls .next').hide(); 
        }

        else {
            $('.arrow-controls .next').show(); 
        }

    });

}
2
  • 4
    This should be on codereview.stackexchange.com instead Commented May 22, 2017 at 10:00
  • 1
    You can remove the if statements by using toggle() and providing a boolean parameter which determines whether the element should be shown or not. Commented May 22, 2017 at 10:06

3 Answers 3

1

I would have built a function to do this for you:

function toggleArrows(position){
    position === 0 ? $('.arrow-controls .prev').hide() : $('.arrow-controls .prev').show()
    position === totalSlides - 1 ?  $('.arrow-controls .next').hide() : $('.arrow-controls .next').show() 
}

and in your code, replace

if( position == 0 ) {
        $('.arrow-controls .prev').hide(); 
    }

    else {
        $('.arrow-controls .prev').show(); 
    }


    if( position == totalSlides - 1 ) {
        $('.arrow-controls .next').hide(); 
    }

    else {
        $('.arrow-controls .next').show(); 
    }

by

toggleArrows(position)
Sign up to request clarification or add additional context in comments.

Comments

1

Another way to call function based on condition.

Use the below method to call functions based on if conditions:

condition && myFunc();

And for else, just change the same condition to function as else. Below is an example:

function a () { }

// for if condition
1 == 1 && a();
// for else condition
1 != 1 && a();

OP's code width minified if-else

    var programSliderContainer = $('.programs-slider'),
        programSliderWidth     = $('.programs-slider').outerWidth(),
        sliderContainer        = $('.programs-slider .view-programs-description .view-content'),
        totalSlides            = $('.programs-slider .view-programs-description .view-content .views-row').length,
        slides                 = $('.programs-slider .view-programs-description .view-content .views-row'),
        position               = 0,
        move                   = 0;


    //Reset Slider Position when resizing
    $('.programs-slider .view-programs-description .view-content').css({'right' : 0 });

    //Apply the Maximum width based on the total number of slides
    sliderContainer.width(programSliderWidth * totalSlides);

    //Apply the width of the slides based on the width of programSliderWidth
    slides.outerWidth(programSliderWidth);


    //Add Class active on Start
    $('.program-controls .program-list ul li:first-child').addClass('active');

    //Hide Previous Arrow on Start
    position == 0 && $('.arrow-controls .prev').hide();

    $('.program-controls .program-list ul li').on('click', function() {
        position = $(this).index();
        $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });

        $('.program-controls .program-list ul li.active').removeClass('active');
        $(this).addClass('active');


        //Show Hide Directional Navigation based on Slider Position
        position == 0 && $('.arrow-controls .prev').hide(); 
        position != 0 && $('.arrow-controls .prev').show(); 

        position == (totalSlides - 1) && $('.arrow-controls .next').hide(); 
        position! (totalSlides - 1) && $('.arrow-controls .next').show(); 
    });


    $('.arrow-controls .prev').on('click', function() {
        position > 0 && function () {
            position--;
            $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });
            $('.program-controls .program-list ul li.active').removeClass('active');
        }();

        position == 0 && $('.arrow-controls .prev').hide(); 
        position != 0 && $('.arrow-controls .prev').show(); 

        position == (totalSlides - 1) && $('.arrow-controls .next').hide(); 
        position != (totalSlides - 1) && $('.arrow-controls .next').show(); 
    });

    $('.arrow-controls .next').on('click', function () {
        position < (totalSlides - 1) && function () {
            position++;
            $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });
            $('.program-controls .program-list ul li.active').removeClass('active');
        }();

        position == 0 && $('.arrow-controls .prev').hide(); 
        position != 0 && $('.arrow-controls .prev').show();

        position == (totalSlides - 1) && $('.arrow-controls .next').hide(); 
        position != (totalSlides - 1) && $('.arrow-controls .next').show(); 
    });
}

You can also use anonymous functions and invoke them.

Comments

1

Created separate functions for adding/removing classes-

var programSliderContainer = $('.programs-slider'),
        programSliderWidth     = $('.programs-slider').outerWidth(),
        sliderContainer        = $('.programs-slider .view-programs-description .view-content'),
        totalSlides            = $('.programs-slider .view-programs-description .view-content .views-row').length,
        slides                 = $('.programs-slider .view-programs-description .view-content .views-row'),
        position               = 0,
        move                   = 0;


    //Reset Slider Position when resizing
    $('.programs-slider .view-programs-description .view-content').css({'right' : 0 });

    //Apply the Maximum width based on the total number of slides
    sliderContainer.width(programSliderWidth * totalSlides);

    //Apply the width of the slides based on the width of programSliderWidth
    slides.outerWidth(programSliderWidth);

    //Add Class active on Start
    $('.program-controls .program-list ul li:first-child').addClass('active');

    //Hide Previous Arrow on Start
    if( position == 0 ) {
        $('.arrow-controls .prev').hide();
    }

    $('.program-controls .program-list ul li').on('click', function() {
        position = $(this).index();
        toggleClasses();
        $(this).addClass('active');
        //Show Hide Directional Navigation based on Slider Position
         toggleArrows();
    });


    $('.arrow-controls .prev').on('click', function() {
        if( position > 0 ) {
            position--;
            toggleClasses();
        }
        toggleArrows();
    });

    $('.arrow-controls .next').on('click', function() {
        if( position < totalSlides - 1 ) {
            position++;
            toggleClasses();
        }
        toggleArrows();
    });
    
function toggleArrows(){

     (position === 0)? $('.arrow-controls .prev').hide() : $('.arrow-controls .prev').show();
     (position === totalSlides-1)? $('.arrow-controls .next').hide():$('.arrow-controls .next').show(); 
}

function toggleClasses() {

     $('.programs-slider .view-programs-description .view-content').css({'right' : position*programSliderWidth });
     $('.program-controls .program-list ul li.active').removeClass('active');
 }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.