温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Wordpress将选中内容分享到新浪微博的方法

发布时间:2020-08-18 14:32:21 来源:亿速云 阅读:134 作者:小新 栏目:建站服务器

这篇文章主要介绍了Wordpress将选中内容分享到新浪微博的方法,具有一定借鉴价值,需要的朋友可以参考下。希望大家阅读完这篇文章后大有收获。下面让小编带着大家一起了解一下。

Wordpress怎么将选中内容分享到新浪微博?

1、引入jQuery,相信大多数WordPress博客都已经引入了jQuery,那就可以直接进行第二步了.

2、在页面底部,或者更确切的说,在引入jQuery库的后面加上这样一段JS,你就可以看到和本站一样的效果了.

选中即分享的功能看上去比较高级,其实实现是相当简单的,其中的会让人头大,一般人也不感兴趣的原理这里就直接跳过,这个js文字选中后分享到新浪微博的功能我简单的封装了下,方法名是:$sinaMiniBlogShare

实例代码如下:

代码如下:

var miniBlogShare = function() { 
//指定位置驻入节点 
$('<img id="imgSinaShare" class="img_share" title="将选中内容分享到新浪微博" src="1328255868614.gif" /><img id="imgQqShare" class="img_share" title="将选中内容分享到腾讯微博" src="/1328255868314.png" />').appendTo('body'); 
//默认样式 
$('.img_share').css({ 
display : 'none', 
position : 'absolute', 
cursor : 'pointer' 
}); 
//选中文字 
var funGetSelectTxt = function() { 
var txt = ''; 
if(document.selection) { 
txt = document.selection.createRange().text; 
} else { 
txt = document.getSelection(); 
} 
return txt.toString(); 
}; 
//选中文字后显示微博图标 
$('html,body').mouseup(function(e) { 
if (e.target.id == 'imgSinaShare' || e.target.id == 'imgQqShare') { 
return 
} 
e = e || window.event; 
var txt = funGetSelectTxt(), 
sh = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0, 
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) { 
$('#imgSinaShare').css({ 
display : 'inline', 
left : left, 
top : top 
}); 
$('#imgQqShare').css({ 
display : 'inline', 
left : left + 30, 
top : top 
}); 
} else { 
$('#imgSinaShare').css('display', 'none'); 
$('#imgQqShare').css('display', 'none'); 
} 
}); 
//点击新浪微博 
$('#imgSinaShare').click(function() { 
var txt = funGetSelectTxt(), title = $('title').html(); 
if (txt) { 
window.open('http://v.t.sina.com.cn/share/share.php?title=' + txt + ' —— 转载自:' + title + '&url=' + window.location.href); 
} 
}); 
//点击腾讯微博 
$('#imgQqShare').click(function() { 
var txt = funGetSelectTxt(), title = $('title').html(); 
if (txt) { 
window.open('http://v.t.qq.com/share/share.php?title=' + encodeURIComponent(txt + ' —— 转载自:' + title) + '&url=' + window.location.href); 
} 
}); 
}();

可以看到$sinaMiniBlogShare方法有两个参数,eleShare和eleContainer,其中,前一个参数是必须的,指的是文字选中后出现的浮动层元素(在本文demo中就是新浪眼睛图标),后面一个参数指文字选择的容器元素,可选参数,如果不设置则指document元素,也就是整个页面文字选中都会触发分享的功能.

假设新浪微博分享图标的HTML如下:

代码如下:

<img id="imgSinaShare" class="img_sina_share" title="将选中内容分享到新浪微博" src="http://simg.sinajs.cn/blog7style/images/common/share.gif" />

则直接使用如下代码:

代码如下:

$sinaMiniBlogShare(document.getElementById("imgSinaShare"));

感谢你能够认真阅读完这篇文章,希望小编分享Wordpress将选中内容分享到新浪微博的方法内容对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,遇到问题就找亿速云,详细的解决方法等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI