查看: 163|回复: 1

Discuz! X5模板帖子内容页打印的旁边添加阅读字号

[复制链接]
  • 大
  • 中
  • 小
  • TA的每日心情
    奋斗
    7 小时前
  • 签到天数: 17 天

    [LV.4]偶尔看看III

    2463

    主题

    138

    回帖

    7633

    积分

    站长

    🤝 贡献
    9
    🌟 威望
    0
    🪙 金币
    5023
    💎 社区币
    430

    管理员帅哥金牛座羊

    发表于 2026-8-10 01:04:10 | 显示全部楼层 |阅读模式

    Discuz! X5模板帖子内容页打印的旁边添加阅读字号

    Discuz! X5模板帖子内容页打印的旁边添加阅读字号



    打开 template\discuzx5\forum\viewthread.php。查找:

    1. <!--{hook/viewthread_title_extra}-->
    复制代码


    换行添加下边的代码:

    1. <style>
    2.                 #viewui_fontsize{float: right;}
    3.                 #viewui_fontsize span{font-size:12px;color:#999;margin-right:3px;}
    4.                 #viewui_fontsize ul{display: inline-block;vertical-align: -4px;}
    5.                 #viewui_fontsize ul li{float: left;background: #b3b3b3;padding: 0px 3px 1px 3px;color: #FFF;font-size: 12px;border-radius: 2px;margin: 0 3px;cursor: pointer;}
    6.                 #viewui_fontsize ul li.a,#viewui_fontsize ul li:hover{background: #369;}
    7. </style>
    8. <div id="viewui_fontsize">
    9. <span>阅读字号:</span>
    10. <ul>
    11. <li class="" onclick="setFontsize(this,'24');" title="点击切换为此字号,选中后再次点击可取消选择">大</li>
    12. <li class="" onclick="setFontsize(this,'18');" title="点击切换为此字号,选中后再次点击可取消选择">中</li>
    13. <li class="a" onclick="setFontsize(this,'14');" title="点击切换为此字号,选中后再次点击可取消选择">小</li>
    14. </ul>
    15. </div>
    16. <script type="text/javascript">
    17.     function setFontsize(checkobj, size){
    18.         if(checkobj.className == 'a'){
    19.             document.cookie = "viewui_fontsize=; expires=" + new Date(0).toUTCString() + ";path=/";
    20.             location.reload();
    21.             return;
    22.         }
    23.         var box = document.getElementById('viewui_fontsize').getElementsByTagName('li');
    24.         for(var i=0; i<box.length; i++){
    25.             box[i].className = '';
    26.         }
    27.         checkobj.className = 'a';

    28.         // 核心:动态生成一个全局样式标签,用 !important 强制覆盖所有子元素(包括代码块)的字号
    29.         var styleId = 'dynamic_fontsize_style';
    30.         var oldStyle = document.getElementById(styleId);
    31.         if (oldStyle) {
    32.             oldStyle.parentNode.removeChild(oldStyle);
    33.         }

    34.         var css = `
    35.             .message, .t_f, .t_f *, .message *, .blockcode, .blockcode *, .blockcode li, .blockcode code, .blockcode td, .pcb {
    36.                 font-size: ` + size + `px !important;
    37.             }
    38.         `;
    39.         
    40.         var head = document.head || document.getElementsByTagName('head')[0];
    41.         var style = document.createElement('style');
    42.         style.id = styleId;
    43.         style.type = 'text/css';
    44.         if (style.styleSheet){
    45.             style.styleSheet.cssText = css;
    46.         } else {
    47.             style.appendChild(document.createTextNode(css));
    48.         }
    49.         head.appendChild(style);

    50.         // 记录 Cookie
    51.         document.cookie = 'viewui_fontsize=' + size + '; expires=Fri, 31 Dec 9999 23:59:59 GMT;path=/';
    52.     }

    53.     // 页面加载时自动读取 Cookie 恢复字号
    54.     window.onload = function(){
    55.         var match = document.cookie.match(new RegExp('(^| )viewui_fontsize=([^;]+)'));
    56.         if (match) {
    57.             var size = match[2];
    58.             var lis = document.getElementById('viewui_fontsize').getElementsByTagName('li');
    59.             var targetSizeLi = (size == '24') ? lis[0] : (size == '18' ? lis[1] : lis[2]);
    60.             if(targetSizeLi){
    61.                 setFontsize(targetSizeLi, size);
    62.             }
    63.         }
    64.     }
    65. </script>
    复制代码


    如下图所示:


    Discuz! X5模板帖子内容页打印的旁边添加阅读字号

    Discuz! X5模板帖子内容页打印的旁边添加阅读字号






  • TA的每日心情
    奋斗
    7 小时前
  • 签到天数: 17 天

    [LV.4]偶尔看看III

    2463

    主题

    138

    回帖

    7633

    积分

    站长

    🤝 贡献
    9
    🌟 威望
    0
    🪙 金币
    5023
    💎 社区币
    430

    管理员帅哥金牛座羊

     楼主| 发表于 2026-8-10 09:26:08 | 显示全部楼层
    Discuz! 自带手机版


    3.webp

    打开 template\default\touch\forum\viewthread.php,查找:

    1.         <div class="view_tit">
    2.                 <!--{if $_G['forum_thread']['typeid'] && $_G['forum']['threadtypes']['types'][$_G['forum_thread']['typeid']]}-->
    3.                         <em>[{$_G['forum']['threadtypes']['types'][$_G['forum_thread']['typeid']]}]</em>
    4.                 <!--{/if}-->
    5.                 <!--{if $threadsorts && $_G['forum_thread']['sortid']}-->
    6.                         <em>[{$_G['forum']['threadsorts']['types'][$_G['forum_thread']['sortid']]}]</em>
    7.                 <!--{/if}-->
    8.                 $_G['forum_thread']['subject']
    9.                 <!--{if $_G['forum_thread']['displayorder'] == -2}--> <span>({lang moderating})</span>
    10.                 <!--{elseif $_G['forum_thread']['displayorder'] == -3}--> <span>({lang have_ignored})</span>
    11.                 <!--{elseif $_G['forum_thread']['displayorder'] == -4}--> <span>({lang draft})</span>
    12.                 <!--{/if}-->
    复制代码


    在下面添加:

    1. <style>
    2.     #viewui_fontsize{float: right;}
    3.     #viewui_fontsize span{font-size:12px;color:#999;margin-right:3px;}
    4.     #viewui_fontsize ul{display: inline-block;vertical-align: -4px;}
    5.     #viewui_fontsize ul li{float: left;background: #b3b3b3;padding: 0px 3px 1px 3px;color: #FFF;font-size: 12px;border-radius: 2px;margin: 0 3px;}
    6.     #viewui_fontsize ul li.a,#viewui_fontsize ul li:hover{background: #369;}
    7. </style>
    8. <div id="viewui_fontsize">
    9. <span>阅读字号:</span>
    10. <ul>
    11. <li class="" onclick="setFontsize(this,'20');" title="点击切换为此字号">大</li>
    12. <li class="" onclick="setFontsize(this,'18');" title="点击切换为此字号">中</li>
    13. <li class="a" onclick="setFontsize(this,'16');" title="点击切换为此字号">小</li>
    14. </ul>
    15. </div>
    16. <script type="text/javascript">
    17.     function setFontsize(checkobj, size){
    18.         if(checkobj.className == 'a'){
    19.             document.cookie = "viewui_fontsize=; expires=" + new Date(0).toUTCString() + ";path=/";
    20.             location.reload();
    21.             return;
    22.         }
    23.         var box = document.getElementById('viewui_fontsize').getElementsByTagName('li');
    24.         for(var i=0; i<box.length; i++){
    25.             box[i].className = '';
    26.         }
    27.         checkobj.className = 'a';

    28.         // 获取手机版正文容器
    29.         var obj = document.getElementsByClassName('message');
    30.         if(obj.length == 0){
    31.             obj = document.getElementsByClassName('t_f');
    32.         }

    33.         if(obj.length > 0){
    34.             for(var i=0; i<obj.length; i++){
    35.                 // 1. 设置正文主容器字号
    36.                 obj[i].style.fontSize = size + 'px';
    37.                
    38.                 // 2. 遍历并修改正文里所有的 font 标签
    39.                 var fonts = obj[i].getElementsByTagName('font');
    40.                 for(var o=0; o<fonts.length; o++){
    41.                     fonts[o].style.fontSize = size + 'px';
    42.                 }

    43.                 // 3. 【新增】强制修改代码块内部的字号(适配 Discuz 的 blockcode / pre / code)
    44.                 var codeBlocks = obj[i].querySelectorAll('.blockcode, .blockcode code, .blockcode pre, .blockcode li, pre, code');
    45.                 for(var c=0; c<codeBlocks.length; c++){
    46.                     codeBlocks[c].style.fontSize = size + 'px';
    47.                 }
    48.             }
    49.         }
    50.         document.cookie = 'viewui_fontsize=' + size + '; expires=Fri, 31 Dec 9999 23:59:59 GMT;path=/';
    51.     }

    52.     // 页面加载时自动读取 Cookie 恢复字号
    53.     window.onload = function(){
    54.         var match = document.cookie.match(new RegExp('(^| )viewui_fontsize=([^;]+)'));
    55.         if (match) {
    56.             var size = match[2];
    57.             var lis = document.getElementById('viewui_fontsize').getElementsByTagName('li');
    58.             var targetSizeLi = (size == '20') ? lis[0] : (size == '18' ? lis[1] : lis[2]);
    59.             setFontsize(targetSizeLi, size);
    60.         }
    61.     }
    62. </script>
    复制代码


    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    手机版|小黑屋|网站地图|文强阁

    相关侵权、举报、投诉及建议等,请发 E-mail:hsbk@hotmail.com

    Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

    在本版发帖