(function (window) {
var bar = function (obj) {
return new bar.fn.init(obj);
}
bar.fn = bar.prototype = {
init: function (obj) {
for (var k in obj) {
this.config[k] = obj[k];
}
if (typeof this.config.element == "string") {
this.config.element = document.getElementById(this.config.element)
}
this.ready();
return this;
},
config: {
element: null,
bbox: null,
width: null,
height: null,
size: "2.5rem",
rowheight: 50,
opacity: .7,
time: 10,
zindex: 1
},
ready: function () {
var bbox = this.config.bbox = document.createElement("div");
bbox.style.position = "absolute";
bbox.style.color = "white";
bbox.style.fontSize = this.config.size;
bbox.style.width = (this.config.width || this.config.element.offsetWidth) + "px";
bbox.style.height = (this.config.height || (this.config.element.offsetHeight - 250)) + "px";
bbox.style.opacity = this.config.opacity;
bbox.style.zIndex = this.config.zindex;
this.config.element.appendChild(bbox);
},
add: function (msg, css) {
var bnode = document.createElement('span');
bnode.innerText = msg;
bnode.style.cssText = css;
this.config.bbox.appendChild(bnode);
return bnode;
}
}
bar.fn.init.prototype = bar.fn;
window.barrage = bar;
})(window);
barrage.douyu = function () {
var ba = barrage({
element: "douyu_room_normal_player_proxy_box"
});
var arrid = [],
gettime = 300,
bmaxrow = Math.floor(ba.config.bbox.style.height.replace("px", "") / ba.config.rowheight),
crow = 0;
ba.si = setInterval(function () {
var lis = document.getElementById('js-barrage-list').children, len = lis.length, si = Math.max(0, len - bmaxrow);
for (var i = si; i < len; i++) {
var li = lis[i];
if (arrid.indexOf(li.id) == -1) {
if (arrid.length > 200) {
arrid.splice(arrid.length - 1, 1);
}
arrid.push(li.id);
ba.arrid = arrid;
var sps = li.getElementsByTagName('span');
var msg = sps[sps.length - 1].innerText;
var bnode = ba.add(msg, "position:absolute;left:" + ba.config.bbox.style.width + ";transition:left " + ba.config.time + "s linear;top:" + (crow * ba.config.rowheight) + "px;white-space:nowrap;");
crow++ >= bmaxrow && (crow = 0);
bnode.style.left = -1 * bnode.offsetWidth + "px";
bnode.start = new Date().valueOf();
}
}
var clis = ba.config.bbox.children;
for (var i = 0; i < clis.length; i++) {
var li = clis[i];
if (new Date().valueOf() - li.start > ba.config.time * 1000) {
ba.config.bbox.removeChild(li);
}
}
}, gettime)
document.body.onresize = function () {
ba.config.element.removeChild(ba.config.bbox);
ba.ready();
};
return ba;
}
var douyu = barrage.douyu();