Skip to content

Instantly share code, notes, and snippets.

@kingwrcy
Created January 11, 2013 11:48
Show Gist options
  • Save kingwrcy/4510137 to your computer and use it in GitHub Desktop.
Save kingwrcy/4510137 to your computer and use it in GitHub Desktop.
qq face
// QQ表情插件
(function($){
var face_cn = ['微笑','撇嘴','色','发呆','流泪','害羞','闭嘴','睡','大哭','尴尬','发怒','调皮','龇牙','惊讶','难过',
'冷汗','抓狂','吐','偷笑','可爱','白眼','傲慢','饥饿','困','惊恐','流汗','憨笑','大兵','奋斗','咒骂','疑问','嘘','晕',
'折磨','衰','敲打','再见','擦汗','抠鼻','糗大了','坏笑','左哼哼','右哼哼','哈欠','鄙视','委屈','快哭了','阴险','亲亲',
'吓','可怜','抱抱','月亮','太阳','炸弹','骷髅','菜刀','猪','西瓜','咖啡','米饭','心碎','强','弱','握手','胜利','抱拳','勾引','OK','NO',
'玫瑰','凋谢','示爱','爱情','飞吻'];
$.fn.qqFace = function(options){
var defaults = {
id : 'facebox',
path : 'face/',
assign : 'content',
tip : '表情'
};
var option = $.extend(defaults, options);
var assign = $('#'+option.assign);
var id = option.id;
var path = option.path;
var tip = option.tip;
if(assign.length<=0){
alert('缺少表情赋值对象。');
return false;
}
$(this).click(function(e){
var strFace, labFace;
if($('#'+id).length<=0){
strFace = '<div id="'+id+'" style="position:absolute;display:none;z-index:1000;" class="qqFace">' +
'<table border="0" cellspacing="0" cellpadding="0"><tr>';
for(var i=1; i<=75; i++){
labFace = '[/'+face_cn[i-1]+']';
strFace += '<td><img src="'+path+i+'.gif" title="'+face_cn[i-1]+'" onclick="$(\'#'+option.assign+'\').setCaret();$(\'#'+option.assign+'\').insertAtCaret(\'' + labFace + '\');" /></td>';
if( i % 15 == 0 ) strFace += '</tr><tr>';
}
strFace += '</tr></table></div>';
}
$('body').append(strFace);
var offset = $(this).offset();
console.log($(this).outerHeight(),offset.top,offset.left);
var top = offset.top + $(this).outerHeight();
$('#'+id).css('top',top);
$('#'+id).css('left',offset.left);
$('#'+id).show();
e.stopPropagation();
});
$(document).click(function(){
$('#'+id).hide();
$('#'+id).remove();
});
};
})(jQuery);
jQuery.extend({
unselectContents: function(){
if(window.getSelection)
window.getSelection().removeAllRanges();
else if(document.selection)
document.selection.empty();
}
});
jQuery.fn.extend({
selectContents: function(){
$(this).each(function(i){
var node = this;
var selection, range, doc, win;
if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined'){
range = doc.createRange();
range.selectNode(node);
if(i == 0){
selection.removeAllRanges();
}
selection.addRange(range);
} else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())){
range.moveToElementText(node);
range.select();
}
});
},
setCaret: function(){
if(!$.browser.msie) return;
var initSetCaret = function(){
var textObj = $(this).get(0);
textObj.caretPos = document.selection.createRange().duplicate();
};
$(this).click(initSetCaret).select(initSetCaret).keyup(initSetCaret);
},
insertAtCaret: function(textFeildValue){
var textObj = $(this).get(0);
if(document.all && textObj.createTextRange && textObj.caretPos){
var caretPos=textObj.caretPos;
caretPos.text = caretPos.text.charAt(caretPos.text.length-1) == '' ?
textFeildValue+'' : textFeildValue;
} else if(textObj.setSelectionRange){
var rangeStart=textObj.selectionStart;
var rangeEnd=textObj.selectionEnd;
var tempStr1=textObj.value.substring(0,rangeStart);
var tempStr2=textObj.value.substring(rangeEnd);
textObj.value=tempStr1+textFeildValue+tempStr2;
textObj.focus();
var len=textFeildValue.length;
textObj.setSelectionRange(rangeStart+len,rangeStart+len);
textObj.blur();
}else{
textObj.value+=textFeildValue;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment