前端如何做分享功能-偷偷学简书~

偷偷的告诉你:你可以在简书上学到前端分享的技术

image.jpg

今天无聊居然打开了简书页面的控制台Network,然后发现了一个接口,感觉发现了大秘密~


pic1.PNG

分享的功能居然在这里!!!

不要脸的我就去看了一下elements

固定侧边栏的分享按钮HTML

    <a tabindex="0" role="button" data-toggle="popover" data-placement="left" data-html="true" data-trigger="focus" href="javascript:void(0);" data-content="<ul class='share-list' style=&quot;width:220px&quot;>
                <li><a class=&quot;weixin-share&quot;><i class=&quot;social-icon-sprite social-icon-weixin&quot;></i><span>分享到微信</span></a></li>
                <li><a href=&quot;javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina.com.cn/share/share.php?appkey=1881139527',u=z||d.location,p=['&amp;url=',e(u),'&amp;title=',e(t||d.title),'&amp;source=',e(r),'&amp;sourceUrl=',e(l),'&amp;content=',c||'gb2312','&amp;pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','', '推荐 繁络 的文章《CSS选择器有哪些?》( 分享自 @简书 )','https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=weibo','页面编码gb2312|utf-8默认gb2312'));&quot;><i class='social-icon-sprite social-icon-weibo'></i><span>分享到微博</span></a></li>
                
                <li><a href=&quot;javascript:void(function(){var d=document,e=encodeURIComponent,r='http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+e('https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=qzone')+'&amp;title='+e('推荐 繁络 的文章《CSS选择器有哪些?》'),x=function(){if(!window.open(r,'qzone','toolbar=0,resizable=1,scrollbars=yes,status=1,width=600,height=600'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();&quot;><i class='social-icon-sprite social-icon-zone'></i><span>分享到QQ空间</span></a></li>
                <li><a href=&quot;javascript:void(function(){var d=document,e=encodeURIComponent,r='https://twitter.com/share?url='+e('https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=twitter')+'&amp;text='+e('推荐 繁络 的文章《CSS选择器有哪些?》( 分享自 @jianshucom )')+'&amp;related='+e('jianshucom'),x=function(){if(!window.open(r,'twitter','toolbar=0,resizable=1,scrollbars=yes,status=1,width=600,height=600'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();&quot;><i class='social-icon-sprite social-icon-twitter'></i><span>分享到Twitter</span></a></li>
                <li><a href=&quot;javascript:void(function(){var d=document,e=encodeURIComponent,r='https://www.facebook.com/dialog/share?app_id=483126645039390&amp;display=popup&amp;href=https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=facebook',x=function(){if(!window.open(r,'facebook','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();&quot;><i class='social-icon-sprite social-icon-facebook'></i><span>分享到Facebook</span></a></li>
                <li><a href=&quot;javascript:void(function(){var d=document,e=encodeURIComponent,r='https://plus.google.com/share?url='+e('https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=google_plus'),x=function(){if(!window.open(r,'google_plus','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();&quot;><i class='social-icon-sprite social-icon-google'></i><span>分享到Google+</span></a></li>
                <li><a href=&quot;javascript:void(function(){var d=document,e=encodeURIComponent,s1=window.getSelection,s2=d.getSelection,s3=d.selection,s=s1?s1():s2?s2():s3?s3.createRange().text:'',r='http://www.douban.com/recommend/?url='+e('https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&amp;utm_content=note&amp;utm_medium=reader_share&amp;utm_source=douban')+'&amp;title='+e('CSS选择器有哪些?')+'&amp;sel='+e(s)+'&amp;v=1',x=function(){if(!window.open(r,'douban','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r+'&amp;r=1'};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})()&quot;><i class='social-icon-sprite social-icon-douban'></i><span>分享到豆瓣</span></a></li>
              </ul>" data-original-title="" title="" class="function-button">
        <i class="iconfont ic-share"></i>
    </a>

这分享功能列表在data-content里面,并且以html格式保存

    <ul class='share-list' style="width:220px">
        <li>
            <a class="weixin-share">
                <i class="social-icon-sprite social-icon-weixin"></i>
                <span>分享到微信</span>
            </a>
        </li>
        <li>
            <a href="javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina.com.cn/share/share.php?appkey=1881139527',u=z||d.location,p=['&url=',e(u),'&title=',e(t||d.title),'&source=',e(r),'&sourceUrl=',e(l),'&content=',c||'gb2312','&pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','', '推荐 繁络 的文章《CSS选择器有哪些?》( 分享自 @简书 )','https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weibo','页面编码gb2312|utf-8默认gb2312'));">
                <i class='social-icon-sprite social-icon-weibo'></i>
                <span>分享到微博</span>
            </a>
        </li>

        <li>
            <a href="javascript:void(function(){var d=document,e=encodeURIComponent,r='http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url='+e('https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=qzone')+'&title='+e('推荐 繁络 的文章《CSS选择器有哪些?》'),x=function(){if(!window.open(r,'qzone','toolbar=0,resizable=1,scrollbars=yes,status=1,width=600,height=600'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();">
                <i class='social-icon-sprite social-icon-zone'></i>
                <span>分享到QQ空间</span>
            </a>
        </li>
        <li>
            <a href="javascript:void(function(){var d=document,e=encodeURIComponent,r='https://twitter.com/share?url='+e('https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=twitter')+'&text='+e('推荐 繁络 的文章《CSS选择器有哪些?》( 分享自 @jianshucom )')+'&related='+e('jianshucom'),x=function(){if(!window.open(r,'twitter','toolbar=0,resizable=1,scrollbars=yes,status=1,width=600,height=600'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();">
                <i class='social-icon-sprite social-icon-twitter'></i>
                <span>分享到Twitter</span>
            </a>
        </li>
        <li>
            <a href="javascript:void(function(){var d=document,e=encodeURIComponent,r='https://www.facebook.com/dialog/share?app_id=483126645039390&display=popup&href=https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=facebook',x=function(){if(!window.open(r,'facebook','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();">
                <i class='social-icon-sprite social-icon-facebook'></i>
                <span>分享到Facebook</span>
            </a>
        </li>
        <li>
            <a href="javascript:void(function(){var d=document,e=encodeURIComponent,r='https://plus.google.com/share?url='+e('https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=google_plus'),x=function(){if(!window.open(r,'google_plus','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})();">
                <i class='social-icon-sprite social-icon-google'></i>
                <span>分享到Google+</span>
            </a>
        </li>
        <li>
            <a href="javascript:void(function(){var d=document,e=encodeURIComponent,s1=window.getSelection,s2=d.getSelection,s3=d.selection,s=s1?s1():s2?s2():s3?s3.createRange().text:'',r='http://www.douban.com/recommend/?url='+e('https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=douban')+'&title='+e('CSS选择器有哪些?')+'&sel='+e(s)+'&v=1',x=function(){if(!window.open(r,'douban','toolbar=0,resizable=1,scrollbars=yes,status=1,width=450,height=330'))location.href=r+'&r=1'};if(/Firefox/.test(navigator.userAgent)){setTimeout(x,0)}else{x()}})()">
                <i class='social-icon-sprite social-icon-douban'></i>
                <span>分享到豆瓣</span>
            </a>
        </li>
    </ul>

然后我就点了点分享按钮~


pic3.gif

哎哟~脑壳痛(胡乱猜一下):
第一步:请求了side_tool接口
第二步:然后将返回的参数中的sns_share_url编译成html放入了分享按钮a标签的data-content
第三步:用户点击分享按钮:data-content渲染到了<div class="popover"><div>

然后我又看了看微博分享(如何做微博分享)

 <a href="javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina.com.cn/share/share.php?appkey=1881139527',u=z||d.location,p=['&url=',e(u),'&title=',e(t||d.title),'&source=',e(r),'&sourceUrl=',e(l),'&content=',c||'gb2312','&pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','', '推荐 繁络 的文章《CSS选择器有哪些?》( 分享自 @简书 )','https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weibo','页面编码gb2312|utf-8默认gb2312'));">
                <i class='social-icon-sprite social-icon-weibo'></i>
                <span>分享到微博</span>
            </a>

这样实在是看不明白一串参数是个啥,不要脸的我只好...
(微博分享:文章地址及描述信息采用查询参数传递)

      (function (s, d, e, r, l, p, t, z, c) {
        // 使用微博分享地址
        var f = 'http://v.t.sina.com.cn/share/share.php?appkey=1881139527',
            u = z || d.location,
            // 拼接查询参数,作为分享描述信息
            p = ['&url=', e(u), '&title=', e(t || d.title), '&source=', e(r), '&sourceUrl=', e(l), '&content=', c || 'gb2312', '&pic=', e(p || '')].join('');
        function a() {
            if (
                // 新开窗口
                !window.open(
                    [f, p].join(''),
                    'mb',
                    // 窗口样式
                    ['toolbar=0,status=0,resizable=1,width=440,height=430,left=', (s.width - 440) / 2, ',top=', (s.height - 430) / 2].join('')
                )
                // 跳转路径
            ) u.href = [f, p].join('');
        };
        // 为什么要setTimeout
        if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();
    })(
        screen, // s : 客户端显示屏幕的信息
        document, // d : 文档对象
        encodeURIComponent, // e : 进行编码
        '', '', '', 
        // r: source 
        // l: sourceUrl
        // p: pic
        '推荐 繁络 的文章《CSS选择器有哪些?》( 分享自 @简书 )', // t : 标题
        'https://www.jianshu.com/p/1d6efea3db1c?utm_campaign=maleskine&utm_content=note&utm_medium=reader_share&utm_source=weibo', // z : 转载地址
        '页面编码gb2312|utf-8默认gb2312' // c : 编码
        )

生成的最后的链接我就不给大家看了~

为什么要这么干呢?

侧边栏其实有两种情况(用户角色权限)

1.登录状态
登录状态下有很多功能:加入专题、收藏文章、喜欢文章,


image.png

2.未登录状态(为此我还得退出一下,脑壳痛~)
未登录状态下只有分享功能


image.png
  1. 静态页面数据


    image.png

由上可见

  1. 文章内容是静态渲染的、利于首屏快速渲染、并且利于爬虫的爬取
  2. 根据用户的状态,动态的渲染,赋予用户不同的功能

根据用户的角色区别了分享内容描述:

  1. 作者分享: '我写了新文章'
  2. 读者分享: '推荐 xx 的文章'

可见简书非常的细心呀!
其实还做了图片懒加载

相信你肯定学到了如何做分享功能(我太懒只贴了微博的分享)

最后有个疑问: 望大佬指点一下,感谢~

// 为什么Firefox要setTimeout
if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0); else a();

最后谢谢简书的前端,若有冒犯,请联系~可立刻删文!!!

image.jpg
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,589评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,615评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,933评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,976评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,999评论 6 393
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,775评论 1 307
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,474评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,359评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,854评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,007评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,146评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,826评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,484评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,029评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,153评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,420评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,107评论 2 356

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,432评论 0 17
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,490评论 1 45
  • 腊月,淡妆素裹牵住了我们的手!离年的脚步越来越近,渴望亲近成悠长的思念。让心情去追逐片片清纯素洁的雪花,灿灿然然,...
    开心莲Maryl阅读 266评论 0 2
  • - (void)viewDidLoad { [superviewDidLoad]; //不能再viewDidL...
    一代骄马阅读 516评论 0 0
  • 健身课 代课教练和我聊天 谈到她觉得谈恋爱很难 谈不好 索性也就不想谈了 我应声附和 心里想着 我当初也没有计划什...
    姚风阅读 324评论 2 1