h5微聊-高仿微信聊天界面实战案例分享

  1. 云栖社区>
  2. 博客>
  3. 正文

h5微聊-高仿微信聊天界面实战案例分享

xiaoyan2015 2018-06-02 00:55:23 浏览2942
展开阅读全文

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

h5微聊场景——高仿微信聊天实战项目,可单聊、多聊操作,运用到html5+css3+zepto+weui+wcPop+swiper等多种技术,实现了预览图片(双击放大、手指缩放)、 播放视频及发送消息、表情,打赏、霸屏、发红包等功能,所有弹窗使用wcPop插件统一风格,长按消息可以浮动定位弹出操作菜单。

项目效果图:

20dba57f9d0a54ecfc3e7f9e35f736b63927889c a067211083adb7d6a04d58f09a69b91c2d9e00ef

f7c09932d1ef6c0e61ccd20351aa363733be0bfa c6543a3c674613304e97b649eb37dfdfd90c7e3e

2a7abba44ed1729108e6f6cfb64af314c5dacc6e 4e77e8c3a51ba416203f93d347fc011ad706fe2e

709837b077704103dd27b293f69649b8558e155d 47a4cabf8c28061abb51cce137841865a7f636f4


// ...表情、选择区切换
$(".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();
	}
});


3d8b26f784f3793787752ea81c60f1c03d4bfefd


网友评论

登录后评论
0/500
评论
xiaoyan2015
+ 关注