/* *PC大首页弹幕实现 * zhangcongfeng * 兼容ie8,9 * */ /** * [$ 拓展 jQuery 动画以支持暂停和继续] */ $(function () { 'use strict'; var pauseId = 'jQuery.pause', uuid = 1, oldAnimate = $.fn.animate, anims = {}; function now() { return new Date().getTime(); } $.fn.animate = function(prop, speed, easing, callback) { var optall = $.speed(speed, easing, callback); return this.each(function() { if (!this[pauseId]) { this[pauseId] = uuid++; } var opt = $.extend({}, optall); oldAnimate.apply($(this), [prop, $.extend({}, opt)]); anims[this[pauseId]] = { run: true, prop: prop, opt: opt, start: now(), done: 0 }; }); }; $.fn.pause = function() { return this.each(function() { if (! this[pauseId]){ this[pauseId] = uuid++; } var data = anims[this[pauseId]]; if (data && data.run) { data.done += now() - data.start; if (data.done > data.opt.duration) { delete anims[this[pauseId]]; } else { $(this).stop(); data.run = false; } } }); }; $.fn.resume = function() { return this.each(function() { if (! this[pauseId]){ this[pauseId] = uuid++; } var data = anims[this[pauseId]]; if (data && ! data.run) { data.opt.duration -= data.done; data.done = 0; data.run = true; data.start = now(); oldAnimate.apply($(this), [data.prop, $.extend({}, data.opt)]); } }); }; }); var ieFlag = !1; var browserName = navigator.userAgent; if (browserName.indexOf('MSIE 9.0') > 0 || browserName.indexOf('MSIE 8.0') > 0) { ieFlag = !0; } (function (w, f) { 'use strict'; window.Barrage = f(jQuery); })(window, function factory($) { 'use strict'; var timeArr = []; var timeout = []; function BarrageAtom(obj) { var that = this; that.prevTop = 0; for (var k in obj) { if (obj.hasOwnProperty(k)) { that[k] = obj[k]; } } that.init(); } BarrageAtom.prototype.init = function () { var that = this; var atom = $('
'); atom.find('a').attr('href',that.url); var moveStr = 'move ' + that.duration + 's ' + that.delay + 's ' + 'linear 1'; atom.css({ top: that.top, overflow: 'hidden', animation: moveStr, '-webkit-animation': moveStr, display: 'none' }).addClass(that.className); that.atom = atom; var danmu = that.wrapper; var closeBtn = $('#closeBtn'); var current = $('#banner_ctr').find('span[class="current"]'); // var currentDanmuIndex = $('.current').index('#banner_ctr span'); // console.log(currentDanmuIndex++); // if ($('.barragetda').length < 30) { // !ieFlag && danmu.append(that.atom); danmu.append(that.atom); // } if (current.attr('data-class') === 'close') { closeBtn.html('\u5f00\u542f\u5f39\u5e55'); }else { // ieFlag && danmu.append(that.atom.show()); that.atom.show(); closeBtn.html('\u5173\u95ed\u5f39\u5e55'); } var startTime = new Date().getTime(); timeArr.push(startTime); if (ieFlag) { var delay = that.delay * 1000; timeout.push(setTimeout(function () { that.animateJQ(); }, delay)); } else { danmu.on('mouseover','div',function () { var $that = $(this); $that.css('z-index','101'); $that.css('animation-play-state','paused'); $that.css('-webkit-animation-play-state','paused'); }); danmu.on('mouseout','div',function () { var $that = $(this); $that.css('z-index','99'); $that.css('animation-play-state','running'); $that.css('-webkit-animation-play-state','running'); }); } }; BarrageAtom.prototype.animateJQ = function () { var that = this; var atom = that.atom; var width = that.text.length * 14 + atom.width(); var duration = that.duration * 1000; atom.animate({ left: -width }, duration, 'linear', function () { atom.hide().css('left', '105%').delay(1000).show(); that.animateJQ(); }); }; function Barrage(obj) { this.textArr = obj.text; this.flagArr = obj.type; this.host = obj.host; this.url = obj.url; this.wrapper = $('#' + obj.wrapper); this.config = []; this.arr = []; this.line = obj.line || 4; this.tongji = obj.tongji; this.init(); } Barrage.prototype.init = function () { var barClose = $('.barclose'); if (barClose.css('display') === 'none') { barClose.show(); } var that = this; that.clearOut(); timeArr = []; var textArr = that.textArr; var flagArr = that.flagArr; var len = textArr.length; var line = that.line; for (var i = 0; i < len; i++) { var obj = {}; obj.className = flagArr[i] === 1 ? 'barragetd' : 'barragetda'; obj.text = textArr[i]; obj.url = that.url; obj.wrapper = that.wrapper; var topArr = [0,25,50,75]; var durationArr = [12,15,10,18]; var delayArr = [0,5.5,3,1.5]; obj.top = topArr[i % line]; obj.delay = delayArr[i % line] + Math.floor(i / line) * 7.5; obj.duration = durationArr[i % line]; that.arr.push(obj); var atomObj = new BarrageAtom(obj), atom = atomObj.atom; atom.hover(function () { $(this).css('z-index','101'); atom.pause && atom.pause(); }, function () { $(this).css('z-index','99'); atom.resume && atom.resume(); }); that.config.push(atomObj); } if (that.host) { $('#host').html('').append(that.host).show(); } if(this.tongji == 'ds') { $('.barclose').attr('id','xfdsxq_B21_02'); $('.bar01 .barragetda').attr('id','xfdsxq_B21_01'); }else if (this.tongji == 'pt') { $('.barclose').attr('id','xfptxq_B21_02'); $('.bar01 .barragetda').attr('id','xfptxq_B21_01'); } // setTimeout(function () { // window.intervalTimer = that.loop(); // },1000); }; Barrage.prototype.clearOut = function () { var timeLen = timeout.length; if (timeLen > 0) { for (var j = 0;j < timeLen;j++) { clearTimeout(timeout[j]); } timeout = []; } }; Barrage.prototype.close = function () { var closeBtn = $('#closeBtn'); var currentDanmuIndex = $('.current').index('#banner_ctr span'); if (currentDanmuIndex == -1) { currentDanmuIndex = 0; } var divArr = $('.barrage').children('.bar01').eq(currentDanmuIndex); var btnStr = ''; var current = $('#banner_ctr').find('span[class="current"]'); // if (ieFlag) { // divArr.toggle(); // if (divArr.children().length <= 0) { // alert('open'); // var that = this; // var arr = that.arr; // console.log(arr); // var len = arr.length; // for (var i = 0;i < len;i++) { // new BarrageAtom(arr[i]); // } // btnStr = '\u5173\u95ed\u5f39\u5e55'; // current.attr('data-class',''); // }else { // alert('close'); // divArr.hide().remove(); // this.clearOut(); // btnStr = '\u5f00\u542f\u5f39\u5e55'; // current.attr('data-class','close'); // } // }else { divArr.toggle(); if ($(".bannger_inbox").length ===1) { $('.barrage').toggle() } var danmuS = $('#danmu' + currentDanmuIndex).children('div'); if (divArr.css('display') === 'none') { btnStr = '\u5f00\u542f\u5f39\u5e55'; closeBtn.html(btnStr); current.attr('data-class','close'); $('.barrage').css('height', '0'); danmuS.pause(); }else { btnStr = '\u5173\u95ed\u5f39\u5e55'; closeBtn.html(btnStr); current.attr('data-class',''); $('.barrage').css('height', ''); danmuS.resume(); } // } }; Barrage.prototype.loop = function () { var that = this; var arr = that.arr; var len = arr.length; var index = len % 4; var maxDelay; switch(index) { case 0: maxDelay = arr[len - 2].delay; break; case 1: maxDelay = arr[len - 3].delay; break; case 2: case 3: maxDelay = arr[len - 1].delay; break; } var time = (maxDelay + 17 + 2.5) * 1000; // console.log(time); }; return Barrage; });