Jquery实现简单的图片轮播 (通过点击数字切换)

最终实现效果图在这里插入图片描述
代码块
html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>轮播</title>
    <link rel="stylesheet" href="css/index.css">
    <script src="jq/jquery-1.7.2.js" type="text/javascript"></script>
    <script src="jq/index.js" type="text/javascript"></script>
</head>
<body>
<div class="box" id="box">
    <div class="big-img" id="big-img">
        <ul>
            <li><a href=""><img src="image/1.jpg"></a></li>
            <li><a href=""><img src="image/2.jpg"></a></li>
            <li><a href=""><img src="image/3.jpg"></a></li>
            <li><a href=""><img src="image/4.jpg"></a></li>
        </ul>
    </div>
    <div class="square" id="square">
        <span class="current">1</span>
        <span>2</span>
        <span>3</span>
        <span>4</span>
    </div>
</div>
</body>
</html>

css

*{margin: 0;padding: 0}
ul{list-style: none}

img {
    vertical-align: top
}
.box{
    position: absolute;
    width: 960px;
    height: 600px;
    margin-left: 200px;
    margin-top: 100px;
    padding: 5px;
    border: 1px solid #ccc;
}
.big-img{
    width: 960px;
    height: 600px;
    overflow: hidden;
    position: relative;
}
.big-img ul{
    width: 100%;
    position: absolute;
    top: 0;
    left: 0;
}
.big-img li{
    float: left;
}

.square{
    position: absolute;
    right: 30px;
    bottom: 20px;
}
.square span{
    display: inline-block;
    width: 26px;
    height: 26px;
    margin: 5px;
    background-color: #fff;
    text-align: center;
    line-height: 26px;
    cursor: pointer;
}
.square span.current{
    background-color: orangered;
    color: #fff;
}

js

var loopIndex = 0;
var timer;
$(function (){
   slide();
   //开启定时器,每隔2秒切换图片
   timer = setInterval(function () {
      autoloop();
   },1000);
   //当鼠标移入图片的时候停止动画
   $("#box").hover(function () {
      clearInterval(timer);
   },function () {
      //当鼠标移出图片的时候继续动画
      timer = setInterval(function () {
         autoloop();
      },2000);
   })
});

function slide() {
   $("#square span").click(function () {
      //当前被点击的索引
      var index = $(this).index();
      // 当前被激活的颜色
      $(this).addClass("current")
          .siblings("span").removeClass("current");
      //当前被点击的图片索引显示,其它的隐藏
      $("#big-img li").eq(index).show().siblings("li").hide();
      //记住当前被点击的图片,向右顺序轮播
      loopIndex = index;
   });
}

function autoloop() {
   loopIndex++;
   //边界判断
   if ( loopIndex == $("#big-img li").length){
      loopIndex = 0;
   }
   $("#square span").eq(loopIndex).addClass("current")
       .siblings("span").removeClass("current");
   $("#big-img li").eq(loopIndex).show().siblings("li").hide();
}
  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值