天天看點

h5微聊-高仿微信聊天界面實戰案例分享

h5仿微信|h5仿微信界面|h5仿微信聊天(單聊、多聊)

h5微聊場景——高仿微信聊天實戰項目,可單聊、多聊操作,運用到html5+css3+zepto+weui+wcPop+swiper等多種技術,實作了預覽圖檔(輕按兩下放大、手指縮放)、 播放視訊及發送消息、表情,打賞、霸屏、發紅包等功能,所有彈窗使用wcPop插件統一風格,長按消息可以浮動定位彈出操作菜單。

項目效果圖:
h5微聊-高仿微信聊天界面實戰案例分享
h5微聊-高仿微信聊天界面實戰案例分享
h5微聊-高仿微信聊天界面實戰案例分享
h5微聊-高仿微信聊天界面實戰案例分享
h5微聊-高仿微信聊天界面實戰案例分享
h5微聊-高仿微信聊天界面實戰案例分享
h5微聊-高仿微信聊天界面實戰案例分享
h5微聊-高仿微信聊天界面實戰案例分享

// ...表情、選擇區切換
$(".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();
	}
});           
h5微聊-高仿微信聊天界面實戰案例分享