<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>轮播图</title> <link rel="stylesheet" type="text/css" href="demo.css"/> <script src="../jquery/jquery-2.1.1.min.js" type="text/javascript" charset="utf-8"></script> <script src="demo.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div id="igs"> <a class="ig" href="#"><img src="images/1.jpg"/></a> <a class="ig" href="#"><img src="images/2.jpg"/></a> <a class="ig" href="#"><img src="images/3.jpg"/></a> <a class="ig" href="#"><img src="images/4.jpg"/></a> <a class="ig" href="#"><img src="images/5.jpg"/></a> <div class="btn btn1"><</div> <div class="btn btn2">></div> <ul id="tabs"> <li class="tab">1</li> <li class="tab">2</li> <li class="tab">3</li> <li class="tab">4</li> <li class="tab">5</li> </ul> </div> </body> </html>
* {
margin: 0;
padding: 0;
}
#igs {
margin: 10px auto;
width: 700px;
height: 320px;
position: relative;
}
.ig {
position: absolute;
}
#tabs {
position: absolute;
list-style: none;
background-color: rgba(255,255,255,.5);
left: 300px;
bottom: 10px;
border-radius: 10px;
padding: 5px 0 5px 5px;
}
.tab{
float: left;
text-align: center;
line-height: 20px;
width: 20px;
height: 20px;
cursor: pointer;
overflow: hidden;
margin-right: 4px;
border-radius: 100%;
background-color: rgb(200,100,150);
}
.btn{
position: absolute;
color: #fff;
top: 110px;
width: 40px;
height: 100px;
background-color: rgba(255,255,255,.3);
font-size: 40px;
font-weight: bold;
text-align: center;
line-height: 100px;
border-radius: 5px;
margin: 0 5px;
}
.btn2{
position: absolute;
right: 0px;
}
.btn:hover{
background-color: rgba(0,0,0,.7);
}
//定义全局变量和定时器
var i = 0 ;
var timer;
$(document).ready(function(){
//用jquery方法设置第一张图片显示,其余隐藏
$('.ig').eq(0).show().siblings('.ig').hide();
//调用showTime()函数(轮播函数)
showTime();
//当鼠标经过下面的数字时,触发两个事件(鼠标悬停和鼠标离开)
$('.tab').hover(function(){
//获取当前i的值,并显示,同时还要清除定时器
i = $(this).index();
Show();
clearInterval(timer);
},function(){
//
showTime();
});
//鼠标点击左侧的箭头
$('.btn1').click(function(){
clearInterval(timer);
if(i == 0){
i = 5;//注意此时i的值
}
i--;
Show();
showTime();
});
//鼠标点击右侧的箭头
$('.btn2').click(function(){
clearInterval(timer);
if(i == 4){
i = -1;//注意此时i的值
}
i++;
Show();
showTime();
});
});
//创建一个showTime函数
function showTime(){
//定时器
timer = setInterval(function(){
//调用一个Show()函数
Show();
i++;
//当图片是最后一张的后面时,设置图片为第一张
if(i==5){
i=0;
}
},2000);
}
//创建一个Show函数
function Show(){
//在这里可以用其他jquery的动画
$('.ig').eq(i).fadeIn(300).siblings('.ig').fadeOut(300);
//给.tab创建一个新的Class为其添加一个新的样式,并且要在css代码中设置该样式
$('.tab').eq(i).addClass('bg').siblings('.tab').removeClass('bg');
/*
* css中添加的代码:
* .bg{ background-color: #f00; }
* */
}
var count;
$(document).ready(function(){
count= $(".main a").length; /*给动态变化的i备用*/;
//。。。代码省略
//鼠标点击左侧的箭头
$('.btn1').click(function(){
clearInterval(timer);
if(i == 0){
i = count;//注意此时i的值
}
i--;
Show();
showTime();
});
//鼠标点击右侧的箭头
$('.btn2').click(function(){
clearInterval(timer);
//console.log(count-1);
if(i == count-1){
i = -1;//注意此时i的值
}
i++;
Show();
showTime();
});
});
<div id="container"> <div id="list" style="left: -600px;"> <img src="img/5.jpg" alt="1"/> <img src="img/1.jpg" alt="1"/> <img src="img/2.jpg" alt="2"/> <img src="img/3.jpg" alt="3"/> <img src="img/4.jpg" alt="4"/> <img src="img/5.jpg" alt="5"/> <img src="img/1.jpg" alt="5"/> </div> <div id="buttons"> <span index="1" class="on"></span> <span index="2"></span> <span index="3"></span> <span index="4"></span> <span index="5"></span> </div> <a href="javascript:;" id="prev" class="arrow"><</a> <a href="javascript:;" id="next" class="arrow">></a> </div>
<script type="text/javascript">
/* 知识点: */
/* this用法 */
/* DOM事件 */
/* 定时器 */
window.onload = function () {
var container = document.getElementById('container');
var list = document.getElementById('list');
var buttons = document.getElementById('buttons').getElementsByTagName('span');
var prev = document.getElementById('prev');
var next = document.getElementById('next');
var index = 1;
var timer;
function animate(offset) {
//获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
//且style.left获取的是字符串,需要用parseInt()取整转化为数字。
var newLeft = parseInt(list.style.left) + offset;
list.style.left = newLeft + 'px';
//无限滚动判断
if (newLeft > -600) {
list.style.left = -3000 + 'px';
}
if (newLeft < -3000) {
list.style.left = -600 + 'px';
}
}
function play() {
//重复执行的定时器
timer = setInterval(function () {
next.onclick();
}, 2000)
}
function stop() {
clearInterval(timer);
}
function buttonsShow() {
//将之前的小圆点的样式清除
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].className == "on") {
buttons[i].className = "";
}
}
//数组从0开始,故index需要-1
buttons[index - 1].className = "on";
}
prev.onclick = function () {
index -= 1;
if (index < 1) {
index = 5
}
buttonsShow();
animate(600);
};
next.onclick = function () {
//由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断
index += 1;
if (index > 5) {
index = 1
}
animate(-600);
buttonsShow();
};
for (var i = 0; i < buttons.length; i++) {
(function (i) {
buttons[i].onclick = function () {
/* 这里获得鼠标移动到小圆点的位置,用this把index绑定到对象buttons[i]上,去谷歌this的用法 */
/* 由于这里的index是自定义属性,需要用到getAttribute()这个DOM2级方法,去获取自定义index的属性*/
var clickIndex = parseInt(this.getAttribute('index'));
var offset = 600 * (index - clickIndex); //这个index是当前图片停留时的index
animate(offset);
index = clickIndex; //存放鼠标点击后的位置,用于小圆点的正常显示
buttonsShow();
}
})(i)
}
container.onmouseover = stop;
container.onmouseout = play;
play();
}
</script>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有