简体   繁体   English

如何在indexPage上加载tumblr nextPage的内容

[英]How can I load the content of tumblr nextPage on indexPage

I'm developing a tumblr theme for my personal portfolio, but I'm encountering a serious problem in my work section, I want to create an horizontal slider effect, when I click next to see older posts, 我正在为我的个人作品集开发一个tumblr主题,但是我在工作部分遇到了一个严重的问题,我想创建一个水平滑块效果,当我点击下一个以查看较旧的帖子时,

like this: 像这样:

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

I have this to define my nextPage, #section4 is where I have the posts of my work: 我有这个来定义我的nextPage,#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 -->

Well, this is defined to open an other page: 好吧,这被定义为打开另一个页面:

"indexPage".tumblr.com#section4

when I click next it goes to: 当我点击下一步它去:

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

Is there a way I can do this slide effect on my index page or at least give the illusion, that I'm making slide effect, when moving to other page? 有没有办法可以在我的索引页面上做这个幻灯片效果,或者至少给人一种错觉,我正在制作幻灯片效果,当移动到其他页面时?

If I understand your question correctly, it is as serakfalcon mentioned but I'll add in tumblr specific points. 如果我正确理解你的问题,就像serakfalcon提到的那样,但我会添加tumblr特定点。 You need to do three things specifically. 你需要专门做三件事。 I won't go into too much tumblr template codes and styling it and stick to these 我不会进入太多的tumblr模板代码并对其进行样式化并坚持使用这些代码

  1. Loading the data dynamically 动态加载数据
  2. Dynamically creating a new section/page 动态创建新的部分/页面
  3. Getting the navigation to work with new sections 让导航与新部分一起使用

Loading the data 加载数据

This is actually the easy bit. 这实际上很容易。 For tumblr, as you mentioned, we can manually go to the next page by clicking the "next page" link. 对于tumblr,正如您所提到的,我们可以通过单击“下一页”链接手动转到下一页。 In tumblr this will look something like 在tumblr中,这看起来像

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

Since the pages are on the same domain, we can override the link to get the page using AJAX. 由于页面位于同一个域中,我们可以覆盖链接以使用AJAX获取页面。 We will be getting the raw HTML for this page. 我们将获取此页面的原始HTML。 Which will include all the other parts of the page like the three other sections. 其中包括页面的所有其他部分,如其他三个部分。 There may be some tumblr template magic that we can do to limit this, but I'll consider that outside the scope. 我们可以做一些tumblr模板魔术来限制这个,但我会考虑在范围之外。 So how do we get the content specifically? 那么我们如何具体获取内容呢? We can feed the raw HTML into jquery for it to build document objects, and we can then select the content that has the post from there. 我们可以将原始HTML提供给jquery,以便构建文档对象,然后我们可以从那里选择包含帖子的内容。 So something like. 所以像。

$(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"
    })
})

Creating a new section/page 创建新的部分/页面

Once we have the data, we now need to put the data into a new page. 获得数据后,我们现在需要将数据放入新页面。 There's many ways on doing this but here's how I did it. 有很多方法可以做到这一点,但这就是我如何做到这一点。 First of all, I had a template for the new section. 首先,我有一个新部分的模板。 I put it in a script tag like so. 我把它放在一个像这样的脚本标签中。 I kind of like the semantics of doing it like this. 我有点像这样做的语义。

<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>

With that done, whenever the data is loaded we can get the raw HTML from this, and create a the new elements by feeding it to jQuery again. 完成后,无论何时加载数据,我们都可以从中获取原始HTML,并通过再次将其提供给jQuery来创建新元素。 We then append the posts to the div with class posts . 然后我们将帖子附加到带有课程postsdiv We also get the next page link from the data to attach to the next-page link so that the earlier ajax call will work. 我们还从数据中获取下一页链接以附加到下一页链接,以便早期的ajax调用可以工作。 If we can't find the next-page link, that means there's no more posts. 如果我们找不到下一页的链接,那就意味着没有更多的帖子了。 So we can hide the next-page link. 所以我们可以隐藏下一页链接。 We'll also stop the existing link to load data through ajax and do the normal slidey thing. 我们还将停止现有链接以通过ajax加载数据并执行正常的滑动操作。 Finally we'll append the new section to the parent div of the sections, fix it's width and then transition to the new section. 最后,我们将新部分附加到部分的父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

Getting the navigation to work 让导航工作

I probably should append new links to the nav, but I guess to make it easier (and imagine how it look like with 40 pages) I decided to just use a "next page" and "last page" for the loaded content. 我可能应该添加新的链接到导航,但我想让它更容易(并想象它有40页的样子)我决定只使用“下一页”和“最后一页”来加载内容。 The code is simple. 代码很简单。 For next page, if it's not .static , move to the next section and ditto last page. 对于下一页,如果它不是.static ,请转到下一部分,然后转到最后一页。 we'll delegate (technically, I'm using .on() but the docs on .delegate seemed easier to understand) it to the document body so that it works for all the new links. 我们将委托 (技术上,我使用.on()但是.delegate上的文档似乎更容易理解)它到文档正文,以便它适用于所有新链接。

So as a whole javascript/jquery will look something like 所以整个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

Here's a live demo of it working. 这是一个有效的现场演示 Didn't really bother with making the slides look nice but hopefully you found this helpful. 没有真正打扰让幻灯片看起来不错,但希望你发现这很有帮助。

Of course, everything has to happen on one page, or you can't exactly have the transition. 当然,一切都必须在一个页面上发生,否则你无法完全实现转换。 So, either you load everything at run time, or you 'cheat' using AJAX. 因此,要么在运行时加载所有内容,要么使用AJAX“欺骗”。

alter the in-page javascript to the following: 将页内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);
        });

This allows you to add a elements dynamically to the navs, and will attach an event listener to a new element we will use to get new content. 这允许您动态地向导航添加元素,并将事件侦听器附加到我们将用于获取新内容的新元素。 Modify the nav menu so that the last element is something like: 修改导航菜单,使最后一个元素类似于:

<li class="getNew">New</li>

There. 那里。 now clicking on that will load the new content. 现在单击它将加载新内容。 add this javascript code to your site above the previous javascript code: 将此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'
   });
}

This javascript will POST a number corresponding to which section to load to a new file, LoadSection.php, which needs to respond with the HTML of the new section, the ID and name it should be called. 这个javascript将POST一个对应于哪个部分的数字加载到一个新文件,LoadSection.php,它需要响应新部分的HTML,它应该被调用的ID和名称。 loadSection.php can look like this: 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);

Now, your page can load content dynamically! 现在,您的页面可以动态加载内容! Note, the nav bar should probably be restructured to be an absolute element that there's only one of instead of repeated on every page, and there's other stuff you can clean up but that's to be expected if you're taking a codrops example. 请注意,导航栏可能应该重新构造为一个绝对元素,只有一个而不是在每个页面上重复,并且还有其他东西可以清理,但如果你正在考虑一个例子,这是可以预期的。

EDIT: 编辑:

tumblr doesn't let you run server-side scripts on their server (makes sense), which are required for the AJAX method described above to work. tumblr不允许您在其服务器上运行服务器端脚本(有意义),这是上述AJAX方法工作所必需的。 However, there are a number of options available to you. 但是,您可以使用多种选项。 1) redirect the tumblr page to a site you control, and use the method described above. 1)将tumblr页面重定向到您控制的站点,并使用上述方法。 2) use an iframe. 2)使用iframe。

The iframe method could be done directly, which requires you to specify the urls of all the pages in advance (or at least they'll need to follow a predictable pattern), or indirectly using a AJAX script similar to the one above. iframe方法可以直接完成,这需要您提前指定所有页面的URL(或者至少它们需要遵循可预测的模式),或间接使用类似于上面的AJAX脚本。 I will demonstrate loading a direct iframe, I'm sure you can figure out the rest. 我将演示加载一个直接的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++;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM