chengfeng6
<div id="share">
            <a href="javascript:void(0)"  share-type="qzone"  style="background-position: 0 0;"     target="_blank"  title="分享到QQ空间"></a>
            <a href="javascript:void(0)"  share-type="tsina"  style="background-position: -35px 0;" target="_blank"  title="分享到新浪微博"></a>
            <a href="javascript:void(0)"  share-type="weixin" style="background-position: -70px 0;" target="_blank"  title="分享到微信朋友圈"><div class="erweima"><div id="qrcode"></div></div></a>
        </div>

这是html;

下边是JS

$(function () {

    var qrcode = new QRCode(document.getElementById("qrcode"), {
        width : 160,//设置宽高
        height : 160
    });
    qrcode.makeCode(location.href);
    
    $(\'#share a\').on(\'click\',share);


    var shareDara = {
        pic:(function () {
            var pics = \'\'
            $(\'.contenr-main .text img\').each(function (i,k) {
                if(i<1){
                    pics +=  $(k).attr(\'src\')
                }else{
                    pics += \'||\'+$(k).attr(\'src\')
                }
            })
            return pics
        })(),
        title:(function () {
           return $(\'.contenr-main .title\').html()
        })(),
        url:location.href,
        desc:(function () {
            return $(\'.contenr-main .text p\').eq(0).html()
        })()
    }



    function share (){
        var self = $(this);
        var shareType = self.attr(\'share-type\');
            //console.log(shareType);
        switch (shareType){
            case \'weixin\':
                break;
            case \'tsina\':
                self.attr(\'href\',
                    \'http://service.weibo.com/share/share.php?\' +
                    \'url=\' +encodeURIComponent(shareDara.url)+
                    \'&title=\' +encodeURIComponent(shareDara.title)+
                    \'&appkey=93034926\' +
                    \'&pic=\'+encodeURIComponent(shareDara.pic)+
                    \'&searchPic=false\')
                break;
            case \'qzone\':
                self.attr(\'href\',
                    \'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?\' +
                    \'url=\'  +encodeURIComponent(shareDara.url)+
                    \'&title=\' +encodeURIComponent(shareDara.title)+
                    \'&desc=\' + encodeURIComponent(shareDara.desc)+
                    \'&summary=\' +encodeURIComponent(shareDara.pic)+
                    \'&site=\'
                    )
                break;
            default:
                break;

        }
    }

});

  再从网上下载一个qrcode.min.js用来生成二维码超级简单实用

分类:

技术点:

相关文章: