class Index { constructor() { this.UserInfo = JSON.parse(localStorage.getItem("UserInfo")) this.onSocket() this.bindIconClick() this.RenderEmoji() } onSocket() { // 设置用户的头像和名字 $(".main_header span").text(this.UserInfo.name) $(".main_header img").attr("src", `../img/${this.UserInfo.url}`) // 显示欢迎信息 ws.socket.on("WelcomeUser", (res) => { $(".height").append(`
${res}
${res.name}
`)
}
// 为动图绑定事件
$(".gif img").on("click", function () {
slec.UserInfo.message = $(this)[0].outerHTML;
ws.socket.emit("SendMessage", slec.UserInfo)
$(".height").append(`
`);
$(".left_chat").animate({ scrollTop: $(".height").outerHeight() }, 1);
$(".main_input textarea").val("")
})
$('.font').click(function(e){
e.stopPropagation()
})
// 选择字体大小
$('.font select').change(function (e) {
var data = $(this).val();
$(".main_input textarea").css("font-size", data)
});
// 设置颜色
$('.font input').bind("input propertychange", function (e) {
$(".main_input textarea").css("color", $(this).val())
})
// 设置颜色
$('.qipao input').bind("input propertychange", function (e) {
console.log("11111");
$(".qipao_select").css("color", $(this).val())
})
}
/**
* 在光标的指定位置插入字符
* @param {*} myValue
*/
innerEmoji(myValue) {
var $t = $(".main_input textarea")[0];
if ($t.selectionStart || $t.selectionStart == "0") {
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.substring(endPos, $t.value.length);
$t.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
} else {
this.value += myValue;
$t.focus();
}
}
/**
* 为底部工具栏图标绑定点击事件
*/
bindIconClick() {
var self = this;
var isShowEmoji = false;
var isShowGif = false;
$(".tools i").on("click", function () {
switch ($(this).index()) {
// 表情 被点击
case 0:
console.log("表情");
if (isShowEmoji) {
$(".emoji").css({
width: 0,
height: 0
});
isShowEmoji = false
} else {
$(".emoji").css({
width: "402px",
height: "200px"
});
isShowEmoji = true
}
break;
case 1:
console.log("动图");
if (isShowGif) {
$(".gif").css({
width: 0,
height: 0
});
isShowGif = false
} else {
$(".gif").css({
width: "415px",
height: "200px"
});
isShowGif = true
}
break;
case 2:
console.log("字体");
$('.font').toggle()
break;
// 抖动 被点击
case 3:
console.log("抖动");
ws.socket.emit("Shake", self.UserInfo)
break;
case 4:
console.log("气泡");
$('.qipao').toggle()
break;
}
})
}
}
new Index()