【问题标题】:.animate() in jQuery and if condition seems to don't workjQuery 中的 .animate() 和 if 条件似乎不起作用
【发布时间】:2016-12-31 08:35:07
【问题描述】:

我不是 jQuery 专家,我尝试创建一种按钮切换: 当我点击切换按钮“主页/新闻”时,内容也应该切换。

但这似乎不起作用......尽管有 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 ;当我单击奇数 => container2 时;当我点击甚至=> container1 ...

一些截图解释:

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

【问题讨论】:

  • 我不明白你想达到什么目的。
  • 嗯,我会用一些截图来解释:这是横幅(来自我的网站):link 当我点击右下角按钮时,我想要这个结果:link(我需要一个带有此按钮的“无限循环”;我的意思是,当我单击奇数、横幅、偶数、幻灯片时。我不知道我是否清楚)我只想更改内容(使用一种覆盖幻灯片效果(这就是我需要使用 .animate() 的原因?))我使用 jQuery 来实现这个结果,但是,正如您在 jsfiddle 中看到的那样,动画执行两种效果(在某种程度上)。

标签: javascript jquery if-statement jquery-animate


【解决方案1】:

动画进出的原因是 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>

【讨论】:

  • 嗨,谢谢,效果很好。我尝试了检查属性技术,但没有使用动画方法,你让我很开心,谢谢!
【解决方案2】:

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

不确定您要完成什么,但我将添加/删除类行移到匿名函数中,以便在动画完成后执行。

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

请尝试告诉我。

【讨论】:

    猜你喜欢
    • 2017-05-15
    • 2020-12-11
    • 2013-07-24
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多