.animate() 在 jQuery 中,如果条件似乎不起作用

.animate() in jQuery and if condition seems to don't work

我不是 jQuery 方面的专家,我尝试创建一种按钮切换: 当我点击切换按钮 "Home/News" 时,内容也应该切换。

但这似乎行不通...尽管有 if 条件,但条件和 else 都已执行...我不明白。谁能告诉我哪里失败了?

这是 jsfiddle:https://jsfiddle.net/zgLsbw2h/1/

jQuery :

$(document).ready(function(){ function switchButton(){ console.log("coucou") $('.onoffswitch').click(function(){ if($('.onoffswitch').hasClass('nothere')){ $('.container_slide_actu').animate({ left : 0}, 700); $('.onoffswitch').removeClass('nothere'); }else{ $('.container_slide_actu').animate({ left : '-100%'}, 700); $('.onoffswitch').addClass('nothere'); } }); } switchButton(); });

提前致谢。

**编辑

更多解释(希望简化):

我有两个 html 容器。当我点击按钮时,我想切换它们。默认情况下,我得到了 container1 ;当我点击 odd => container2 时;当我点击 even => container1...

部分截图说明:

-我网站的横幅(默认 => container1(屏幕截图上的 1))和幻灯片(谁过来横幅 => container2(屏幕截图上的 2)):http://prntscr.com/dpwxat

我添加 console.log('has nothere');console.log('nothere not present'); 只是为了确保 if 正常工作,并且在开始时注意到 if单击一次即可满足 else 条件。

不确定您要完成什么,但我将 add/remove class 行移到匿名函数中,以便在动画完成后执行。

$(document).ready(function(){

function switchButton(){
  console.log("coucou")
  $('.onoffswitch').click(function(){
    if($('.onoffswitch').hasClass('nothere')){
      console.log('has nothere');
        $('.container_slide_actu').animate({
        left : 0}, 700, function() {
                // Animation complete.
                $('.onoffswitch').removeClass('nothere');
            });
    }else{
        console.log('nothere not present');
      $('.container_slide_actu').animate({
        left : '-100%'}, 700, function() {
                // Animation complete.);
                $('.onoffswitch').addClass('nothere');
            });
       }
  });
}

switchButton();
});

请尝试告诉我。

你的动画进进出出的原因是因为onclick是运行两次。这样做的原因是因为您的目标是 "onoffswitch" class,您有不止一个。如果您将“#myonoffswitch”作为目标,它是一个 id,只针对一件事并且只会触发一次。我也清理了你的 JS。看看吧,告诉我你的想法。

$(document).ready(function(){
  $('#myonoffswitch').on("click", function(){
    if($("#myonoffswitch").is(':checked') == false) {
        $('.container_slide_actu').animate({
        left : 0}, 700);
    }else{
      $('.container_slide_actu').animate({
        left : '-100%'}, 700);
    }
  });
});
body{
  margin:0;
}
.banner-site{
    width:100%;
    background-color:white !important;
    background-position:center !important;
    background-repeat:no-repeat !important;
    background-size:cover !important;
    background-attachment:fixed;
    height:350px;
    display:flex;
    align-items:center;
    margin-top:-15px;
    transition:all .7s;
    position:relative;
    border-bottom:1px solid white;
    border-top:1px solid white;
    overflow:hidden;
  }
  .banner-site:hover h1{
    color:white;
    border:2px solid white;
    text-shadow:1px 1px 5px rgba(0,0,0,.9);
  }
  .banner-site:hover h1 a{
    color:white;
  }
  .banner-site a{
  color:black;
  text-decoration:none;
    transition:all .7s;
  }
  .banner-site .false-hover{
    position:absolute;
    width:100%;
    height:100%;
    top:0;
    left:0;
    background-color:rgba(0,0,0,0);
    z-index:1;
    transition:all .7s;
  }
  .banner-site h1{
    text-align:center;
    border:2px solid #161416;
    color:#161416;
    margin:auto;
    padding:10px;
    border-radius:2px;
    font-family:'Cinzel';
    font-size:30px;
    font-weight:300;
    text-transform:uppercase;
    position:relative;
    z-index:2;
    text-shadow:1px 1px 5px rgba(255,255,255,.9);
    transition:all .3s;
  }
  .banner-site .false-hover:hover{
    background-color:rgba(0,0,0,.5);
  }
  /*On/Off switch | ty https://proto.io/freebies/onoff/ */
.onoffswitch {
    position: absolute; width: 90px;
    -webkit-user-select:none; -moz-user-select:none; -ms-user-select: none;
    opacity:.5;/*à voir si on keep*/
    right:10px; bottom:15px;
    z-index:9999;
    display:block;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; overflow: hidden; cursor: pointer;
    border: 2px solid #999999; border-radius: 20px; text-transform:uppercase;
}
.onoffswitch-inner {
    display: block; width: 200%; margin-left: -100%;
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; float: left; width: 50%; height: 30px; padding: 0; line-height: 30px;
    font-size: 14px; color: white; font-family: 'Oswald', Arial, sans-serif; font-weight: bold;
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "Home";
    padding-left: 10px;
    background-color: #000000; color: #FFFFFF;
}
.onoffswitch-inner:after {
    content: "News";
    padding-right: 10px;
    background-color: #FFFFFF; color: #000000;
    text-align: right;
}
.onoffswitch-switch {
    display: block; width: 25px; margin: 2.5px;
    background: #FFFFFF;
    position: absolute; top: 0; bottom: 0;
    right: 56px;
    border: 2px solid #999999; border-radius: 20px;
    transition: all 0.3s ease-in 0s;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px;
}
.container_slide_actu{
  width:100%;
  height:350px;
  z-index:999;
  position:absolute;
  top:0;
  left:-100%;
  display:block;
}
#mavideo {
  top: 0;
  left: 0;
  width: 100%;
  height: 350px;
  object-fit:cover;
}
/*look http://www.alsacreations.com/tuto/lire/1620-une-video-arriere-plan-sur-toute-la-page.html*/
<!DOCTYPE html>
 <html>
 <head>
  <title></title>

   <body>


   <div class="banner-site" style="background-image:url('https://media.senscritique.com/media/000009498078/1200/Lady_Vengeance.jpg')">
     <h1><a class="tdn cgrey" href="#" title="Retour à l'accueil de">Play it's evil?</a></h1>
     <div class="false-hover"></div>
    <!-- Button on/off à replace-->
     <div class="onoffswitch">
         <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked autocomplete="off">
         <label class="onoffswitch-label" for="myonoffswitch">
             <span class="onoffswitch-inner"></span>
             <span class="onoffswitch-switch"></span>
         </label>
     </div>
   <div class="container_slide_actu">
    <video id="mavideo" controls autoplay muted loop="true">
     <source src="" type="video/mp4">
    </video>
   </div>
  </div>

   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>


 </body>
 </html>