JQuery实现图片自动轮播左右切换鼠标移入

首先 轮播的精髓就是在于图片的平滑滚动,下面图片应该能一下就看明白;

第一 我们将可视区控制在我们想呈现的内容的大小
第二 接着是在可视区内放入内容(图片);
第三 JQ效果实现 ,

中下方小圆点(按钮移入切换事件);
图片左右两侧点击切换事件;
自动播放事件;
移入图片终止播放移出开启播放;

JQuery实现图片自动轮播左右切换鼠标移入_第1张图片

css 以及 html 展示

 <style>
        * {
            margin: 0;
            padding: 0;
        }

        li {
            list-style: none;
        }

        img {
            display: block;
        }

        #banner {
            width: 800px;
            height: 533px;
            margin: 0 auto;
            position: relative;
            overflow: hidden;	//注意此处的内容溢出隐藏
        }

        #banner .pic {
            position: absolute;
            left: 0;
            top: 0;
            width: 3200px;			//ul宽度为所有 图片宽*数量
        }

        #banner .pic li {
            float: left;			//让所有图片横向排列
        }

        #banner .btn {			// 定位中下方小圆点
            position: absolute;
            left: 50%;
            bottom: 50px;
            transform: translateX(-50%);
        }

        #banner .btn li {		//中下方小圆点样式
            float: left;
            width: 20px;
            height: 20px;
            background-color: #ccc;
            margin: 0 5px;
            border-radius: 50%;
            font-size: 0;
        }

        #banner .btn .active {		// 中下方小圆点样式添加背景颜色为橘红色
            background-color: orangered;
        }

        #banner .LRbtn {			//定位左右按钮
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 100%;
        }

        .LRbtn span {		//定位左右按钮样式
            float: left;
            width: 30px;
            height: 30px;
            background-color: rgba(255, 255,255, 0.5);
            font-size: 20px;
            line-height: 30px;
            text-align: center;
            color:black;
            cursor: pointer;
        }

        #banner .LRbtn .right {
            float: right;
        }
    </style>
</head>
<body>
<div id="banner">
    <ul class="pic">
        <li><img src="images/maldives_1.jpg" alt=""/></li>
        <li><img src="images/maldives_2.jpg" alt=""/></li>
        <li><img src="images/maldives_3.jpg" alt=""/></li>
        <li><img src="images/maldives_4.jpg" alt=""/></li>
    </ul>
    <ul class="btn">
        <li class="active"></li>	
        //注意 此处先给于中下方第一小按钮一个类名	效果为背景橘红色
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <div class="LRbtn">
        <span class="left">&lt;</span>
        <span class="right">&gt;</span>
    </div>
</div>

</body>

样式呈现效果
JQuery实现图片自动轮播左右切换鼠标移入_第2张图片
JQuery代码呈现

  			var index = 0;       
  			 //  定义一个初始值
            var picwidth = $('.pic li img').width();    
               // 一张图片的宽度
            var   timer = '';                                      
              //声明一个定时器

            $('.btn li').mouseenter(function(){         
                //小按钮移入事件
                index = $(this).index()-1;             
                 // 减一是因为move 函数自身+1;
                move();			
                	//move()方法最在下方
            });

            timer = setInterval(move,1000);    //  自动播放

            $('.left').click(function () {    
             //点击切换   左
                index -= 2;
                move();
            });
            $('.right').click(function () {   
            //点击切换   右
                move();
            });

            function move() {                //运动函数
                index++ ;
                if (index < 0) {   //当状态值时为0时  使图片跳转到最后张图
                    index = 3;
                    $('.pic').stop().animate({'left': -index * picwidth})
                }
                if (index == 4) {//当状态值时为4时  使图片跳转到第一张图
                    index = 0;
                    $('.pic').stop().animate({'left': 0})
                }
                  // ul 宽度随状态值切换
                $('.pic').stop().animate({'left': -index * picwidth}) ;
                   //  小按钮类名随状态值切换
                $('#banner .btn li').removeClass('active').eq(index).addClass('active') ;   
            }
            	 // 移入清除定时器 //移出开始定时器
$('#banner').hover(function(){   clearInterval(timer);},function(){   timer = setInterval(move,1000);});

以上就是Jquery实现轮播图的步骤与原理。

你可能感兴趣的:(JQuery)