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

问题描述 投票:0回答:1

我有以下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('')
});

这将使该站点易于在移动设备上阅读小说。如何添加下一个/上一个按钮,这样我就不必滚动到下一页?

或者是否可以添加触摸到下一页?谢谢!

javascript jquery button tampermonkey
1个回答
0
投票

我已经设法完成该按钮,但现在它阻止了下面的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);
});

我的#navbar将阻止顶部page的一部分。如何避免这种情况,以便当我单击#navbar时将显示顶部的div?

© www.soinside.com 2019 - 2024. All rights reserved.