IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    替换分享工具自带图标

    知更鸟发表于 2017-05-11 13:03:33
    love 0

    目前常用的社会化分享工具有 百度分享  和 JiaThis,特别是这个JiaThis年代够久远,在百度也加入这个领域后,居然还坚持为大家提供免费工具,这里点32个赞!

    随着时代的变化,这些分享工具自带的小图标却一成不变,已不符合目前流行的扁平化风格,与网站风格格不入,极不协调,但却有些网站包括一些设计良好的WordPress 主题,虽然用的也上面两款分享工具,但图标却不一样,扁平、大方、简洁,显得高大上。今天就分享一下修改后的代码,起个抛砖引玉的作用,以JiaThis分享工具为例,下面是修改后的效果:

    替换分享工具自带图标

    默认的分享按钮

    美化后的分享按钮

    基本原理就是找到默认分享代码的DIV选择器,然后隐藏背景图片,添加字体图标并重写样式。

    一、首先将下面代码添加到当前主题functions.php的最后:

    1. function entry_share($content) {
    2. if (is_single()) {
    3.     $content .= '
    4.     <div class="entry-share">
    5.         <div class="share-box">
    6.             <span class="pull-left">分享到</span>
    7.             <!-- JiaThis Button BEGIN -->
    8.             <div class="jiathis_style_32x32">
    9.                 <a class="jiathis_button_tsina"><i class="fa fa-weibo"></i></a>
    10.                 <a class="jiathis_button_weixin"><i class="fa fa-wechat"></i></a>
    11.                 <a class="jiathis_button_renren"><i class="fa fa-renren"></i></a>
    12.                 <a class="jiathis_button_qzone"><i class="fa fa-qq"></i></a>
    13.                 <a class="jiathis_button_fb"><i class="fa fa-facebook"></i></a>
    14.                 <a class="jiathis_button_twitter"><i class="fa fa-twitter"></i></a>
    15.                 <a href="http://www.jiathis.com/share" class="jiathis jiathis_txt jtico jtico_jiathis" target="_blank"><span><i class="fa fa-share-alt"></i></span></a>
    16.                 <a class="jiathis_counter_style"></a>
    17.             </div>
    18.         </div>
    19.         <script type="text/javascript" src="http://v3.jiathis.com/code/jia.js" charset="utf-8"></script>
    20.         <!-- JiaThis Button END -->
    21.     </div>';
    22. }
    23. return $content;
    24. }
    25. add_filter('the_content','entry_share');

    之后分享代码会自动添加到文章正文的下面。

    上面代码只是在原JiaThis分享代码基础上添加了字体图标。

    二、再把下面配套的样式添加到当前主题样式文件style.css中即可。

    展示代码展开

    1. /** 外部盒子 **/
    2. .entry-share {
    3.     font-size: 14px;
    4.     text-align: center;
    5.     margin: 0 auto 20px;
    6. }
    7. /********* 分享文字 ********/
    8. .entry-share .pull-left {
    9.     font-weight: 700;
    10.     line-height: 40px;
    11. }
    12. /******** 分享样式 ********/
    13. .entry-share div.share-box {
    14.     display: inline-block;
    15.     overflow: hidden;
    16. }
    17. .entry-share .jiathis_style_32x32 {
    18.     float: left !important;
    19.     margin-left: 10px;
    20. }
    21. .entry-share .jiathis_style_32x32 a {
    22.     float: left;
    23.     width: 40px;
    24.     height: 40px;
    25.     color: #a8a8a8;
    26.     font-size: 16px !important;
    27.     display: block;
    28.     border-radius: 5px;
    29.     margin-right: 10px;
    30.     border: 1px solid #999;
    31. }
    32. .entry-share .jiathis_style_32x32 a:hover {
    33.     text-decoration: none;
    34.     color: #fff;
    35. }
    36. /** 图标大小 **/
    37. .entry-share .jiathis_style_32x32 a span {
    38.     background: transparent !important;
    39.     width: 38px !important;
    40.     height: 38px !important;
    41.     padding: 0 !important;
    42.     margin: 0 !important;
    43.     float: none !important;
    44.     font-size: 20px !important;
    45.     display: block !important;
    46.     text-align: center !important;
    47.     line-height: 38px !important;
    48. }
    49. /******** 更多分享 ********/
    50. .entry-share .jiathis_style_32x32 a.jtico_jiathis {
    51.     background: transparent !important;
    52.     width: 38px !important;
    53.     height: 38px !important;
    54.     padding: 0 !important;
    55.     margin: 0 !important;
    56.     font-size: 24px !important;
    57.     display: block !important;
    58.     text-align: center !important;
    59.     line-height: 38px !important;
    60. }
    61. .entry-share .jiathis_style_32x32 a:hover.jtico_jiathis {
    62.     background: #666 !important;
    63.     border-color: #666;
    64. }
    65. /******** 分享次数 ********/
    66. .entry-share .jiathis_style_32x32 a.jiathis_counter_style {
    67.     width: auto;
    68.     margin-left: 10px;
    69.     padding: 0 15px;
    70.     border-radius: 5px;
    71.     display: inline-block;
    72.     position: relative;
    73.     background: #e8e8e8;
    74.     border-color: #e8e8e8;
    75.     color: #fff !important;
    76. }
    77. /** 箭头 **/
    78. .entry-share .jiathis_style_32x32 a.jiathis_counter_style:before {
    79.     content: '';
    80.     width: 0;
    81.     height: 0;
    82.     border-style: solid;
    83.     border-width: 10px 10px 10px 0;
    84.     border-color: transparent #e8e8e8 transparent transparent;
    85.     position: absolute;
    86.     right: 100%;
    87.     top: 50%;
    88.     margin-top: -10px;
    89. }
    90. .entry-share .jiathis_style_32x32 a.jiathis_counter_style span.jiathis_bubble_style {
    91.     color: #666 !important;
    92.     font-size: 14px !important;
    93.     width: auto !important;
    94. }
    95. /******** 不同图标悬停背景颜色 ********/
    96. .entry-share .jiathis_style_32x32 a:hover.jiathis_button_tsina {
    97.     background: #e74c3c;
    98.     border-color: #e74c3c;
    99. }
    100. .entry-share .jiathis_style_32x32 a:hover.jiathis_button_weixin {
    101.     background: #2ecc71;
    102.     border-color: #2ecc71;
    103. }
    104. .entry-share .jiathis_style_32x32 a:hover.jiathis_button_fb {
    105.     background: #4760A5;
    106.     border-color: #4760A5;
    107. }
    108. .entry-share .jiathis_style_32x32 a:hover.jiathis_button_twitter {
    109.     background: #50ABF1;
    110.     border-color: #50ABF1;
    111. }
    112. .entry-share .jiathis_style_32x32 a:hover.jiathis_button_renren {
    113.     background: #3777BE;
    114.     border-color: #3777BE;
    115. }
    116. .entry-share .jiathis_style_32x32 a:hover.jiathis_button_qzone {
    117.     background: #2174C3;
    118.     border-color: #2174C3;
    119. }

    鼠标悬停在分享按钮上,背景会变成不同的颜色。

    注:

    因代码中使用了Font Awesome字体图标,如果你的主题没有加载字体图标,可以到WP后台--插件--安装插件页面搜索:Font Awesome 4 Menus 安装并启用,才能演示替换后的图标。

    如果你动手能力比较强,也可以同样替换百度分享图标。



沪ICP备19023445号-2号
友情链接