2014-11-05 6 views
0

У меня есть следующий код, который находится внутри функции прокрутки.Условия все еще работают, когда переменная пуста

При обнаружении прокрутки этот код запускается.

В верхней части вы увидите, что я проверяю, есть ли идентификатор для предыдущих слайдов. Если это возвращается как «undefined», я устанавливаю переменную как null.

Тогда, в условиях «слева» или «справа», я проверяю, имеет ли значение prevId или nextId. Например: if (direction == "left" && nextId) {

Я проверил, и значения null, но условие все еще работает. Я пробовал несколько разных комбинаций, но каждый раз значение равно null, и оно все еще выполняется.

У кого-нибудь есть идеи?

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(); 
    } 
} 

ответ

0

Try Ниже кодекса

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(); 
    } 
} 
+0

Благодаря Prog, но до сих пор не повезло. Переменные - «null», и все еще выполняется условное. – ccdavies

+0

Вы использовали 'if (direction ==" left "&&! NextId) {...}'? – prog1011

+0

См. Ту же проблему - http://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript – prog1011

Смежные вопросы