网页分享到微信、微博、QQ,QQ空间,豆瓣等等函数的封装

直接上代码! 

 //分享到新浪微博
    function shareToSinaWB(event){
        event.preventDefault();
        var _shareUrl = 'http://v.t.sina.com.cn/share/share.php?&appkey=895033136';     //真实的appkey,必选参数
        _shareUrl += '&url='+ encodeURIComponent(_url||document.location);     //参数url设置分享的内容链接|默认当前页location,可选参数
        _shareUrl += '&title=' + encodeURIComponent(_title||document.title);    //参数title设置分享的标题|默认当前页标题,可选参数
        _shareUrl += '&source=' + encodeURIComponent(_source||'');
        _shareUrl += '&sourceUrl=' + encodeURIComponent(_sourceUrl||'');
        _shareUrl += '&content=' + 'utf-8';   //参数content设置页面编码gb2312|utf-8,可选参数
        _shareUrl += '&pic=' + encodeURIComponent(_pic||'');  //参数pic设置图片链接|默认为空,可选参数
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',top='+_top+',left='+_left+',toolbar=no,menubar=no,scrollbars=no, resizable=1,location=no,status=0');
    }

    //分享到QQ空间
    function shareToQzone(event){
        event.preventDefault();

        var _shareUrl = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?';
        _shareUrl += 'url=' + encodeURIComponent(_url||document.location);   //参数url设置分享的内容链接|默认当前页location
        _shareUrl += '&showcount=' + _showcount||0;      //参数showcount是否显示分享总数,显示:'1',不显示:'0',默认不显示
        _shareUrl += '&desc=' + encodeURIComponent(_desc||'分享的描述');    //参数desc设置分享的描述,可选参数
        _shareUrl += '&summary=' + encodeURIComponent(_summary||'分享摘要');    //参数summary设置分享摘要,可选参数
        _shareUrl += '&title=' + encodeURIComponent(_title||document.title);    //参数title设置分享标题,可选参数
        _shareUrl += '&site=' + encodeURIComponent(_site||'');   //参数site设置分享来源,可选参数
        _shareUrl += '&pics=' + encodeURIComponent(_pic||'');   //参数pics设置分享图片的路径,多张图片以"|"隔开,可选参数
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',top='+_top+',left='+_left+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到百度贴吧
    function shareToTieba(event){
        event.preventDefault();

        var _shareUrl = 'http://tieba.baidu.com/f/commit/share/openShareApi?';
        _shareUrl += 'title=' + encodeURIComponent(_title||document.title);  //分享的标题
        _shareUrl += '&url=' + encodeURIComponent(_url||document.location);  //分享的链接
        _shareUrl += '&pic=' + encodeURIComponent(_pic||'');    //分享的图片
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到豆瓣
    function shareToDouban(event){
        event.preventDefault();

        var _shareUrl = 'http://shuo.douban.com/!service/share?';
        _shareUrl += 'href=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&name=' + encodeURIComponent(_title||document.title);    //分享的标题
        _shareUrl += '&image=' + encodeURIComponent(_pic||'');    //分享的图片
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }
    //分享到腾讯微博
    function shareToQQwb(event){
        event.preventDefault();

        var _shareUrl = 'http://v.t.qq.com/share/share.php?';
        _shareUrl += 'title=' + encodeURIComponent(_title||document.title);    //分享的标题
        _shareUrl += '&url=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&appkey=5bd32d6f1dff4725ba40338b233ff155';    //在腾迅微博平台创建应用获取微博AppKey
        _shareUrl += '&site=' + encodeURIComponent(_site||'');   //分享来源
        _shareUrl += '&pic=' + encodeURIComponent(_pic||'');    //分享的图片,如果是多张图片,则定义var _pic='图片url1|图片url2|图片url3....'
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }
    //分享到人人网
    function shareToRenren(event){
        event.preventDefault();

        var _shareUrl = 'http://share.renren.com/share/buttonshare.do?';
        _shareUrl += 'link=' + encodeURIComponent(_url||location.href);   //分享的链接
        _shareUrl += '&title=' + encodeURIComponent(_title||document.title);     //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到开心网
    function shareToKaixin(event){
        event.preventDefault();

        var _shareUrl = 'http://www.kaixin001.com/rest/records.php?';
        _shareUrl += 'url=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&content=' + encodeURIComponent('分享的文字');    //需要分享的文字,当文字为空时,自动抓取分享网址的title
        _shareUrl += '&pic=' + encodeURIComponent(_pic||'');     //分享的图片,多个使用半角逗号分隔
        _shareUrl += '&showcount=0';    //是否显示分享数,显示:'1',不显示:'0'
        _shareUrl += '&style=11';      //显示的样式,必选参数
        _shareUrl += '&aid=' + encodeURIComponent(_site||'');    //显示分享来源
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    function shareToKaixin2(event){
        event.preventDefault();

        var _shareUrl = 'http://www.kaixin001.com/repaste/share.php?';
        _shareUrl += 'rtitle=' + encodeURIComponent(_title||document.title);   //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到facebook
    function shareToFacebook(event){
        event.preventDefault();

        var _shareUrl = 'http://www.facebook.com/sharer/sharer.php?';
        _shareUrl += 'u=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&t=' + encodeURIComponent(_title||document.title);    //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }
    //分享到facebook
    function shareToFacebook(event){
        event.preventDefault();

        var _shareUrl = 'http://www.facebook.com/sharer/sharer.php?';
        _shareUrl += 'u=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&t=' + encodeURIComponent(_title||document.title);    //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到Twitter
    function shareToTwitter(event){
        event.preventDefault();

        var _shareUrl = 'http://twitter.com/intent/tweet?';
        _shareUrl += 'url=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&text=' + encodeURIComponent(_title||document.title);    //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

ps:因为没加二维码的图片,因此代码显示网页中的微信分享下没有图片 

用法给小白看看


<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0,user-scalable=no">
    <meta name="format-detection" content="telephone=no" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta http-equiv="pragma" content="no-cache" />
    <style>
        *{margin:0;padding:0;}
        img{border:0 none;}
        body{background:#eee;}
        .m-box{width:800px;margin:0 auto;padding:20px;background:#fff;}
        .m-box p{margin:0 0 10px;}
        .m-box .icn a{display:block;width:55px;height:35px;background:url('http://l.bst.126.net/rsc/img/weibo.png?035') no-repeat;}
        .m-box .icn .wb1{background-position:10px -216px;}
        .m-box .icn .wb2{background-position:-190px -216px;}
        .m-box .icn .wb3{background:url(http://l.bst.126.net/rsc/img/postshare/tieba24.png) 15px 0 no-repeat;}
        .m-box .icn .wb4{background-position:-88px -215px;}
        .m-box .icn .wb5{background-position:-138px -216px;}
        .m-box .icn .wb6{background-position:-245px -216px;}
        .m-box .icn .wb7{background-position:-300px -216px;}
        .m-box .icn .wb8{background-position:-355px -216px;}
        .m-box .icn .wb9{background-position:-415px -215px;}
    </style>
</head>

<body>
<div class="m-box">
    <p>分享到微信,易信:</p>
    <div class="icn"><img width="200" src="二维码图标的路径" /></div>
    <!--二维码生成地址:http://cli.im/-->
</div>

<div class="m-box">
    <p>分享到新浪微博:</p>
    <div class="icn"><a  class="wb1" onclick="shareToSinaWB(event)"></a></div>
</div>

<div class="m-box">
    <p>分享到QQ空间:</p>
    <div class="icn"><a  class="wb2" onclick="shareToQzone(event)"></a></div>
</div>

<div class="m-box">
    <p>分享到百度贴吧:</p>
    <div class="icn"><a  class="wb3" onclick="shareToTieba(event)"></a></div>
</div>

<div class="m-box">
    <p>分享到豆瓣:</p>
    <div class="icn"><a  class="wb4" onclick="shareToDouban(event)"></a></div>
</div>

<div class="m-box">
    <p>分享到腾迅微博:</p>
    <div class="icn"><a  class="wb5"  onclick="shareToQQwb(event)"></a></div>
</div>

<div class="m-box">
    <p>分享到人人网:</p>
    <div class="icn"><a  class="wb6" onclick="shareToRenren(event)"></a></div>
</div>

<div class="m-box">
    <p>分享到开心网:</p>
    <div class="icn"><a  class="wb7" onclick="shareToKaixin(event)"></a></div>
</div>

<div class="m-box">
    <p>分享到facebook:</p>
    <div class="icn"><a  class="wb8" onclick="shareToFacebook(event)"></a></div>
</div>
<div class="m-box">
    <p>分享到Twitter:</p>
    <div class="icn"><a  class="wb9" onclick="shareToTwitter(event)"></a></div>
</div>


<script>
    var _title,_source,_sourceUrl,_pic,_showcount,_desc,_summary,_site,
            _width = 600,
            _height = 600,
            _top = (screen.height-_height)/2,
            _left = (screen.width-_width)/2,
            _url = 'www.baidu.com',
            _pic = '';


    //分享到新浪微博
    function shareToSinaWB(event){
        event.preventDefault();
        var _shareUrl = 'http://v.t.sina.com.cn/share/share.php?&appkey=895033136';     //真实的appkey,必选参数
        _shareUrl += '&url='+ encodeURIComponent(_url||document.location);     //参数url设置分享的内容链接|默认当前页location,可选参数
        _shareUrl += '&title=' + encodeURIComponent(_title||document.title);    //参数title设置分享的标题|默认当前页标题,可选参数
        _shareUrl += '&source=' + encodeURIComponent(_source||'');
        _shareUrl += '&sourceUrl=' + encodeURIComponent(_sourceUrl||'');
        _shareUrl += '&content=' + 'utf-8';   //参数content设置页面编码gb2312|utf-8,可选参数
        _shareUrl += '&pic=' + encodeURIComponent(_pic||'');  //参数pic设置图片链接|默认为空,可选参数
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',top='+_top+',left='+_left+',toolbar=no,menubar=no,scrollbars=no, resizable=1,location=no,status=0');
    }

    //分享到QQ空间
    function shareToQzone(event){
        event.preventDefault();

        var _shareUrl = 'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?';
        _shareUrl += 'url=' + encodeURIComponent(_url||document.location);   //参数url设置分享的内容链接|默认当前页location
        _shareUrl += '&showcount=' + _showcount||0;      //参数showcount是否显示分享总数,显示:'1',不显示:'0',默认不显示
        _shareUrl += '&desc=' + encodeURIComponent(_desc||'分享的描述');    //参数desc设置分享的描述,可选参数
        _shareUrl += '&summary=' + encodeURIComponent(_summary||'分享摘要');    //参数summary设置分享摘要,可选参数
        _shareUrl += '&title=' + encodeURIComponent(_title||document.title);    //参数title设置分享标题,可选参数
        _shareUrl += '&site=' + encodeURIComponent(_site||'');   //参数site设置分享来源,可选参数
        _shareUrl += '&pics=' + encodeURIComponent(_pic||'');   //参数pics设置分享图片的路径,多张图片以"|"隔开,可选参数
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',top='+_top+',left='+_left+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到百度贴吧
    function shareToTieba(event){
        event.preventDefault();

        var _shareUrl = 'http://tieba.baidu.com/f/commit/share/openShareApi?';
        _shareUrl += 'title=' + encodeURIComponent(_title||document.title);  //分享的标题
        _shareUrl += '&url=' + encodeURIComponent(_url||document.location);  //分享的链接
        _shareUrl += '&pic=' + encodeURIComponent(_pic||'');    //分享的图片
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到豆瓣
    function shareToDouban(event){
        event.preventDefault();

        var _shareUrl = 'http://shuo.douban.com/!service/share?';
        _shareUrl += 'href=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&name=' + encodeURIComponent(_title||document.title);    //分享的标题
        _shareUrl += '&image=' + encodeURIComponent(_pic||'');    //分享的图片
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }
    //分享到腾讯微博
    function shareToQQwb(event){
        event.preventDefault();

        var _shareUrl = 'http://v.t.qq.com/share/share.php?';
        _shareUrl += 'title=' + encodeURIComponent(_title||document.title);    //分享的标题
        _shareUrl += '&url=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&appkey=5bd32d6f1dff4725ba40338b233ff155';    //在腾迅微博平台创建应用获取微博AppKey
        _shareUrl += '&site=' + encodeURIComponent(_site||'');   //分享来源
        _shareUrl += '&pic=' + encodeURIComponent(_pic||'');    //分享的图片,如果是多张图片,则定义var _pic='图片url1|图片url2|图片url3....'
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }
    //分享到人人网
    function shareToRenren(event){
        event.preventDefault();

        var _shareUrl = 'http://share.renren.com/share/buttonshare.do?';
        _shareUrl += 'link=' + encodeURIComponent(_url||location.href);   //分享的链接
        _shareUrl += '&title=' + encodeURIComponent(_title||document.title);     //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到开心网
    function shareToKaixin(event){
        event.preventDefault();

        var _shareUrl = 'http://www.kaixin001.com/rest/records.php?';
        _shareUrl += 'url=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&content=' + encodeURIComponent('分享的文字');    //需要分享的文字,当文字为空时,自动抓取分享网址的title
        _shareUrl += '&pic=' + encodeURIComponent(_pic||'');     //分享的图片,多个使用半角逗号分隔
        _shareUrl += '&showcount=0';    //是否显示分享数,显示:'1',不显示:'0'
        _shareUrl += '&style=11';      //显示的样式,必选参数
        _shareUrl += '&aid=' + encodeURIComponent(_site||'');    //显示分享来源
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    function shareToKaixin2(event){
        event.preventDefault();

        var _shareUrl = 'http://www.kaixin001.com/repaste/share.php?';
        _shareUrl += 'rtitle=' + encodeURIComponent(_title||document.title);   //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到facebook
    function shareToFacebook(event){
        event.preventDefault();

        var _shareUrl = 'http://www.facebook.com/sharer/sharer.php?';
        _shareUrl += 'u=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&t=' + encodeURIComponent(_title||document.title);    //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }
    //分享到facebook
    function shareToFacebook(event){
        event.preventDefault();

        var _shareUrl = 'http://www.facebook.com/sharer/sharer.php?';
        _shareUrl += 'u=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&t=' + encodeURIComponent(_title||document.title);    //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

    //分享到Twitter
    function shareToTwitter(event){
        event.preventDefault();

        var _shareUrl = 'http://twitter.com/intent/tweet?';
        _shareUrl += 'url=' + encodeURIComponent(_url||location.href);    //分享的链接
        _shareUrl += '&text=' + encodeURIComponent(_title||document.title);    //分享的标题
        window.open(_shareUrl,'_blank','width='+_width+',height='+_height+',left='+_left+',top='+_top+',toolbar=no,menubar=no,scrollbars=no,resizable=1,location=no,status=0');
    }

</script>

</body>
</html>

 

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个基于 ShareSDK 的封装代码,支持分享微信QQ微博等平台。具体步骤如下: 1. 引入 ShareSDK 库。 在 app 的 build.gradle 文件中添加以下依赖: ``` implementation 'cn.sharesdk:sharesdk:3.2.0' ``` 2. 在项目中注册 ShareSDK。 在 Application 的 onCreate() 方法中添加以下代码: ```java ShareSDK.initSDK(this); ``` 3. 添加分享平台。 ```java // 添加微信平台 PlatformConfig.setWeixin("微信 AppID", "微信 AppSecret"); // 添加QQ平台 PlatformConfig.setQQZone("QQ AppID", "QQ AppKey"); // 添加微博平台 PlatformConfig.setSinaWeibo("微博 AppKey", "微博 AppSecret", "回调地址"); ``` 4. 调用分享接口。 ```java /** * 分享到指定平台 * * @param platform 平台名称,如:Wechat.NAME 表示分享微信 * @param title 分享标题 * @param content 分享内容 * @param url 分享链接 * @param imageUrl 分享图片链接 */ public void share(String platform, String title, String content, String url, String imageUrl) { Platform.ShareParams shareParams = new Platform.ShareParams(); shareParams.setTitle(title); shareParams.setText(content); shareParams.setUrl(url); shareParams.setImageUrl(imageUrl); Platform platform = ShareSDK.getPlatform(platform); platform.share(shareParams); } ``` 其中 platform 参数为平台名称,可以使用以下常量: ```java Wechat.NAME // 分享微信 WechatMoments.NAME // 分享微信朋友圈 QQ.NAME // 分享QQ QZone.NAME // 分享QQ空间 SinaWeibo.NAME // 分享到新浪微博 ``` 示例代码如下: ```java share(Wechat.NAME, "分享标题", "分享内容", "http://www.example.com", "http://www.example.com/image.jpg"); ``` 这样就可以实现分享功能了。需要注意的是,分享功能需要用户安装对应的客户端才能生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值