引入插件
index.html:
<html lang="en" id="page-root">
<script src="./js/danmaku.js"></script>
<script src="./js/index.js"></script>
准备弹幕容器
index.html:
<div id="my_container"></div>
实现效果
index.js:
$(function () {
initDanmu();
setDanmuData();
})
let damuPool = [
{ name1:'发文件哦我哦我回家哦',name2:'发v可发货v每次上课',tx1: './images/tx.jpg', tx2:'./images/tx.jpg',gift:'./images/cp/gift.png' },
{ name1:'那一天一体化不会同意',name2:'吃吧读个瑞哥让',tx1: './images/tx.jpg', tx2:'./images/tx.jpg',gift:'./images/cp/gift.png' },
{ name1:'我确认你好体育课iu',name2:'留挂号费大V擦深V大',tx1: './images/tx.jpg', tx2:'./images/tx.jpg',gift:'./images/cp/gift.png' },
{ name1:'cbru7yi范围及天气',name2:'feryhynb8bj57u6b67h',tx1: './images/tx.jpg', tx2:'./images/tx.jpg',gift:'./images/cp/gift.png' }
]
let danmu = []
let danmaku
const initDanmu = () => {
danmaku = new Danmaku({
container: document.getElementById('my_container'),
speed: 50,
})
}
const setDanmuData = () => {
setInterval(() => {
if (!danmu.length) {
danmu = [...damuPool]
}
let danmushoot = danmu.shift();
if (danmushoot) {
renderDM(danmushoot);
}
}, 2000);
}
const renderDM = (item) => {
danmaku.emit({
render: function () {
var $div = document.createElement('div');
var $div1 = document.createElement('div');
var $img1 = document.createElement('img');
var $img2 = document.createElement('img');
var $img3 = document.createElement('img');
var $div2 = document.createElement('div');
var $p1 = document.createElement('p');
var $p2 = document.createElement('p');
$img1.src = item.tx1;
$img2.src = item.gift;
$img3.src = item.tx2;
$p1.innerHTML = item.name1.length > 7 ? item.name1.slice(0, 7) + '...' : item.name1;
$p2.innerHTML = item.name2.length > 7 ? item.name2.slice(0, 7) + '...' : item.name2;
$div.style = `width: 6.2666666667rem; height: 4rem; background: url(./images/cp/cpBg.png) no-repeat; background-size: 100% 100%; overflow: hidden;`;
$div1.style = `display: flex;align-items: center;justify-content: center;margin: 0.92rem auto 0;`;
$img1.style = `width: 1.32rem;height: 1.32rem;box-sizing: border-box;border: 0.0266666667rem solid #fdf7c3;border-radius: 50%;`;
$img2.style = `width: 1.2533333333rem;`;
$img3.style = `width: 1.32rem;height: 1.32rem;box-sizing: border-box;border: 0.0266666667rem solid #fdf7c3;border-radius: 50%;`;
$div2.style = `display: flex;align-items: center;justify-content: center;margin: 0.0666666667rem auto 0;`;
$p1.style = `width: 2.6666666667rem;height: 0.5333333333rem;background: url(./images/nameBg2.png) no-repeat;background-size: 100% 100%;font-size: 0.32rem;font-weight: 500;color: #FFFFFF;line-height: 0.5066666667rem;text-align: center;`;
$p2.style = `width: 2.6666666667rem;height: 0.5333333333rem;background: url(./images/nameBg2.png) no-repeat;background-size: 100% 100%;font-size: 0.32rem;font-weight: 500;color: #FFFFFF;line-height: 0.5066666667rem;text-align: center;`;
$div.appendChild($div1);
$div1.appendChild($img1);
$div1.appendChild($img2);
$div1.appendChild($img3);
$div.appendChild($div2);
$div2.appendChild($p1);
$div2.appendChild($p2);
return $div;
},
})
}
$('.myqs').click(() => {
damuPool.push({
name1:'dsfkjwjflksdhglkahdlksg',
name2:'eityjnbmcnfhgkjvnxzc',
tx1: './images/tx.jpg',
tx2:'./images/tx.jpg',
gift:'./images/cp/gift.png'
})
setDanmuData();
})
具体api可参考
danmaku.js插件的github,点击查看