我在jquery中构建了一个简单的滑块。它工作正常。但现在我想根据图像的变化突出显示滑块底部的方块。
我的代码就在这里。
我使用了以下的html代码。
<div>
<div class="slider">
<span class="control_next">Next</span>
<span class="control_prev">Previous</span>
<ul>
<li style="background:#ccc;">Image 1</li>
<li style="background:#547;">Image 2</li>
<li style="background:#124;">Image 3</li>
<li style="background:#054;">Image 4</li>
</ul>
</div>
<div class="square square1"></div>
<div class="square square2"></div>
<div class="square square3"></div>
<div class="square square4"></div>
</div>
上述代码的css
.slider {
position: relative;
overflow: hidden;
border: 1px solid #ccc;
}
.slider ul {
position: relative;
margin: 0;
padding: 0;
list-style: none;
}
.slider ul li {
position: relative;
display: block;
float: left;
margin: 0px;
padding: 0;
width: 500px;
height: 150px;
color
}
.control_prev, .control_next {
position: absolute;
bottom: 0;
z-index: 999;
display: block;
padding: 5px;
width: auto;
height: auto;
background: #2a2a2a;
color: #fff;
text-decoration: none;
font-size: 18px;
opacity: 0.8;
cursor: pointer;
margin:5px;
}
.control_prev:hover, .control_next:hover {
opacity: 1;
-webkit-transition: all 0.2s ease;
}
.control_prev {
right:50;
border-radius: 0 2px 2px 0;
}
.control_next {
right: 0;
border-radius: 2px 0 0 2px;
}
.square
{
z-index: 999;
height:20px;
width:20px;
background:#ccc;
position: relative;
float: left;
margin:0px 5px;
}
和用于滑动图像的jquery是
$(document).ready(function(){
var slideCount = $('.slider ul li').length;
var slideWidth = $('.slider ul li').width();
var slideHeight = $('.slider ul li').height();
var sliderUlWidth = slideCount * slideWidth;
$('.slider').css({ width: slideWidth, height: + slideHeight });
$('.slider ul').css({ width: sliderUlWidth, marginLeft: - slideWidth });
$('.slider ul li:last-child').prependTo('.slider ul');
function moveLeft() {
$('.slider ul').animate({
left: + slideWidth
}, 700, function () {
$('.slider ul li:last-child').prependTo('.slider ul');
$('.slider ul').css('left', '');
});
};
function moveRight() {
$('.slider ul').animate({
left: - slideWidth
}, 700, function () {
$('.slider ul li:first-child').appendTo('.slider ul');
$('.slider ul').css('left', '');
});
};
$('.control_prev').click(function () {
moveLeft();
});
$('.control_next').click(function () {
moveRight();
});
$(function(){
setInterval(function () {
moveRight();
}, 5000);
});
});
请帮我完成此操作。 感谢。
答案 0 :(得分:0)
您需要跟踪当前显示的图像,然后相应地向“正方形”添加一个类:http://jsfiddle.net/WZCA3/
//additional variables
var allSquares = $('.square');
var totalSquares = allSquares.length;
var index = 0;
//the move function
function moveRight() {
index++;
$('.slider ul').animate({
left: - slideWidth
}, 700, function () {
$('.slider ul li:first-child').appendTo('.slider ul');
$('.slider ul').css('left', '');
});
setSquare();
};
// the active status
function setSquare() {
allSquares.removeClass("active").eq(index % totalSquares).addClass("active");
}
按照jsfiddle链接查看整个代码。
索引变量基本上跟踪当前显示的图像。当滑块转到下一个图像时它会增加1,当它转到前一个图像时它会减1。 每次更改图像时,都会获得当前索引,并通过生成currentIndex%numberOfImages找到它对应的“square”,在您的情况下,这将始终是0到3之间的值,始终设置正确的方块