javascript - 封装了一个渐隐渐现的轮播图组件,请问如何解决 第一次切换效果不正确?
阿神
阿神 2017-04-10 17:00:07
[JavaScript讨论组]

封装了一个渐隐渐现的轮播图组件,但是只有在第一次点击向左或向右按钮时,切换效果都不是渐隐渐现,其他时候切换效果都正确

轮播图组件代码

//轮播图组件
;(function($){

    var Carousel = function(poster){

        var self = this;
        this.poster = poster;
        this.posterMain = poster.find(".slider-list");
        this.posterItem = poster.find(".slider-panel");
        this.posterSize = this.posterItem.length;
        this.prevBtn = poster.find(".slider-page .prev");
        this.nextBtn = poster.find(".slider-page .next");
        this.navSpan = poster.find(".pic-num span");
        this.isanimated = true;
        this.index = 0;
        //默认参数
        this.setting = {
            "autoPlay" : false,
            "speed" : 200,
            "delay" : 3000,
            "hasnav" : false
        };

        $.extend(this.setting,this.getSetting());

        //prevBtn
        this.prevBtn.click(function(){
            if(self.isanimated){
                self.index --;
                if(self.index < 0 ){
                    self.index = self.posterSize -1;
                }
                self.showImg(self.index);
            }
        });

        //nextBtn
        this.nextBtn.click(function(){
            if(!self.isanimated){
                return;
            }
            self.index ++;
            if(self.index >= self.posterSize ){
                self.index = 0;
            }
            self.showImg(self.index);
        });

        //判断自动播放
        if(this.setting.autoPlay){
            this.autoPlay();
        };
    };

    Carousel.prototype = {

        //自动播放
        autoPlay : function(){
            var self = this;
            if(!self.isanimated){
                return;
            }
            self.index ++;
            if(self.index >= self.posterSize ){
                self.index = 0;
            }
            self.showImg(self.index);
            this.timer = window.setInterval(function(){
                self.autoPlay();
            },this.setting.delay);
        },

        //获取设置的参数
        getSetting : function(){
            var setting = this.poster.attr("data-setting");
            if( setting && setting != ""){
                return $.parseJSON(setting);
            }
            return {};
        },

        //showImg
        showImg : function(index){
            var _this = this;
            this.isanimated = false;
            this.posterItem.eq(index).addClass("on").animate({opacity:1,"z-index":0},this.speed,function(){
                _this.isanimated = true;
            }).siblings().removeClass("on").animate({opacity:0,"z-index":-1},this.speed);
            $(".pic-num span").eq(index).addClass("on").siblings(".pic-num span").removeClass("on");
        }
    }

    //初始化函数
    Carousel.init = function(poster){
        var _this = this;
        poster.each(function(){
            new _this($(this));
        });
    }

    window["Carousel"] = Carousel;

})(jQuery);

html代码

css代码

请问呢该如何解决呢?求大神

阿神
阿神

闭关修行中......

全部回复(1)
怪我咯

~1.this.index = -1;~
~2.data-setting='{"width":1000,"height":200,"autoPlay":true}'~

构造函数的默认先手动调用一下:

self.showImg(0);

先将opacity属性初始化到元素上~~~

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号