電視劇大圖滾動

//電視劇大圖滾動
(function(jq){
    var posterTvGrid = function(o, options, data){
        this.parent = jq("#"+ o);
        this.body  = jq("body");
        if (this.parent.length <= 0) { 
            return false;
        }
        
        this.options = jq.extend({}, posterTvGrid.options, options);
        if(typeof(data) !== 'object') return false;

        this.data = data || {};
        this.reset();
        //處理頁面resize
        var _this = this;
        jq(window).resize(function(){
            _this.reset();
        });
    };
    posterTvGrid.prototype = {
        reset: function(options){
            if(typeof(options) == 'object'){
                jq.extend(this.options, options);    
            }
            if(parseInt(this.body.outerWidth())>1255 || navigator.userAgent.indexOf('iPad') !== -1){
                this.options.width = 1190;    
            }else{
                this.options.width = 970;    
            }
            this.total = this.data.length;
            this.pageNow = this.options.initPage;
            this.preLeft = 0;
            this.nextLeft = this.options.width-530;
            this.preNLeft = -530;
            this.nextNLeft = this.options.width;
            this.pageNowLeft = (this.options.width-640)/2;
            this.drawContent();
        },
        drawContent: function(){
            this.parent.empty();
            this.parent.css({width:this.options.width+"px", height:this.options.height+"px", position: "relative"});
            this.content = document.createElement("DIV");
            this.content.className = this.options.className;
            this.content.cssText = "width:"+this.options.width+"px;height:"+this.options.height+"px;cursor:move;position:absolute;";
                this.bottomNav = document.createElement("DIV");
                this.bottomNav.className = "bottomNav";
                for(var i=1; i<= this.total; i++){
                    var bottomItem = document.createElement("DIV");
                    bottomItem.className = "bottomNavButtonOFF";
                    if(i == this.pageNow){
                        bottomItem.className = "bottomNavButtonOFF bottomNavButtonON";
                    }
                    bottomItem.setAttribute("ref", i);
                    this.bottomNav.appendChild(bottomItem);
                }
                this.content.appendChild(this.bottomNav);
                this.bannerControls = '<div class="bannerControls"> <div class="leftNav" style="display: block;"></div> <div class="rightNav" style="display: block;"></div> </div>';
                this.content.innerHTML += this.bannerControls;

            this.contentHolder = document.createElement("DIV");
            this.contentHolder.style.width = this.options.width + 'px';
            this.contentHolder.style.height = this.options.height + 'px';
            
            for(var item=0, i = 1, l = this.data.length ; item < l ; ++item, ++i){
                var contentHolderUnit = document.createElement("DIV");
                contentHolderUnit.className = "contentHolderUnit";
                contentHolderUnit.setAttribute("ref", i);
                contentHolderUnit.setAttribute("id", 'contentHolderUnit' + i);
                var unitItem = '<a href="'+this.data[item].url+'" target="_blank" class="elementLink">';
                unitItem += '</a>';
                unitItem += '<img src="'+this.data[item].img+'" alt="'+this.data[item].title+'"/>';
                unitItem += '<span class="elementOverlay"></span>';
                unitItem += '<span class="leftShadow"></span>';
                unitItem += '<span class="rightShadow"></span>';
                contentHolderUnit.innerHTML = unitItem;
                this.contentHolder.appendChild(contentHolderUnit);
            }
            this.content.appendChild(this.contentHolder);
            this.parent.append(this.content);
            this.parent.css({overflow:'hidden'});
            this.initContent();
            this.bind();
            this.start();
        },
        initContent: function(){
            contentHolderUnit = this.parent.find(".contentHolderUnit");
            contentHolderUnit.css({width:'0px',height:'0px', opacity: 0, left:this.options.width/2+'px', zIndex:0, marginTop: '135px'});
            this.parent.find(".contentHolderUnit:nth-child("+this.pageNow+")").css({width:'640px',height:'270px', opacity: 1, left: this.pageNowLeft+'px', zIndex: 3, marginTop: 0});
            this.parent.find(".contentHolderUnit:nth-child("+this.pageNow+") .elementOverlay").css({opacity:0});
            this.parent.find(".contentHolderUnit:nth-child("+this.pageNow+") .leftShadow").css({opacity:1});
            this.parent.find(".contentHolderUnit:nth-child("+this.pageNow+") .rightShadow").css({opacity:1});
            
            var pre = this.pageNow > 1 ? this.pageNow -1: this.total;
            var next = this.pageNow == this.total ? 1 : this.pageNow + 1;
            this.parent.find(".contentHolderUnit:nth-child("+pre+")").css({opacity: 1, left: this.preLeft+'px',height: '224px', width: '530px', zIndex: 0, marginTop: '23px'});
            this.parent.find(".contentHolderUnit:nth-child("+pre+") .elementOverlay").css({opacity:0.4});
            this.parent.find(".contentHolderUnit:nth-child("+pre+") .leftShadow").css({opacity:0});
            this.parent.find(".contentHolderUnit:nth-child("+pre+") .rightShadow").css({opacity:0});

            this.parent.find(".contentHolderUnit:nth-child("+next+")").css({opacity: 1, left: this.nextLeft+'px',height: '224px', width: '530px', zIndex: 0, marginTop: '23px'});
            this.parent.find(".contentHolderUnit:nth-child("+next+") .elementOverlay").css({opacity:0.4});
            this.parent.find(".contentHolderUnit:nth-child("+next+") .leftShadow").css({opacity:0});
            this.parent.find(".contentHolderUnit:nth-child("+next+") .rightShadow").css({opacity:0});
        },
        bind: function(){
            this.leftNav = this.parent.find(".leftNav");
            this.rightNav = this.parent.find(".rightNav");
            this.bottonNav = this.parent.find(".bottomNavButtonOFF");
            this.lists = this.parent.find(".contentHolderUnit");
            var _this = this;
            this.parent.mouseover(function(){
                _this.stop();
                console.log(666);
                _this.leftNav.show();
                _this.rightNav.show();
            });
            this.parent.mouseout(function(){
                _this.start();
                //_this.leftNav.hide();
                //_this.rightNav.hide();
            });
            
            _this.leftNav.click(function(){
                _this.turn("right");                    
            });
            _this.rightNav.click(function(){
                _this.turn("left");                    
            });
            _this.bottonNav.click(function(){
                var ref = parseInt(this.getAttribute("ref"));
                if(_this.pageNow == ref) return false;

                if(_this.pageNow < ref){
                    var rightAbs = ref - _this.pageNow;
                    var leftAbs = _this.pageNow + _this.total - ref;
                }else{
                    var rightAbs = _this.total - _this.pageNow + ref;
                    var leftAbs = _this.pageNow - ref;
                }
                if(leftAbs < rightAbs){
                    dir = "right";    
                }else{
                    dir = "left";    
                }

                _this.turnpage(ref, dir);
                return false;
            });
            
            _this.lists.click(function(e){
                var ref = parseInt(this.getAttribute("ref"));
                if(_this.pageNow == ref) {
                    return true;
                }else{
                    e.preventDefault();
                }
                if(_this.pageNow < ref){
                    var rightAbs = ref - _this.pageNow;
                    var leftAbs = _this.pageNow + _this.total - ref;
                }else{
                    var rightAbs = _this.total - _this.pageNow + ref;
                    var leftAbs = _this.pageNow - ref;
                }
                if(leftAbs < rightAbs){
                    dir = "right";    
                }else{
                    dir = "left";    
                }
                _this.turnpage(ref, dir);    

            });
        },
        initBottomNav: function(){
                this.parent.find(".bottomNavButtonOFF").removeClass("bottomNavButtonON");
                this.parent.find(".bottomNavButtonOFF:nth-child("+this.pageNow+")").addClass("bottomNavButtonON");
        },
        start: function(){
            var _this = this;
            if(_this.timerId) _this.stop();
            _this.timerId = setInterval(function(){
                if(_this.options.direct == "left"){
                    _this.turn("left");    
                }else{
                    _this.turn("right");    
                }
            }, _this.options.delay);
        },
        stop: function(){
            clearInterval(this.timerId);
        },
        turn: function(dir){
            var _this = this;
            
            if(dir == "right"){
                var page = _this.pageNow -1;
                if(page <= 0) page = _this.total;
            }else{
                var page = _this.pageNow + 1;
                if(page > _this.total) page = 1;
            }
            _this.turnpage(page, dir);
        },
        turnpage: function(page, dir){
            var _this = this;
            if(_this.locked) return false;
            _this.locked = true;
            if(_this.pageNow == page) return false;
            
            var run = function(page, dir, t){
                var pre = page > 1 ? page -1: _this.total;
                var next = page == _this.total ? 1 : page + 1;
                var preP = pre - 1 >= 1 ? pre-1 : _this.total;
                var nextN = next + 1 > _this.total ? 1 : next+1;
                if(pre != _this.pageNow && next != _this.pageNow){
                    var nowpre = _this.pageNow > 1 ? _this.pageNow -1: _this.total;
                    var nownext = _this.pageNow == _this.total ? 1 : _this.pageNow + 1;
                    _this.parent.find(".contentHolderUnit:nth-child("+nowpre+")").animate({width:'0px',height:'0px', opacity: 0, left:_this.options.width/2+'px', zIndex:0, marginTop: '135px'}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+_this.pageNow+")").animate({width:'0px',height:'0px', opacity: 0, left:_this.options.width/2+'px', zIndex:0, marginTop: '135px'}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+nownext+")").animate({width:'0px',height:'0px', opacity: 0, left:_this.options.width/2+'px', zIndex:0, marginTop: '135px'}, t);
                }
                if(dir == 'left'){                    
                    _this.parent.find(".contentHolderUnit:nth-child("+_this.pageNow+")").css({zIndex: 0});
                
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+") .elementOverlay").css({opacity:0.4});
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+")").animate({opacity: 1, left: _this.preLeft+'px', height: '224px', width: '530px', zIndex: 2, marginTop: '23px'}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+") .leftShadow").css({opacity:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+") .rightShadow").css({opacity:0});
                    
                    _this.parent.find(".contentHolderUnit:nth-child("+page+")").css({zIndex: 3});
                    _this.parent.find(".contentHolderUnit:nth-child("+page+")").animate({opacity: 1, left: _this.pageNowLeft+'px', height: '270px', width: '640px', zIndex: 3, marginTop: '0'}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+page+") .elementOverlay").css({opacity:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+page+") .leftShadow").css({opacity:1});
                    _this.parent.find(".contentHolderUnit:nth-child("+page+") .rightShadow").css({opacity:1});
                    
                    _this.parent.find(".contentHolderUnit:nth-child("+next+")").css({opacity: 0, left: _this.nextNLeft+'px', height: '100px', width: '530px', zIndex: 2, marginTop: '85px'});
                    _this.parent.find(".contentHolderUnit:nth-child("+next+")").animate({opacity: 1, left: _this.nextLeft+'px', height: '224px', width:"530px", zIndex: 2, marginTop: '23px'}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+next+") .elementOverlay").css({opacity:0.4});
                    _this.parent.find(".contentHolderUnit:nth-child("+next+") .leftShadow").css({opacity:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+next+") .rightShadow").css({opacity:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+preP+")").css({zIndex:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+preP+")").animate({width:'530px',height:'100px', opacity: 0, left:_this.preNLeft+'px', zIndex:0, marginTop: '85px'},t, "", function(){_this.locked=false;});
                    
                }else{
                    _this.parent.find(".contentHolderUnit:nth-child("+_this.pageNow+")").css({zIndex: 0});
                    
                    _this.parent.find(".contentHolderUnit:nth-child("+next+")").css({zIndex:2});
                    _this.parent.find(".contentHolderUnit:nth-child("+next+")").animate({opacity: 1, left: _this.nextLeft+'px', height: '224px', width: '530px', zIndex: 2, marginTop: '23px'}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+next+") .elementOverlay").css({opacity:0.4});
                    _this.parent.find(".contentHolderUnit:nth-child("+next+") .leftShadow").css({opacity:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+next+") .rightShadow").css({opacity:0});
                    
                    _this.parent.find(".contentHolderUnit:nth-child("+page+")").css({zIndex: 3}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+page+")").animate({opacity: 1, left: _this.pageNowLeft+'px', height: '270px', width: '640px', zIndex: 3, marginTop: '0px'}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+page+") .elementOverlay").css({opacity:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+page+") .leftShadow").css({opacity:1});
                    _this.parent.find(".contentHolderUnit:nth-child("+page+") .rightShadow").css({opacity:1});
                    
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+")").css({opacity: 0, left: _this.preNLeft+'px', height: '100px', width: '530px', zIndex: 2, marginTop: '85px'});
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+")").animate({opacity: 1, left: _this.preLeft+'px', height: '224px', width: '530px', zIndex: 2, marginTop: '23px'}, t);
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+") .elementOverlay").css({opacity:0.4});
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+") .leftShadow").css({opacity:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+pre+") .rightShadow").css({opacity:0});
                    
                    _this.parent.find(".contentHolderUnit:nth-child("+nextN+")").css({zIndex:0});
                    _this.parent.find(".contentHolderUnit:nth-child("+nextN+")").animate({width:'530px',height:'100px', opacity: 0, left:_this.nextNLeft+'px', zIndex:0, marginTop: '85px'}, t, "",function(){_this.locked=false;});
                }
            
                _this.pageNow = page;
                _this.initBottomNav();
            };
            
            run(page, dir,_this.options.speed);                    
            
        }
        
    };

    posterTvGrid.options = {
        offsetPages : 3,//默認(rèn)可視最大條數(shù)
        direct : "left",//滾動的方向
        initPage : 1,//默認(rèn)當(dāng)前顯示第幾條
        className : "posterTvGrid",//最外層樣式
        autoWidth : true,//默認(rèn)不用設(shè)置寬
        width : 970,//最外層寬,需要使用的時候在傳,默認(rèn)由程序自動判斷
        height : 310,//最外層高  
        delay : 5000,//滾動間隔(毫秒)
        speed : 500 //滾動速度毫秒
    };
    
    window.posterTvGrid = posterTvGrid;
})(jQuery);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市通砍,隨后出現(xiàn)的幾起案子炫贤,更是在濱河造成了極大的恐慌米丘,老刑警劉巖玩徊,帶你破解...
    沈念sama閱讀 212,454評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異奕枝,居然都是意外死亡哥谷,警方通過查閱死者的電腦和手機岸夯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,553評論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來们妥,“玉大人猜扮,你說我怎么就攤上這事〖嗌簦” “怎么了旅赢?”我有些...
    開封第一講書人閱讀 157,921評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我鲜漩,道長源譬,這世上最難降的妖魔是什么集惋? 我笑而不...
    開封第一講書人閱讀 56,648評論 1 284
  • 正文 為了忘掉前任孕似,我火速辦了婚禮,結(jié)果婚禮上刮刑,老公的妹妹穿的比我還像新娘喉祭。我一直安慰自己,他們只是感情好雷绢,可當(dāng)我...
    茶點故事閱讀 65,770評論 6 386
  • 文/花漫 我一把揭開白布泛烙。 她就那樣靜靜地躺著,像睡著了一般翘紊。 火紅的嫁衣襯著肌膚如雪蔽氨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,950評論 1 291
  • 那天帆疟,我揣著相機與錄音鹉究,去河邊找鬼。 笑死踪宠,一個胖子當(dāng)著我的面吹牛自赔,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播柳琢,決...
    沈念sama閱讀 39,090評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼绍妨,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了柬脸?” 一聲冷哼從身側(cè)響起他去,我...
    開封第一講書人閱讀 37,817評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎倒堕,沒想到半個月后灾测,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,275評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡涩馆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,592評論 2 327
  • 正文 我和宋清朗相戀三年行施,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片魂那。...
    茶點故事閱讀 38,724評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡蛾号,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出涯雅,到底是詐尸還是另有隱情鲜结,我是刑警寧澤,帶...
    沈念sama閱讀 34,409評論 4 333
  • 正文 年R本政府宣布,位于F島的核電站精刷,受9級特大地震影響拗胜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜怒允,卻給世界環(huán)境...
    茶點故事閱讀 40,052評論 3 316
  • 文/蒙蒙 一埂软、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧纫事,春花似錦勘畔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,815評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至钾唬,卻和暖如春万哪,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背抡秆。 一陣腳步聲響...
    開封第一講書人閱讀 32,043評論 1 266
  • 我被黑心中介騙來泰國打工奕巍, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人琅轧。 一個月前我還...
    沈念sama閱讀 46,503評論 2 361
  • 正文 我出身青樓伍绳,卻偏偏與公主長得像,于是被迫代替她去往敵國和親乍桂。 傳聞我的和親對象是個殘疾皇子冲杀,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 43,627評論 2 350

推薦閱讀更多精彩內(nèi)容