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

时间:2016-12-30 23:29:17

标签: javascript jquery if-statement jquery-animate

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

但这似乎不起作用......尽管if条件,条件和其他条件都已执行......我不明白。有人可以告诉我在哪里失败了吗?

这是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;当我点击偶数=> container1 ...

一些截图解释:

- 我网站的横幅(默认=>容器1(屏幕截图中的1))和幻灯片显示(来自横幅=>容器2(屏幕截图中的2))):http://prntscr.com/dpwxat

2 个答案:

答案 0 :(得分:0)

我添加console.log('has nothere');console.log('nothere not present');只是为了确保 if 正在运行,并在开头注意到 if 条件。

不确定你想要完成什么,但我在动画完成后将匿名函数中的添加/删除类行移动。

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

请尝试让我知道。

答案 1 :(得分:0)

动画进出的原因是因为onclick正在运行两次。之所以这样,是因为你将目标定位在" onoffswitch"上课,你有多个。如果你的目标是"#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>
&#13;
&#13;
&#13;