家伙! 我有一个简单的js简单滑块的问题。它适用于左侧位置: - = 999px; 我的js更改类的按钮使其无效,但按钮仍然有效。 我想念的地方?我需要禁用单击按钮。
$(function(){
var slideWidth = $('#slider ul li').width();
$('a.control_next').click(function() {
$('ol > li:first-child > a').removeClass('active');
$('ol > li:last-child > a').removeClass('control_next');
$('ol > li:first-child > a').addClass('control_prev');
$('ol > li:last-child > a').addClass('active');
$('#anim').animate({
left: "-=920",
}, 500, function() {
});
});
$('a.control_prev').click(function() {
$('ol > li:last-child > a').removeClass('active');
$('ol > li:last-child > a').addClass('control_next');
$('ol > li:first-child > a').addClass('active');
$('ol > li:first-child > a').removeClass('control_prev');
$('#anim').animate({
left: "+=920",
}, 500, function() {
})
});
});
#slider {
overflow: hidden;
padding-left: 0;
padding-right: 0;
}
#slider ul {
position: absolute;
overflow: hidden;
width: 1000%;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}
#slider ul li {
float: left;
width: 920px;
}
ol{
margin: 200px auto 0 auto;
padding: 30px 20px;
list-style: none;
bottom: 0px;
text-align: center;
}
ol > li {
margin: 0 6px;
display: inline-block;
}
ol > li > a{
width: 11px;
height: 11px;
display: block;
background: #f7f7f7;
cursor: pointer;
border-radius: 20px;
box-shadow: inset 0 0 3px #a9afb3;
}
ol > li > a:hover:not(.active) {
background: #333;
-webkit-transition: all 250ms ease-out;
-moz-transition: all 250ms ease-out;
-o-transition: all 250ms ease-out;
transition: all 250ms ease-out;
}
ol > li > a.active{
background: #a9afb3;
cursor: default;
-webkit-transition: all 250ms ease-out;
-moz-transition: all 250ms ease-out;
-o-transition: all 250ms ease-out;
transition: all 250ms ease-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slider" class="block">
<ul id="anim">
<li><h2 class="twitter">AOS New York @aod</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit, sint! Accusamus rem molestias facilis, aliquid. Eum facere adipisci, minus ullam, quis quam eaque quia at recusandae vel veniam sint, veritatis.</p></li>
<li><h2 class="twitter">AOS New York @aod</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit, sint! Accusamus rem molestias facilis, aliquid. Eum facere adipisci, minus ullam, quis quam eaque quia at recusandae vel veniam sint, veritatis.</p></li>
</ul>
<ol>
<li><a class="control_prev active"> </a></li>
<li><a class="control_next"> </a></li>
</ol>
</div>
答案 0 :(得分:0)
尝试使用标志并在每个相应的处理程序中设置它们,如
$(function() {
var slideWidth = $('#slider ul li').width();
var next = true;
var prev = false;
$('a.control_next').click(function() {
if (next) {
$('ol > li:first-child > a').removeClass('active');
$('ol > li:last-child > a').removeClass('control_next');
$('ol > li:first-child > a').addClass('control_prev');
$('ol > li:last-child > a').addClass('active');
$('#anim').animate({
left: "-=920",
}, 500, function() {});
next = false;
prev = true;
}
});
$('a.control_prev').click(function() {
if (prev) {
$('ol > li:last-child > a').removeClass('active');
$('ol > li:last-child > a').addClass('control_next');
$('ol > li:first-child > a').addClass('active');
$('ol > li:first-child > a').removeClass('control_prev');
$('#anim').animate({
left: "+=920",
}, 500, function() {})
}
next = true;
prev = false;
});
});
#slider {
overflow: hidden;
padding-left: 0;
padding-right: 0;
}
#slider ul {
position: absolute;
overflow: hidden;
width: 1000%;
margin: 0;
padding: 0;
height: 200px;
list-style: none;
}
#slider ul li {
float: left;
width: 920px;
}
ol {
margin: 200px auto 0 auto;
padding: 30px 20px;
list-style: none;
bottom: 0px;
text-align: center;
}
ol > li {
margin: 0 6px;
display: inline-block;
}
ol > li > a {
width: 11px;
height: 11px;
display: block;
background: #f7f7f7;
cursor: pointer;
border-radius: 20px;
box-shadow: inset 0 0 3px #a9afb3;
}
ol > li > a:hover:not(.active) {
background: #333;
-webkit-transition: all 250ms ease-out;
-moz-transition: all 250ms ease-out;
-o-transition: all 250ms ease-out;
transition: all 250ms ease-out;
}
ol > li > a.active {
background: #a9afb3;
cursor: default;
-webkit-transition: all 250ms ease-out;
-moz-transition: all 250ms ease-out;
-o-transition: all 250ms ease-out;
transition: all 250ms ease-out;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slider" class="block">
<ul id="anim">
<li>
<h2 class="twitter">AOS New York @aod</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit, sint! Accusamus rem molestias facilis, aliquid. Eum facere adipisci, minus ullam, quis quam eaque quia at recusandae vel veniam sint, veritatis.</p>
</li>
<li>
<h2 class="twitter">AOS New York @aod</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Fugit, sint! Accusamus rem molestias facilis, aliquid. Eum facere adipisci, minus ullam, quis quam eaque quia at recusandae vel veniam sint, veritatis.</p>
</li>
</ul>
<ol>
<li><a class="control_prev active"> </a>
</li>
<li><a class="control_next"> </a>
</li>
</ol>
</div>