jquery简单轮播

jquery简单轮播_第1张图片

html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>001轮播title>
    <script src="jquery-1.11.3.js">script>
    <style>
        * {  padding: 0;  margin: 0;  }
        a {  text-decoration: none;  }
        ul {  list-style: none;  }
        .slider, .slider-extra, .slider-panel img {
            width: 650px;
            height: 413px;
        }
        .slider {
            margin: 30px auto;
            text-align: center;
            position: relative;
        }
        .slider-panel, .slider-nav, .slider-pre, .slider-next {
            position: absolute;
            z-index: 8;
        }
        .slider-panel img {
            border: none;
        }
        .slider-extra {
            position: relative;
        }
        .slider-nav {
            margin-left: -51px;
            left: 50%;
            bottom: 4px;
        }
        .slider-nav li {
            display: inline-block;
            width: 18px;
            height: 18px;
            line-height: 18px;
            border-radius: 50%;
            text-align: center;
            margin: 0 2px;
            color: #fff;
            background: #3e3e3e;
            overflow: hidden;
            cursor: pointer;
        }
        .slider-nav .slider-item-selected {
            background: blue;
        }
        .slider-page a {
            display: block;
            position: absolute;
            top: 50%;
            width: 28px;
            height: 62px;
            line-height: 62px;
            margin-top: -31px;
            font-size: 22px;
            text-align: center;
            color: #fff;
            background: rgba(0,0,0,0.2);
        }
        .slider-next {
            left: 100%;
            margin-left: -28px;
        }
        .slider-page a:hover {
            background: rgba(0,0,0,0.5);
        }
    style>
head>
<body>
    <div class="slider">
        <ul class="slider-main">
            <li class="slider-panel"><a href="#"><img src="img/1.jpg" alt="001">a>li>
            <li class="slider-panel"><a href="#"><img src="img/2.jpg" alt="001">a>li>
            <li class="slider-panel"><a href="#"><img src="img/3.jpg" alt="001">a>li>
            <li class="slider-panel"><a href="#"><img src="img/4.jpg" alt="001">a>li>
        ul>
        <div class="slider-extra">
            <ul class="slider-nav">
                <li class="slider-item slider-item-selected">1li>
                <li class="slider-item">2li>
                <li class="slider-item">3li>
                <li class="slider-item">4li>
            ul>
            <div class="slider-page">
                <a class="slider-pre" href="javascript:;;"><a>
                <a class="slider-next" href="javascript:;;">>a>
            div>
        div>
    div>

    <script>
        $(document).ready(function () {
            var currentIndex = 0,
                    interval,
                    hasStarted = false,   //是否已经开始轮播
                    t = 3000;           //轮播时间间隔
            length = $('.slider-panel').length;   //轮播图片的长度,即有几张图

            $('.slider-panel:not(:first)').hide();  //除了第一张图片其他全部隐藏

            $('.slider-page').hide();   //上下翻页隐藏
            //鼠标悬停时轮播停止,上下翻页按钮显示;鼠标离开时轮播开始,上下翻页按钮隐藏
            $('.slider-panel,.slider-pre,.slider-next').hover(function () {
                stop();
                $('.slider-page').show();
            }, function () {
                $('.slider-page').hide();
                start();
            });

            $('.slider-item').hover(function (e) {
                stop();
                var preIndex = $('.slider-item').filter('.slider-item-selected').index();
                var currentIndex = $(this).index();
                play(preIndex,currentIndex);
            }, function () {
                start();
            });
            $('.slider-pre').bind('click',function () {
                pre();
            });
            $('.slider-next').bind('click',function () {
                next();
            });

            /* 向前翻页 */
            function pre() {
                var preIndex = currentIndex;
                currentIndex = (--currentIndex + length) % length;
                play(preIndex,currentIndex);
            }
            /* 向前翻页 */
            function next() {
                var preIndex = currentIndex;
                currentIndex = ++currentIndex % length;
                play(preIndex,currentIndex);
            }
            /*
            * preIndex页翻到currentIndex
            * preIndex整数,翻页的起始页
            * currentIndex整数,翻到的那页
            * */
            function play(preIndex, currentIndex) {
                $('.slider-panel').eq(preIndex).fadeOut(500)
                        .parent().children().eq(currentIndex).fadeIn(1000);
                $('.slider-item').removeClass('slider-item-selected')
                        .eq(currentIndex).addClass('slider-item-selected');
            }
            /* 开始轮播 */
            function start() {
                if (!hasStarted){
                    hasStarted = true;
                    interval = setInterval(next,t);
                }
            }
            /* 停止轮播 */
            function stop() {
                clearInterval(interval);
                hasStarted = false;
            }
            start();   //开始轮播
        });
    script>
body>
html>

你可能感兴趣的:(jquery简单轮播)