html5 高仿微信聊天示例|仿微信界面|仿微信聊天界面模板
利用html5+css3+jQ+wcPop+wcKeyboard+swiper等技術開發的仿微信聊天項目案例,前前後後開發調試了許久,實現了消息、表情發送,圖片、視頻查看,紅包、打賞、霸屏等微交互場景。
// ...滾動聊天區底部
function wchat_ToBottom(){
setTimeout(function(){
$(".wc__chatMsg-panel").animate({scrollTop: $("#J__chatMsgList").height()}, 0);
}, 10);
}
// ...點擊聊天面板區域
$(document).on("click", ".wc__chatMsg-panel", function(e){
var _tapMenu = $(".wc__chatTapMenu");
if(_tapMenu.length && e.target != _tapMenu && !$.contains(_tapMenu[0], e.target)){
// 關閉長按菜單
_tapMenu.hide();
$(".wc__chatMsg-panel").find("li .msg").removeClass("taped");
}
$(".wc__choose-panel").hide();
});
// ...表情、選擇區切換
$(".wc__editor-panel").on("click", ".btn", function(){
var that = $(this);
$(".wc__choose-panel").show();
if (that.hasClass("btn-emotion")) {
$(".wc__choose-panel .wrap-emotion").show();
$(".wc__choose-panel .wrap-choose").hide();
// 初始化swiper表情
!emotionSwiper && $("#J__emotionFootTab ul li.cur").trigger("click");
} else if (that.hasClass("btn-choose")) {
$(".wc__choose-panel .wrap-emotion").hide();
$(".wc__choose-panel .wrap-choose").show();
}
wchat_ToBottom();
});
// ...處理編輯器信息
var $editor = $(".J__wcEditor"), _editor = $editor[0];
function surrounds(){
setTimeout(function () { //chrome
var sel = window.getSelection();
var anchorNode = sel.anchorNode;
if (!anchorNode) return;
if (sel.anchorNode === _editor ||
(sel.anchorNode.nodeType === 3 && sel.anchorNode.parentNode === _editor)) {
var range = sel.getRangeAt(0);
var p = document.createElement("p");
range.surroundContents(p);
range.selectNodeContents(p);
range.insertNode(document.createElement("br")); //chrome
sel.collapse(p, 0);
(function clearBr() {
var elems = [].slice.call(_editor.children);
for (var i = 0, len = elems.length; i < len; i++) {
var el = elems[i];
if (el.tagName.toLowerCase() == "br") {
_editor.removeChild(el);
}
}
elems.length = 0;
})();
}
}, 10);
}
// 格式化編輯器包含標籤
_editor.addEventListener("click", function () {
$(".wc__choose-panel").hide();
}, true);
_editor.addEventListener("focus", function(){
surrounds();
}, true);
_editor.addEventListener("input", function(){
surrounds();
}, false);
// 點擊表情
$("#J__swiperEmotion").on("click", ".face-list span img", function(){
var that = $(this), range;
if(that.hasClass("face")){ //小表情
var img = that[0].cloneNode(true);
_editor.focus();
_editor.blur(); //輸入表情時禁止輸入法
setTimeout(function(){
if(document.selection && document.selection.createRange){
document.selection.createRange().pasteHTML(img);
}else if(window.getSelection && window.getSelection().getRangeAt){
range = window.getSelection().getRangeAt(0);
range.insertNode(img);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}
}, 10);
}else if(that.hasClass("del")){ //刪除
_editor.focus();
_editor.blur(); //輸入表情時禁止輸入法
setTimeout(function(){
range = window.getSelection().getRangeAt(0);
range.collapse(false);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
document.execCommand("delete");
}, 10);
} else if(that.hasClass("lg-face")){ //大表情
var _img = that.parent().html();
var _tpl = [
'<li class="me">\
<div class="content">\
<p class="author">Nice奶思</p>\
<div class="msg lgface">'+ _img + '</div>\
</div>\
<a class="avatar" href="微聊(好友主頁).html"><img src="img/uimg/u__chat-img14.jpg" /></a>\
</li>'
].join("");
$chatMsgList.append(_tpl);
wchat_ToBottom();
}
});