如何在主页上加载tumblr nextPage的内容

14

我正在为个人作品集开发Tumblr主题,但在我的工作区域遇到了一个严重的问题。 我想创建一个水平滑块效果,当我单击“下一页”以查看旧文章时,就像这样:

http://tympanus.net/Tutorials/WebsiteScrolling/

我已经有以下代码来定义我的下一页,#section4是我工作帖子的位置:

<!-- Next Page -->
{block:NextPage}
<div>
    <a id="next-button" href="{NextPage}#section4" class="next panel" style="position:relative; top:630px; left:1550px;">next</a>
</div>
{/block:NextPage}
<!-- / Next Page -->

好的,这是定义打开另一个页面的内容:

"indexPage".tumblr.com#section4

当我点击下一步时,它会跳转到:

"indexPage".tumblr.com/page/2#section4

有没有办法在我的首页上实现这种幻灯片效果,或者至少在转移到其他页面时给人以滑动效果的错觉?


3
你是否感觉自己负担过重? - nicael
4
不,我真的需要一个答案...... :( - gn66
我相信你需要一个单页网站来实现这个效果。你也可以查看这个链接,了解如何实现不同的页面过渡效果:http://tympanus.net/codrops/2013/05/07/a-collection-of-page-transitions/ - TheBokiya
请澄清一下,你的工作页面是一个tumblr页面吗?(例如 site.tumblr.com/work)还是你的tumblr帖子就是你的作品(例如:site.tumblr.com/page/2 包含你的旧作品,但你的主题有一个首页,而 #section4 是你发布帖子的地方),或者它是你主题的一部分(例如:主题的 #section4)? - mfirdaus
你好!我的工作在site.tumblr.com/#section4上,我已经有一个垂直滑块来切换部分,但现在我需要一个水平滑块来在第4节内切换,并加载page/2以便我可以进行滑块操作。 - gn66
2个回答

8
如果我正确理解了你的问题,它就像serakfalcon提到的那样,但我会添加一些tumblr特定的要点。你需要具体做三件事情。我不会深入讲解tumblr模板代码和样式,而是专注于以下这些方面:
  1. 动态加载数据
  2. 动态创建新的部分/页面
  3. 使导航栏与新部分/页面协同工作

加载数据

这实际上很简单。对于tumblr,正如你所提到的,我们可以通过点击“下一页”链接手动进入下一页。在tumblr中,它将看起来像:

{block:NextPage}
  <li></li><a href="{NextPage}" class="next-page static">{lang:Next page}</a></li>   
{/block:NextPage}

由于这些页面位于相同的域上,我们可以通过 AJAX 覆盖链接以获取页面。我们将获得该页面的原始 HTML,其中将包括其他部分(例如另外三个部分)。可能有一些 Tumblr 模板魔法可以限制这一点,但我认为这超出了范围。那么我们如何获取内容呢?我们可以将原始 HTML 提供给 jQuery 以构建文档对象,然后从中选择带有文章的内容。因此,可以使用类似以下的代码:
$(document.body).on('click',".static",function(e){ //delegated
    var xhr=$.get($(this).attr("href"),function(a){ //a is next page's HTML
        $loaded=$(a); //loaded now has a temporary object with next page's objects
        var postsHTML=$loaded.find("#posts").html() //contains posts we can embed
        //what to find depends on your template. I used the default id="posts"
    })
})

创建新的部分/页面

一旦我们有了数据,现在我们需要把数据放到一个新页面中。有很多方法可以做到这一点,但这是我是如何做的。首先,我为新部分准备了一个模板。我将它放在一个脚本标签中,就像这样。我喜欢这样做的语义。

<script type="text/x-template" id="section-template">
    <div class="section">
        <div class="posts"></div>
        <ul class="nav">
            <li><a href="#section1">1</a></li>
            <li><a href="#section2">2</a></li>
            <li><a href="#section2">3</a></li>
            <li><a href="#section4">4</a></li>
            <li><a href="#" class="last-page">Back</a></li>
            <li><a href="#" class="next-page static">Next</a></li>
        </ul>
    </div>
</script>

完成这一步后,每当数据加载时,我们可以从中获取原始HTML,并通过再次将其提供给jQuery来创建新的元素。然后将帖子附加到具有class为“posts”的div中。 我们还从数据中获取下一页链接,将其附加到下一页链接上,以便之前的Ajax调用将起作用。 如果找不到下一页链接,则表示没有更多帖子。因此,我们可以隐藏下一页链接。 我们还将停止现有链接通过ajax加载数据并执行正常的滑动操作。最后,我们将新节的部分附加到节的父级div中,修复其宽度,然后转换到新节。
$(document.body).on('click',".static",function(e){ //deferred
    e.preventDefault();
    var that=this //to refer inside $.get callback
    var xhr=$.get($(this).attr("href"),function(a){
        $(that).removeClass('static') //stop this link from loading again
        var $loaded=$(a)
        var next_section=$($("#section-template").html()); //make the new section
        var num_sections=$(".section").length + 1 //number of sections
        next_section.addClass(num_sections%2 ? "white" : "black") //consistency

        //find .posts in the new section and put the tumblr data in it
        next_section.find(".posts").html($loaded.find("#posts").html())
        //tumblr next page link. change according to template
        var next_link=$loaded.find(".static")
        if(next_link.length){ //if next link exists
            //attach href to next page link in new section
            next_section.find(".static").attr("href",next_link.attr("href"))
        } else { //no next
            //hide the next page link in new section
            next_section.find(".static").hide()
        }
        $("#horizontal-scroller").append(next_section); //append to body
        $("#horizontal-scroller").width(4000*num_sections); //resize body

        $('html, body').stop().animate({ //to the new section
            scrollLeft: $(next_section).offset().left
        }, 1000);

    }) //$.get

}) //click

让导航栏正常工作

我可能应该将新链接添加到导航栏中,但考虑到这会使其变得更加复杂(想象一下有40个页面时的情况),我决定只为加载的内容使用“下一页”和“上一页”。代码很简单。对于下一页,如果它不是.static,则移动到下一个部分,同样的道理适用于上一页。我们将委托(实际上,我使用的是.on(),但是关于.delegate的文档似乎更容易理解),以便它适用于所有新链接。

因此,整个JavaScript/jQuery代码看起来像:

$(document.body)
.on('click','ul.nav a:not(.next-page):not(.last-page)',function(e){
    var $anchor = $(this);
    $('html, body').stop().animate({
        scrollLeft: $($anchor.attr('href')).offset().left
    }, 1000);
    e.preventDefault();
})
.on('click',".next-page:not(.static)",function(e){ //next page not static
    e.preventDefault()
    $('html, body').stop().animate({
        scrollLeft: $(this).closest(".section").nextAll(".section").offset().left
    }, 1000);
})
.on('click',".last-page",function(e){ //last page
    e.preventDefault()
    $('html, body').stop().animate({
        scrollLeft: $(this).closest(".section").prevAll(".section").offset().left
    }, 1000);
})
.on('click',".static",function(e){
    e.preventDefault();
    var that=this
    var xhr=$.get($(this).attr("href"),function(a){
        $(that).removeClass('static')
        var $loaded=$(a)
        var next_section=$($("#section-template").html());
        var num_sections=$(".section").length + 1
        next_section.addClass(num_sections%2 ? "white" : "black")
        next_section.find(".posts").html($loaded.find("#posts").html())
        var next_link=$loaded.find(".static")
        if(next_link.length){
            next_section.find(".static").attr("href",next_link.attr("href"))
        } else {
            next_section.find(".static").hide()
        }
        $("#horizontal-scroller").append(next_section); //append to body
        $("#horizontal-scroller").width(4000*num_sections); //resize body

        $('html, body').stop().animate({
            scrollLeft: $(next_section).offset().left
        }, 1000);
    }) //$.get
}) //click

以下是它正在运行的实时演示。我并没有花太多功夫让幻灯片看起来好看,但希望这对您有所帮助。

你好,我已经将所有内容集成在一起,非常完美。但是我遇到了一个小问题,因为我已经有了一个垂直滚动条,所以我添加了另一个部分:.section2{ margin:0px; bottom:0px; width:4000px; float:right; height:1000px; text-shadow:1px 1px 2px #f0f0f0; 和`script type="text/x-template" id="section-template"> <div class="section2">
<div class="posts"></div>
</div>
</script>` 唯一的问题是帖子出现在我的页面底部,而不是跟随#section4 div,有什么解决办法吗? :X
- gn66
当然,但我还有很多问题:P 再次感谢您的回复。1个问题:所有东西只能在Chrome上运行; 2个问题:所有东西都是为我的屏幕分辨率制作的,所以我必须适应它,如果您看到的是小东西,请增加滚动条,直到我的导航中的“+”与金字塔的顶部相匹配,您会明白的 :) 3个问题:您可能会发现一些错误,请忽略它们xD - gn66
1
你有一个大屏幕。我认为我看到了问题所在。原始网页只是将内容附加到body中。而且部分是浮动的,因此由于存在垂直滚动条,它会渲染在底部附近。我认为我们应该有一个包含水平滚动条的div,并将内容附加到其中,而不仅仅是body。不确定它是否会破坏任何东西,但我会编辑我的模板并查看答案,看看是否有帮助。 - mfirdaus
你真的很棒,伙计!非常感谢你的帮助! - gn66
我刚刚成功了。谢谢你的帮助,你不需要在这里浪费更多时间 :) - gn66
显示剩余10条评论

5
当然,所有内容必须在一个页面上进行,否则您就无法进行过渡。因此,要么您在运行时加载所有内容,要么您可以使用AJAX“作弊”。
将页面中的JavaScript更改为以下内容:
function bindAnimation(event) {
            var $anchor = $(this);
            /*
            if you want to use one of the easing effects:
            $('html, body').stop().animate({
                scrollLeft: $($anchor.attr('href')).offset().left
            }, 1500,'easeInOutExpo');
             */
            $('html, body').stop().animate({
                scrollLeft: $($anchor.attr('href')).offset().left
            }, 1000);
            event.preventDefault();
        }

        $(function() {
            $('ul.nav').on('click','.getNew',getNewSection);    
            $('ul.nav').on('click','a',bindAnimation);
        });

这样可以让您动态地向导航菜单添加元素,并将事件侦听器附加到我们将用于获取新内容的新元素。 修改导航菜单,使最后一个元素类似于:
<li class="getNew">New</li>

现在点击它将加载新内容。 在您的网站中添加以下JavaScript代码,位于以前的JavaScript代码之上:

window.newSectionBlack = false;
        window.SectionID = 4;
        function displaySection(data,textStatus, jqXHR) {
            $('#section3').after(data.html); //add the new section
            $('#'+data.id).addClass(newSectionBlack ? 'black' : 'white');
            newSectionBlack = !newSectionBlack; //style it consistently
            $('.getNew').before('<li><a href="#'+data.id+'">'+data.name+'</li>'); //attach a link to the new section to the menu
            $('body').css({width:4000*SectionID});
            $('#'+data.id+' ul.nav').on('click','a',bindAnimation); //bind scripts
            $('#'+data.id+' ul.nav').on('click','.getNew',getNewSection);
            $('#section1 > ul li:nth-last-child(2) > a').trigger('click'); //go to new
            SectionID++;
        }

function getNewSection() {
    $.ajax({
        type: "POST",
        url: 'LoadSection.php',
        data: {section:SectionID},
        success: displaySection,
        dataType: 'json'
   });
}

这段 JavaScript 会向一个名为 LoadSection.php 的新文件 POST 一个对应要加载的章节编号,该文件需要回应新章节的 HTML 内容,以及该章节应该被称为的 ID 和名称。 LoadSection.php 的代码可以如下所示:
<?php
header('Content-Type: application/json');
$send = array();
switch($_POST['section']) {
default:
    $send['html'] = '<div class="section" id="section'.$_POST['section'] .'"><h2>Section ' . $_POST['section'] . '</h2>
        <p>
            This is a new section loaded via AJAX, cool huh?
        </p>
        <ul class="nav">
            <li><a href="#section1">1</a></li>
            <li><a href="#section2">2</a></li>
            <li><a href="#section3">3</a></li>
            <li class="getNew">New</li>
        </ul>
    </div>';
    $send['id'] = 'section'.$_POST['section'];
    $send['name'] = $_POST['section'];
}
echo json_encode($send);

现在,您的页面可以动态加载内容!请注意,导航栏可能应该重新结构化为绝对元素,只有一个而不是在每个页面上重复出现,还有其他一些可以清理的东西,但如果您正在使用codrops示例,则此类情况应该是预期的。
编辑: Tumblr不允许在其服务器上运行服务器端脚本(有道理),这是上述AJAX方法所需的。但是,您有许多选择。 1)将tumblr页面重定向到您控制的站点,并使用上述方法。 2)使用iframe。
Iframe方法可以直接完成,这需要您预先指定所有页面的URL(或者至少它们需要遵循可预测的模式),或者间接使用类似于上面的AJAX脚本。我将演示如何加载直接的iframe,我相信您可以弄清楚其余部分。
window.newSectionBlack = false;
window.newSectionID = 4;

function getNewSection() {
    $('#section3').after('<div class="section" id="section'+newSectionID+'"><h2>New Section</h2>
        <iframe src="yourtumbrsite.com></iframe>
        <ul class="nav">
            <li><a href="#section1">1</a></li>
            <li><a href="#section2">2</a></li>
            <li><a href="#section3">3</a></li>
            <li class="getNew">New</li>
        </ul>
    </div>
'); //add the new section
            $('#section'+newSectionID).addClass(newSectionBlack ? 'black' : 'white');
            newSectionBlack = !newSectionBlack; //style it consistently
            $('.getNew').before('<li><a href="#section'+newSectionID+'">'+newSectionID+</li>'); //attach a link to the new section to the menu
            $('body').css({width:4000*newSectionID});
            $('#'+data.id+' ul.nav').on('click','a',bindAnimation); //bind scripts
            $('#'+data.id+' ul.nav').on('click','.getNew',getNewSection);
            $('#section1 > ul li:nth-last-child(2) > a').trigger('click'); //go to new
            newSectionID++;
}

你好,我已经实现了这个功能,但不幸的是它没有起作用。我会继续尝试,因为也许我没有正确地调整您的代码。感谢您的回答,如果一切正常,我会接受它。您有 Gmail 帐户吗?我们可以聊聊吗? - gn66
我测试了代码并确保其正常工作。该代码假设您可以发送任何内容。如果您正在从另一个页面(您无法控制)加载,您可能希望改用 iframe,可以直接实现(通过替换 AJAX 调用,插入 iframe 链接),也可以使用我编写的 AJAX 方法间接使用 php 页面。 - serakfalcon
1
如果可以的话,我更喜欢在stackoverflow上进行沟通。 - serakfalcon
好的,我会继续尝试在Tumblr上实现这个功能,我已经有了一个垂直滑块,所以需要稍微调整一下。成功后我会在这里发布,谢谢。 - gn66
好的,你在tumblr上试验过了吗?我真的做不来。如果我向你展示代码,你能实现你的解决方案吗?这样我就可以看到你所做的更改,然后学习它。再次感谢。 - gn66

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接