0

I have the following code, which is within a swipe function.

When a swipe is detected, this code runs.

At the top you will see I am checking if there is an ID for previous slides. If this is returned as 'undefined' I am setting the variable as null.

Then, on the 'left' or 'right' conditions I am checking if either prevId or nextId has a value. For example: if (direction == "left" && nextId) {

I have checked, and the values are null, but the condition still runs. I have tried several different combinations, but each time the value is null and it still runs.

Does anyone have any ideas?

var prevId = $('.active').prev('.slide-item').attr('id');
var nextId = $('.active').next('.slide-item').attr('id');

if (prevId === undefined) {
    prevId = null;
}
if (nextId === undefined) {
    nextId = null;
}

if( phase == "move" && (direction == "left" || direction == "right") ) {

    var duration = 0;
    if (direction == "left" && nextId) {
        scrollImages(0 - distance, duration);
    } else if (direction == "right" && prevId) {
        scrollImages(0 + distance, duration);
    }
} else if ( phase == "cancel") {
    $('.active').css("-webkit-transform", "translate3d(0px,0px,0px)")
                .css("-moz-transform", "translate3d(0px,0px,0px)")
                .css("-ms-transform", "translate3d(0px,0px,0px)")
                .css("-0-transform", "translate3d(0px,0px,0px)")
                .css("transform", "translate3d(0px,0px,0px)");
} else if ( phase =="end" ) {
    if (direction == "right") {
        prev();
    } else if (direction == "left") {
        next();
    }
}
ccdavies
  • 1,576
  • 5
  • 19
  • 29

1 Answers1

0

Try Below Code

var prevId = $('.active').prev('.slide-item').attr('id');
var nextId = $('.active').next('.slide-item').attr('id');

if (prevId === undefined) {
    prevId = null;
}
if (nextId === undefined) {
    nextId = null;
}

if( phase == "move" && (direction == "left" || direction == "right") ) {

    var duration = 0;
    if (direction == "left" && !nextId) {
        scrollImages(0 - distance, duration);
    } else if (direction == "right" && !prevId) {
        scrollImages(0 + distance, duration);
    }
} else if ( phase == "cancel") {
    $('.active').css("-webkit-transform", "translate3d(0px,0px,0px)")
                .css("-moz-transform", "translate3d(0px,0px,0px)")
                .css("-ms-transform", "translate3d(0px,0px,0px)")
                .css("-0-transform", "translate3d(0px,0px,0px)")
                .css("transform", "translate3d(0px,0px,0px)");
} else if ( phase =="end" ) {
    if (direction == "right") {
        prev();
    } else if (direction == "left") {
        next();
    }
}
prog1011
  • 3,425
  • 3
  • 30
  • 57