使用animate()无法正常滑动div元素

时间:2015-04-14 09:20:21

标签: javascript jquery html css

我试图使用animate() jQuery方法让我的div元素从左向右滑动。每当有人点击按钮时,它应该检查divs left属性值。如果该值等于-90%,则应将其从左向右滑动。否则(如果它是0)它应该向后滑动(左:-90%)。

JS:

$("button").click(function() {
    if($("div").css('left') == "-90%"){//check if left:-90%, if true slide it to right
        $("div").animate({left: "0px"},1000);       
    }else{
        $("div").animate({left: "-90%"},1000);//if left is not -90% slide it to left
    }
});;

HTML:

<button>Click Me</button>
<div>
</div>

CSS:

div{
    height:100px;
    width:90%;
    position:absolute;
    background-color:#77A3C5;
    left:-90%;  
}
button{
    display:block;
    position:absolute;
}

4 个答案:

答案 0 :(得分:3)

不需要看中百分比,只需保持简单:

$("button").click(function() {
    if($("div").position().left < 0){
        $("div").animate({left: "0px"},1000);         
    }else{
        $("div").animate({left: "-90%"},1000);
    }
});

演示:http://jsbin.com/juxoko/4/

答案 1 :(得分:2)

试试这个,我测试了它。

 $("button").click(function() {
    $('.parent').hide();
    var leftPercentage = $('.child').css('left');
    $('.parent').show();
    if(leftPercentage == "-90%"){//check if left:-90%, if true slide it to right
        $("div.child").animate({left: "0px"},1000);       
    }else{
        $("div").animate({left: "-90%"},1000);//if left is not -90% slide it to left
    }
});
div.child{
    height:100px;
    width:90%;
    position:absolute;
    background-color:#77A3C5;
    left:-90%;  
}
button{
    display:block;
    position:absolute;
  z-index:1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>Click Me</button>
<div class="parent">
    <div class="child">Testing</div>
</div>

答案 2 :(得分:1)

这是你可以做到的一种方式

	$(document).ready(function() {
	  $menuLeft = $('.pushmenu-left');
	  $nav_list = $('#nav_list');

	  $nav_list.click(function() {
	    $(this).toggleClass('active');
	    $('.pushmenu-push').toggleClass('pushmenu-push-toright');
	    $menuLeft.toggleClass('pushmenu-open');
	  });
	});
body {
  margin: 0;
}
.pushmenu {
  /*this is the nav*/
  background: #3c3933;
  font-family: Arial, Helvetics, sans-serif;
  width: 240px;
  height: 100%;
  top: 0;
  z-index: 1000;
  position: fixed;
}
.pushmenu h3 {
  color: #cbbfad;
  font-size: 14px;
  font-weight: bold;
  padding: 15px 20px;
  margin: 0;
  background: #282522;
  height: 16px;
}
.pushmenu a {
  display: block;
  /* drops the nav vertically*/
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  text-decoration: none;
  border-top: 1px solid #56544e;
  border-bottom: 1px solid #312e2a;
  padding: 14px;
}
.pushmenu a:hover {
  background: #00A287;
}
.pushmenu a:active {
  background: #454f5c;
  color: #fff;
}
.pushmenu-left {
  left: -240px;
}
.pushmenu-left.pushmenu-open {
  left: 0;
}
.pushmenu-push {
  overflow-x: hidden;
  position: relative;
  left: 0;
}
.pushmenu-push-toright {
  left: 240px;
}
/*Transition*/

.pushmenu,
.pushmenu-push {
  -webkit-transition: all 0.3s ease;
  -moz-transition: all 0.3s ease;
  transition: all 0.3s ease;
}
#nav_list {
  background: url(http://www.onlywebpro.com/demo/jquery/icon_nav.png) no-repeat left top;
  cursor: pointer;
  height: 27px;
  width: 33px;
  text-indent: -99999em;
}
nav-list.active {
  background-position: -33px top;
}
.buttonset {
  background: #00A287;
  height: 16px;
  padding: 10px 20px 20px;
}
section.content {
  font-family: Arial, Helvetica, sans-serif;
  padding: 10px 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<body class="pushmenu-push">
  <nav class="pushmenu pushmenu-left">
    <h3>Menu</h3>
    <a href="#">Home</a>
    <a href="#">Nav2</a>
    <a href="#">Nav3</a>
    <a href="#">Nav4</a>
    <a href="#">Nav5</a>
    <a href="#">Nav6</a>
    <a href="#">Nav7</a>
  </nav>

  <div class="container">
    <div class="main">
      <section class="buttonset">
        <div id="nav_list">Menu</div>
      </section>

      <section class="content">
        <h1>Slideout Navigation</h1>
        <p>

        </p>

      </section>
      <!-- End Content -->
    </div>
    <!-- End Main -->
  </div>
  <!-- End Container -->
</body>

答案 3 :(得分:1)

$("div").css('left')

仅以px而非百分比

返回

所以改变你的状况

if($("div").position().left/$(window).width() * 100 == "90%")

这将返回位置百分比