这个js是在网上找的,我们其实要做一个类似功能,这个js就是获取了鼠标事件鼠标有个点击选中事件,如果有选中就执行!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
<script type="text/javascript" > var sinaImg = '<img style="display: none; cursor: pointer; position: absolute; width:26px; height:26px;" id="imgSinaShare" title="将选中内容分享到新浪微博" src="http://simg.sinajs.cn/blog7style/images/common/share.gif"/>'; var qqImg = '<img style="display: none; cursor: pointer; position: absolute; width:25px; height:25px;" id="imgQqShare" title="将选中内容分享到腾讯微博" src="http://open.t.qq.com/images/resource/weiboicon32.png">'; document.write(sinaImg); document.write(qqImg); var sinaImgShare = document.getElementById("imgSinaShare"); var qqImgShare = document.getElementById("imgQqShare"); var artMain = document.getElementById("share"); //需要实现选择转发到微博的标签块 var $miniBlogShare = function(sinaShare, qqShare, eleContainer) { var eleTitle = document.getElementsByTagName("title")[0]; eleContainer = eleContainer || document; var funGetSelectTxt = function() { var txt = ""; if (document.selection) { txt = document.selection.createRange().text; } else { txt = document.getSelection(); } return txt.toString(); }; eleContainer.onmouseup = function(e) { e = e || window.event; var txt = funGetSelectTxt(), sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0; var left = (e.clientX - 40 < 0) ? e.clientX + 20: e.clientX - 40, top = (e.clientY - 40 < 0) ? e.clientY + sh + 20: e.clientY + sh - 40; if (txt) { sinaShare.style.display = "inline"; sinaShare.style.left = left + "px"; sinaShare.style.top = top + "px"; qqShare.style.display = "inline"; qqShare.style.left = left + 30 + "px"; qqShare.style.top = top + "px"; } else { sinaShare.style.display = "none"; qqShare.style.display = "none"; } }; sinaShare.onclick = function() { var txt = funGetSelectTxt(), title = (eleTitle && eleTitle.innerHTML) ? eleTitle.innerHTML: "未命名页面"; if (txt) { window.open('http://v.t.sina.com.cn/share/share.php?ralateuid=2719305811&title=' + txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href); } }; qqShare.onclick = function() { var txt = funGetSelectTxt(), title = (eleTitle && eleTitle.innerHTML) ? eleTitle.innerHTML: "未命名页面"; if (txt) { window.open('http://v.t.qq.com/share/share.php?title=' + encodeURIComponent(txt + '→来自页面"' + title + '"的文字片段&url=' + window.location.href)); } }; } (sinaImgShare, qqImgShare, artMain); </script> |
本站链接: http://www.521php.com/archives/1152/
程序本天成,妙手偶得之!我们只是代码的搬运工!
转载请注明:http://www.521php.com/archives/1152/