jQuery图片渐变轮播的面向过程实现&&面向对象组件化

效果:点击链接

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .clear::after {
            content: '';
            display: block;
            clear: both;
        }
        
        .layout {
            width: 1000px;
            margin: 0 auto;
        }
        
        .carousel {
            width: 1000px;
            height: 500px;
            background-color: #DDD;
            overflow: hidden;
            position: relative;
        }
        
        li {
            list-style: none;
        }
        
        .img-ct {
            position: relative;
        }
        
        .img-ct li {
            width: 1000px;
            height: 500px;
            display: none;
            position: absolute;
        }
        
        .arrow {
            position: absolute;
            top: 50%;
            margin-top: -25px;
            width: 30px;
            height: 50px;
            background-color: rgba(0, 0, 0, 1);
            text-decoration: none;
            color: #FFF;
            font-size: 20px;
            line-height: 50px;
            text-align: center;
            border-radius: 5px;
            opacity: 0.5;
        }
        
        .arrow:hover {
            opacity: 0.8;
        }
        
        .arrow-pro {
            left: 10px;
        }
        
        .arrow-next {
            right: 10px;
        }
        
        .bar {
            display: block;
            text-align: center;
            position: absolute;
            bottom: 30px;
            width: 100%;
            font-size: 0px;
        }
        
        .bar-ct {
            display: inline-block;
            padding: 0;
        }
        
        .bar-ct>li {
            display: inline-block;
            width: 150px;
            height: 75px;
            margin: 0px 10px;
            border: 1px solid #EEE;
        }
        
        .active {
            box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.5), 0 6px 20px 0 rgba(0, 0, 0, 0.6);
        }
        
        .bar-ct img {
            width: 150px;
            height: 75px;
        }
    </style>
</head>

<body>
    <div class="layout">
        <div class="carousel">
            <ul class="img-ct clear">
                <li>
                    <a href="javaScript:void(0)">![](./img/1.jpg)</a>
                </li>
                <li>
                    <a href="javaScript:void(0)">![](./img/2.jpg)</a>
                </li>
                <li>
                    <a href="javaScript:void(0)">![](./img/3.jpg)</a>
                </li>
                <li>
                    <a href="javaScript:void(0)">![](./img/4.jpg)</a>
                </li>
            </ul>
            <a href="javaScript:void(0)" class="arrow-pro arrow">
                <</a>
                    <a href="javaScript:void(0)" class="arrow-next arrow">></a>
                    <div class="bar">
                        <ul class="bar-ct clear">
                            <li class="active">
                                <a href="javaScript:void(0)"><img src="" alt=""></a>
                            </li>
                            <li>
                                <a href="javaScript:void(0)"><img src="" alt=""></a>
                            </li>
                            <li>
                                <a href="javaScript:void(0)"><img src="" alt=""></a>
                            </li>
                            <li>
                                <a href="javaScript:void(0)"><img src="" alt=""></a>
                            </li>
                        </ul>
                    </div>
        </div>
    </div>
    <script>
        var $imgct = $('.img-ct'),
            $barli = $('.bar-ct li'),
            $firstImg = $imgct.find('li').first(),
            $lastImg = $imgct.find('li').last(),
            imgWidth = $firstImg.width(),
            isAnimate = false

        barliGetImg()  
        $imgct.find('li').eq('0').css('display', 'block')
        $imgct.width(imgWidth * $imgct.find('li').length)
        carouselindex = 0;

        autoplay() 

        $('.arrow-next').on('click', function () {   //下一张
            turn(carouselindex + 1)
        })
        $('.arrow-pro').on('click', function () {  //上一张
            turn(carouselindex - 1)
        })
        $('.bar-ct').find('li').on('click', function () {  //选择切换
            var barindex = ($(this).index())
            console.log(barindex)
            turn(barindex)
        })

        function turn(idx) {  //切换函数,根据图片下标切换
            if (isAnimate) {
                return
            }
            if (carouselindex === idx) {
                return
            }
            isAnimate = true;
            $imgct.find('li').eq(carouselindex).fadeOut(500)
            if (idx < 0) {
                idx = $imgct.find('li').length - 1
            }
            if (idx > $imgct.find('li').length - 1) {
                idx = 0
            }
            $imgct.find('li').eq(idx).fadeIn(500, function () {
                carouselindex = idx
                $barli.removeClass('active')
                $barli.eq(carouselindex).addClass('active')
                isAnimate = false;

            })

        }

        function barliGetImg() {  //给下面的滚动栏缩略预览图
            $imgct.find('li').each(function () {
                $barli.eq($(this).index()).find('img')
                    .attr('src', $(this).find('img').attr('src'))
            })
        }

        function autoplay() {  自动播放
            clock = setInterval('turn(carouselindex+1)', 2000)
        }
    </script>
</body>

</html>

组件化API实现:

 var Carousel = (function () {     //声明构造函数
            function _Carousel($ct) {
                this.$ct = $ct       //指定容器
                this.init()    
                this.bind()  //事件执行

            }
            _Carousel.prototype = {
                init: function () {  //建立变量
                    this.$imgct = this.$ct.find('.img-ct')
                    this.$barli = this.$ct.find('.bar-ct li')
                    this.$firstImg = this.$imgct.find('li').first()
                    this.$lastImg = this.$imgct.find('li').last()
                    this.imgWidth = this.$firstImg.width()
                    this.isAnimate = false
                },
                bind: function () {  //事件函数
                    this.barliGetImg()
                    var _this = this
                    console.log(this.$ct.attr('id'))
                    this.$imgct.find('li').eq('0').css('display', 'block')
                    this.$imgct.width(this.imgWidth * this.$imgct.find('li').length)
                    this.carouselindex = 0;
                    this.autoplay()  //执行自动播放
                    this.$ct.find('.arrow-next').on('click', function () {
                        _this.turn(_this.carouselindex + 1)
                    })
                    this.$ct.find('.arrow-pro').on('click', function () {
                        _this.turn(_this.carouselindex - 1)
                    })
                    this.$ct.find('.bar-ct').find('li').on('click', function () {
                        var barindex = ($(this).index())
                        console.log(barindex)
                        _this.turn(barindex)
                    })
                },
                turn: function (idx) {  //切换功能,能根据图片下标idx切换
                    var _this = this
                    if (this.isAnimate) {
                        return
                    }
                    if (this.carouselindex === idx) {
                        return
                    }
                    this.isAnimate = true;
                    this.$imgct.find('li').eq(this.carouselindex).fadeOut(500)
                    if (idx < 0) {
                        idx = this.$imgct.find('li').length - 1
                    }
                    if (idx > this.$imgct.find('li').length - 1) {
                        idx = 0
                    }
                    this.$imgct.find('li').eq(idx).fadeIn(500, function () {
                        _this.carouselindex = idx
                        _this.$barli.removeClass('active')
                        _this.$barli.eq(_this.carouselindex).addClass('active')
                        _this.isAnimate = false;

                    })
                },
                barliGetImg: function () {   //给下面滚动栏设置缩略图
                    var _this = this
                    this.$imgct.find('li').each(function () {
                        _this.$barli.eq($(this).index()).find('img')
                            .attr('src', $(this).find('img').attr('src'))
                    })
                },
                autoplay: function () {  //自动播放功能
                    var _this = this
                    clock = setInterval(function () {
                        _this.turn(_this.carouselindex + 1)
                    }, 2000)
                }
            }
            return {
                init:function($ct){  //API接口
                    $ct.each(function(index, node){
                        new _Carousel($(node));
                    })
                }
            }
        })()

    Carousel.init($('.carousel'))    //调用API
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,716评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,558评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,431评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,127评论 0 209
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,511评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,692评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,915评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,664评论 0 202
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,412评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,616评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,105评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,424评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,098评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,096评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,869评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,748评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,641评论 2 271

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 170,569评论 25 707
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 11,618评论 4 59
  • 曾经有朋友问我红豆馅怎么做?刚好今天做豆沙馅,就把做法记录成文字。给想做的朋友做个参考吧! 首先是材料,我因...
    小龙女妈妈阅读 3,486评论 4 4
  • 脱掉衣服用勺子喝酒母亲不看我尽管我许久未归一条漫长的模糊的时间线起点是母亲不再为我洗澡我这样肮脏下去直到在远方找到...
    中习习阅读 277评论 2 5