简体   繁体   English

如何在 Tampermonkey 脚本上添加下一个/上一个按钮分页?

[英]How do I add next/previous button pagination on tampermonkey script?

I have the following Tampermonkey script我有以下 Tampermonkey 脚本

// ==UserScript==
// @name         easyEye
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Read your web novel easily on mobile with paginated content
// @author       Zimorok
// @match        *://boxnovel.com/*
// @require      http://code.jquery.com/jquery-latest.js
// @grant        none
// ==/UserScript==
var $ = window.jQuery
$(document).ready(function() {
    'use strict';

    var chapterText = $("div.reading-content p").each(function(){
                        $(this).clone().wrap('<p>').parent().html();
                        });

    //--div.read-container
    //--replace the whole div text with only the chapterText
    $('div.text-left').wrap('<div id="story">').html(chapterText);

//--https://stackoverflow.com/questions/12202324/split-text-into-pages-and-present-separately-html5
var contentBox = $('div.text-left');
var words = contentBox.html().split(' ');
function paginate() {
    var newPage = $('<div class="page">').css({'border':'1px solid black','text-align':'justify','padding':'5px'});
    contentBox.empty().append(newPage);
    var pageText = null;
    for(var i = 0; i < words.length; i++) {
        var betterPageText;
        if(pageText) {
            betterPageText = pageText + ' ' + words[i];
        } else {
            betterPageText = words[i];
        }
        newPage.html(betterPageText);
        if(newPage.height() > $(window).height()) {
            newPage.html(pageText);
            newPage.clone().insertBefore(newPage)
            pageText = null;
        } else {
            pageText = betterPageText;             
        }
    }
}

$(window).resize(paginate).resize();
$('html, body').animate({ scrollTop: $('#story').offset().top }, 'slow'); //--focus to the story
//--debug
console.log('')
});

This renders the site for an easy reading of the novel on mobile.这使得该网站可以在移动设备上轻松阅读小说。 How to add the next/previous button so I don't have to scroll to the next page?如何添加下一个/上一个按钮,这样我就不必滚动到下一页?

Or is it possible to add touch-to-next page?或者是否可以添加触摸到下一页? Thanks!谢谢!

i have manage to do the button but now it is blocking the div below.我已经设法做按钮,但现在它阻止了下面的 div。

// ==UserScript==
// @name         easyEye
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Read your web novel easily on mobile with paginated content
// @author       Zimorok
// @match        *://boxnovel.com/*
// @require      http://code.jquery.com/jquery-latest.js
// @grant        GM_addStyle
// ==/UserScript==
GM_addStyle("#navbar{overflow:hidden;background-color:#333}#navbar a{float:left;display:block;color:#f2f2f2;text-align:center;padding:14px;text-decoration:none}.content{padding:16px}.sticky{position:fixed;top:0;width:100%}.sticky+.content{padding-top:60px}");
var $ = window.jQuery
$(document).ready(function() {
    'use strict';

    var chapterText = $("div.reading-content p").each(function(){
                        $(this).clone().wrap('<p>').parent().html();
                        });

    //--div.read-container
    //--replace the whole div text with only the chapterText
    $('div.text-left').wrap('<div id="story">').html(chapterText);

//--https://stackoverflow.com/questions/12202324/split-text-into-pages-and-present-separately-html5
var contentBox = $('div.text-left');
var words = contentBox.html().split(' ');
function paginate() {
    var newPage = $('<div class="page">').css({'border':'1px solid black','text-align':'justify','padding':'5px'});
    contentBox.empty().append(newPage);
    var pageText = null;
    for(var i = 0; i < words.length; i++) {
        var betterPageText;
        if(pageText) {
            betterPageText = pageText + ' ' + words[i];
        } else {
            betterPageText = words[i];
        }
        newPage.html(betterPageText);
        if(newPage.height() > $(window).height()) {
            newPage.html(pageText);
            newPage.clone().insertBefore(newPage)
            pageText = null;
        } else {
            pageText = betterPageText;
        }
    }
    //--create the page ID and add the div.page
    $('div.page').each(function(index){
        $(this).prop('id','page_'+index);
    });

    //--creating next/previous button
    //--append the next/previous button
    var nav = $('<div id="navbar">');
    contentBox.prepend(nav);
    for(var pn = 0; pn < $('div.page').length; pn++)
    {
        nav.append('<a href="#page_'+pn+'">'+pn+'</a>');
    }
    //--simple numbering and scroll-to 
    $("a[href^='#']").click(function(e) {
    e.preventDefault();

    var position = $($(this).attr("href")).offset().top;

    $("body, html").animate({
        scrollTop: position
    } /* speed */ );
    });

}

$(window).resize(paginate).resize();
$('html, body').animate({ scrollTop: $('#story').offset().top }, 'slow'); //--focus to the story

//fixed page button
var num = 200; //number of pixels before modifying styles

$(window).bind('scroll', function () {
    if ($(window).scrollTop() > num) {
        $('#navbar').addClass('sticky');
    } else {
        $('#navbar').removeClass('sticky');
    }
});

//--debug
console.log('Pages: '+$('div.page').length);
});

my #navbar will block a part of the top page .我的#navbar将阻止page的一部分。 how to avoid it so that when i click on the #navbar , it will show the top div?如何避免它,以便当我点击#navbar时,它会显示顶部 div?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在分页中添加上一个和下一个 - How to add Previous and Next in Pagination 如何为此 slider 添加下一个和上一个按钮而不是 data-link=“n”(删除分页并仅添加下一个和上一个按钮) - How can i add Next and Previous button for this slider instead of data-link=“n” (remove pagination and add only Next and Prev. button) 我如何使 Tampermonkey 脚本在网站上“单击”按钮? 我试过以前在这里问过的问题 - How do i make tampermonkey script will 'click' button on website? I tried question asked here previously 我想在分页中使用下一个和上一个按钮 - I want to use next and previous button in my pagination 如何使用按钮调用数组中的下一个和上一个值? - How do I call the next and the previous value in an array with a button? 如何为 Javascript 中的幻灯片同步上一个和下一个按钮? - How Do I Make A Previous and next Button synchronized For A Slideshow In Javascript? 表格分页:如何创建一个包含 10 个元素的表格并实现第一个、上一个、下一个和最后一个按钮? - Table Pagination : How can I create a table with 10 elements and implement first, previous, next and last button? 如何在tampermonkey中制作脚本以自动单击 - How do i make a script in tampermonkey to auto click 如何使用jQuery将下一个和上一个按钮添加到我的幻灯片? - How do I add next and previous buttons to my slideshow with jQuery? 如何在垂直传送带上添加上一个和下一个 function? - How do I add a previous and next function to on a vertical carousel?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM