我已经在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>
上面的代码
.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);
});
});
请帮我完成这个。谢谢。
您需要跟踪当前显示的图像,然后相应地为"squares"添加一个类: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,在你的例子中,这将始终是一个0到3之间的值,始终设置正确的正方形