class Index { constructor() { this.userinfo = JSON.parse(localStorage.getItem("userinfo")); this.fontSetConfig = { fontSize: '22px', color: '', bgColor: '' } this.setUserInfo(); this.golableOn(); this.sendMessage(); this.renderBiaoQing(); this.renderDouTu(); this.fontSet() this.renderDouDong(); } golableOn() { io.socket.emit("getUserOnline", this.userinfo.roomid); io.socket.on('UserOnline', (list) => { $(".right_header p").text(list.length) $(".right_content").empty(); list.forEach((v, i) => { $(".right_content").append(`

${v.username}

`) }) }); io.socket.on('sedMessage', (list) => { console.log("其他人收到的消息为: ", list) this.renderMessage(list, 'left') }) } setUserInfo() { $(".user img").attr("src", this.userinfo.img); $(".user p").text(this.userinfo.username); $(".rightlog").text(this.userinfo.roomid); } sendMessage() { $('.shuru').on('keydown', (event) => { if (event.keyCode == 13) { event.preventDefault(); if ($(".shuru").text().length > 0) { var data = Object.assign(this.userinfo, { message: `
${$(".shuru").html()}
` }); this.renderMessage(data); io.socket.emit("getMessage", data); $(".shuru").html("") } } }); } renderMessage(data, position = "right") { if (position == "right") { $(".left_content .chat_message").append(`

${data.username}

${data.message}
`); } else { $(".left_content .chat_message").append(`

${data.username}

${data.message}
`) }; $(".left_content").animate({ scrollTop: $(".chat_message").outerHeight() }, 1); } renderBiaoQing() { for (var i = 2; i <= 209; i++) { $(".bq").append(`
  • `) }; $(".bq li img").mouseover(function () { var oldImg = $(this).attr("src"); $(this).attr("src", oldImg.replace("png", "gif")) }); $(".bq li img").mouseout(function () { var oldImg = $(this).attr("src"); $(this).attr("src", oldImg.replace("gif", "png")) }); var a = this; $(".bq li img").click(function () { a.innerEmoji($(this).attr("src")) }); $(".icon-xiaolian1").click(function () { $(".bq").toggle(); }) } renderDouTu() { for (var i = 0; i <= 12; i++) { $(".dt").append(`
  • `) }; var a = this; $(".dt li img").click(function () { a.innerEmoji($(this).attr("src")) }); $(".icon-GIF").click(function () { $(".dt").toggle(); }) } innerEmoji(src) { if (window.getSelection) { var sel = window.getSelection(); var range = sel.getRangeAt(0); var img = new Image(); range.deleteContents() img.src = src; console.log(range); range.insertNode(img); range.collapse(false);//对于IE来说,参数不可省略 } } renderDouDong(){ $(".icon-doudong-shake").click(function () { $('body').addClass('active'); setTimeout(()=>{ $('body').removeClass('active') },1000); }) } fontSet() { var self = this; $(".fontSize").change(function () { self.fontSetConfig.fontSize = $(this).val(); console.log("self.fontSetConfig: ", self.fontSetConfig) }); $(".fontColor").change(function () { self.fontSetConfig.color = $(this).val(); }); $(".bgColor").change(function () { self.fontSetConfig.bgColor = $(this).val(); }); } } new Index();