|
本帖最后由 hbghlyj 于 2022-2-11 21:06 编辑 在/static/js/forum_viewthread.js中加入- var currentpost = 0;
- function updatecurrentpost() {
- currentpost = [0, 1, 2].find(x=>parseInt(document.querySelectorAll('table.plhin')[x].getBoundingClientRect().bottom) > 0)
- }
- onkeydown = function(e) {
- if (e.key == 'ArrowUp' && e.ctrlKey) {
- if (parseInt(document.querySelectorAll('table.plhin')[currentpost].getBoundingClientRect().top) < 0) {
- scrollto(currentpost)
- } else {
- scrollto(currentpost - 1)
- }
- }
- if (e.key == 'ArrowDown' && e.ctrlKey) {
- scrollto(currentpost + 1)
- }
- }
- function scrollto(num) {
- if (num == -1) {
- scrollTo(0, 0)
- } else {
- if (document.querySelectorAll('table.plhin')[num]) {
- scrollBy(0, document.querySelectorAll('table.plhin')[num].getBoundingClientRect().top)
- } else {
- scrollTo(0, document.body.scrollHeight)
- }
- }
- }
- _attachEvent(window, 'scroll', updatecurrentpost);
复制代码 注1:因为我的论坛上是每3个帖子就换一页,所以currentpost只能取0,1,2这些值.
注2:_attachEvent 是Discuz自带的函数
$\Huge\unicode{x1F600}$
左右键翻页是Discuz自带的功能(包括这个论坛!),由模板:template/default/forum/viewthread.htm中的下面的部分实现:- <script type="text/javascript">document.onkeyup = function(e){keyPageScroll(e, <!--{if $page > 1}-->1<!--{else}-->0<!--{/if}-->, <!--{if $page < $_G['setting']['threadmaxpages'] && $page < $_G['page_next']}-->1<!--{else}-->0<!--{/if}-->, 'forum.php?mod=viewthread&tid=$_G[tid]<!--{if $_GET[authorid]}-->&authorid=$_GET[authorid]<!--{/if}-->', $page);}</script>
复制代码 其中的keyPageScroll的定义在forum.js中:- function keyPageScroll(e, prev, next, url, page) {
- if (loadUserdata('is_blindman')) {
- return true;
- }
- e = e ? e : window.event;
- var tagname = BROWSER.ie ? e.srcElement.tagName : e.target.tagName;
- if (tagname == 'INPUT' || tagname == 'TEXTAREA')
- return;
- actualCode = e.keyCode ? e.keyCode : e.charCode;
- if (next && actualCode == 39) {
- window.location = url + '&page=' + (page + 1);
- }
- if (prev && actualCode == 37) {
- window.location = url + '&page=' + (page - 1);
- }
- }
复制代码 例如下面是从某个帖子的第3页的源代码中抽取出来的片段:
document.onkeyup = function(e){keyPageScroll(e, 1, 1, 'forum.php?mod=viewthread&tid=262', 3);}
这里的e是事件,第一个1代表之前有页(如果是第一页那么之前没有页则为0),第二个1代表之后有页(如果是末页那么之后没有页则为0),最后一个参数3代表当前页面为第3页. |
|