jQuery绑定/取消绑定不起作用

时间:2016-07-19 06:29:02

标签: javascript jquery

我正在尝试创建一个简单的滑块。这是一个例子,但是下一个滑块和上一个按钮不能正常工作。

// next
var next = $('.next').click(function() {
  var storepos = $(".storepos").val();
  $('.prev').bind('click');
  $('.storepos').val($('.storepos').val() / 1 + 110);
  $('.container').animate({
    scrollLeft: $('.storepos').val()
  }, 200);
});

//prev
$('.prev').click(function() {
  var storepos = $(".storepos").val();
  $('.next').bind('click');
  $('.storepos').val($('.storepos').val() / 1 - 110);
  $('.container').animate({
    scrollLeft: $('.storepos').val()
  }, 200);
});

//after scrollend right event
$('.container').bind('scroll', function() {
  if ($('.container').scrollLeft() + $(this).innerWidth() >= $(this)[0].scrollWidth) {
    $('.next').unbind('click');
  }
});
//after scrollend left event                               
$('.container').bind('scroll', function() {
  if ($('.container').scrollLeft() < 1) {
    $('.prev').unbind('click');
  }
});
.container {
  overflow: hidden !important
}

.container::-webkit-scrollbar {
  width: 0;
  height: 0
}

.content {
  width: 1600px
}

.items {
  background: black;
  color: white;
  margin-left: 10px;
  width: 100px;
  height: 100px;
  float: left;
  text-align: center
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="content">
    <div class="items">1</div>
    <div class="items">2</div>
    <div class="items">3</div>
    <div class="items">4</div>
    <div class="items">5</div>
    <div class="items">6</div>
    <div class="items">7</div>
    <div class="items">8</div>
    <div class="items">9</div>
    <div class="items">10</div>
  </div>
</div>
<a href="#" class="prev">Prev</a> / <a href="#" class="next">Next</a>

<input class="storeposx" value="" />
<input class="storepos" value="" />

fiddle

1 个答案:

答案 0 :(得分:1)

我看到两个错误。首先,前一个按钮在乞讨时处于活动状态,可以滚动到负值。其次,当你到达双方的末端时,你会解开事件,但在此之后你又不会将它们绑定回来。

我使用两个变量来保持按钮状态。当我到达开始或结束位置时,我不会解开它们,相反,我只是在点击时返回false。

&#13;
&#13;
// next
var next = $('.next').click(function() {
	if (!nextIsActive || $('.container').is(':animated')) return false;
	var storepos = $(".storepos").val();
	$('.prev').bind('click');
	$('.storepos').val($('.storepos').val() / 1 + 110);
	$('.container').animate({
		scrollLeft: $('.storepos').val()
	}, 200);
});

//prev
$('.prev').click(function() {
	if (!prevIsActive || $('.container').is(':animated')) return false;
	var storepos = $(".storepos").val();
	$('.next').bind('click');
	$('.storepos').val($('.storepos').val() / 1 - 110);
	$('.container').animate({
		scrollLeft: $('.storepos').val()
	}, 200);
});

var nextIsActive=true;
var prevIsActive=false;
//after scrollend right event
$('.container').bind('scroll', function() {
	if ($('.container').scrollLeft() + $(this).innerWidth() >= $(this)[0].scrollWidth) {
		nextIsActive=false;
		}else{
		nextIsActive=true;
	}
});
//after scrollend left event                               
$('.container').bind('scroll', function() {
	if ($('.container').scrollLeft() < 1) {
		prevIsActive=false;
		}else{
		prevIsActive=true;
	}
});
&#13;
.container{overflow:hidden !important}
.container::-webkit-scrollbar {
    width:0;
    height:0
}
.content {width:1600px}
.items { background:black;
       color:white;
       margin-left:10px;
       width:100px;
       height:100px;
       float:left;
       text-align:center
      }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="content">
<div class="items">1</div>
<div class="items">2</div>
<div class="items">3</div>
<div class="items">4</div>
<div class="items">5</div>
<div class="items">6</div>
<div class="items">7</div>
<div class="items">8</div>
<div class="items">9</div>
<div class="items">10</div>
</div>
</div>
<a href="#" class="prev">Prev</a> / <a href="#" class="next">Next</a>

<input class="storeposx" value="" />
<input class="storepos" value="" />
&#13;
&#13;
&#13;