在函数中添加多个条件

时间:2016-05-17 06:41:10

标签: javascript

我想在1个函数中添加2个条件,但是我遇到了一些问题。我正在使用的代码是

 var timeCheck = function(){
    var timePos = videoBGPlayer.currentTime; 
    if (timePos > 1){
        TweenLite.to(videoBGPlayer, 3, { marginTop: -130});
        videoBGPlayer.removeEventListener('timeupdate', timeCheck, false);
    } 
};

我想要实现的是,当视频timePos为1时,视频向上移动,当视频timePos为7时,视频会回到原始位置。

任何帮助将不胜感激

4 个答案:

答案 0 :(得分:0)

您可以使用多种条件,例如

var timeCheck = function(){
  var timePos = videoBGPlayer.currentTime;
  if (timePos < 7){
    TweenLite.to( videoBGPlayer , 3 , { marginTop: -130 }  );
    videoBGPlayer.removeEventListener('timeupdate',timeCheck,false);
  } 
  else if (timePos == 7) {
    //go back to original position.
  }
};

答案 1 :(得分:0)

首先检查timePos是否超过7,并且只检查是否超过1

 var timeCheck = function(){
    var timePos = videoBGPlayer.currentTime;

    if (timePos >= 7) {
        // move to origin position
    } else if (timePos > 1){
        TweenLite.to(videoBGPlayer, 3, { marginTop: -130});
        videoBGPlayer.removeEventListener('timeupdate', timeCheck, false);
    } 
};

答案 2 :(得分:0)

简单if/else if逻辑。

if (timePos == 1){
    TweenLite.to(videoBGPlayer, 3, { marginTop: -130});
    videoBGPlayer.removeEventListener('timeupdate', timeCheck, false);
} 
else if( timePos == 7){
    //Move video down
}

你说当视频的timePos为1且不大于1时,如果你这样离开:

if (timePos > 1){

控件永远不会到达else if因为7大于1所以如果可能的话使用等于运算符

答案 3 :(得分:0)

这样的东西?

var timeCheck = function(){
    var timePos = videoBGPlayer.currentTime; 
    if (timePos >= 1 && timePos < 7){
        TweenLite.to(videoBGPlayer, 3, { marginTop: -130});
        videoBGPlayer.removeEventListener('timeupdate', timeCheck, false);
    }else if(timePos >= 7){
       TweenLite.to(videoBGPlayer, 3, { marginTop: 0});
    } 
};