0

I am attempting to add an animated scroll to my Tumblr and I have found one in this question: Smooth scroll to div id jQuery. But the trick is, I need the button to jump to a specific post on the Tumblr.

I have found that each post has a specific id called PostID, and I have located the specific PostID I need. What I cannot find is an actual use of the PostID in the HTML, so I do not know how to target it. I tried:

$("#button").click(function() {
    $('html, body').animate({
        scrollTop: $("post-{130746374784}").offset().top
    }, 2000);
});

But I am sure I am calling it wrong. Can anyone tell me how to target it?

Community
  • 1
  • 1
Da Hao
  • 49
  • 6

1 Answers1

1

The simple preamble

As pointed out by @approxiblue in this comment, Tumblr offers a variety of variables that can be used in a theme, including PostID.

It would therefore be possible to replace something like this:

<div class="post photo">
    <!-- more HTML here -->
</div>

With something like this:

<div class="post photo" id="myPost_{PostID}">
    <!-- more HTML here -->
</div>

Allowing a more direct targeting of the post, as such:

var post = $('#myPost_129129898812');

The long answer

Observing a Tumblr page, it seems the PostID is only available in links. This means we have to find the link including the PostID, then go back up to its container div.

This is an example of a Tumblr post on a homepage:

<!-- PHOTO POST 
///////////////////////////////////////////////////////-->
<div class="post photo">
    <div class="wide">
        <a href="http://stolenjokescomic.tumblr.com/image/129129898812">
        <img src="http://41.media.tumblr.com/16c2f56a9f739889fbd4896f8524907c/tumblr_nupf28wgLl1toph1no1_1280.png" alt="" />
        </a>
    </div>
    <div class="narrow">
        <div class="metadata">
            <div class="date">
                <a href="http://stolenjokescomic.tumblr.com/post/129129898812">Sep. 15 2015</a>
            </div>
        </div> <!-- metadata end -->
    </div> <!-- narrow end -->
</div> <!-- post end -->

We will:

  1. Target the element with class post
  2. Target the child with class date
  3. Target the child link with attribute href containing post/129129898812, which is the PostID used in this example
  4. Go back up by targeting the parent with class post using parents()
  5. Get the position

Code:

$("#button").click(function() {
    var position = $('.post .date a[href*="post/129129898812"]')
        .parents('.post')
        .offset().top;

    $('html, body').animate({
        scrollTop: position
    }, 2000);
});

You can try it out by:

  1. Going to stolenjokescomic
  2. Opening a scratchpad (SHIFT+F4 in Firefox)
  3. Pasting this code:

    $('html, body').animate({
        scrollTop: $('.post .date a[href*="post/129129898812"]')
            .parents('.post').offset().top
    }, 2000);
    
  4. Running it (CTRL+R in Firefox)

The page should scroll down to the post. Unless you read this in a few months and the post is not on the homepage anymore.

Alternative for the OKMove theme

  1. Get the target element using the PostID.
  2. Get the index of the target within the list of posts.
  3. Get the element to which the zoomIn function is attached.
  4. Trigger the click event to call the zoomIn function as many times as necessary to reach the target.

Code:

var zoomIn = $('.zoom-in');
var target = $('#content section .about a[href*="post/18629325910"]').parents('section');
var id     = $('#content section').index(target[0]);

for(var i=0; i<id; ++i) {
    zoomIn.click();
}

You can try out this code on OKMove using a scratchpad.

If this goes too fast, we can adjust using a a recursive setTimeout:

var zoomIn = $('.zoom-in');
var target = $('#content section .about a[href*="post/18629325910"]').parents('section');
var index  = $('#content section').index(target[0]);

var count = 0;
var weNeedToGoDeeper = function() {

    if(count < index) {
       zoomIn.click();
       setTimeout(weNeedToGoDeeper, 400);
    }

    ++count;
};

weNeedToGoDeeper();

We can expand this further to work from any position, analyzing the current position by parsing the transform property and then decide which way to zoom and how many times to do so by calculating the relative position between where we are and where we want to go:

function parseMatrix(s) {
    return (
        s.match(/^matrix3d.(.*).$/) || ['', '0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0']
    )[1].split(', ');
}

var zoomIn  = $('.zoom-in');
var zoomOut = $('.zoom-out');
var content = $('#content');
var target  = $('#content section .about a[href*="post/18629325910"]').parents('section');
var index   = $('#content section').index(target[0]);

var pos = parseMatrix(pos = content.css('transform'))[14];

pos = Math.round(pos/1000);
pos = index - pos;

var zoomElem = pos <= 0 ? zoomOut : zoomIn;

pos = Math.abs(pos);

for(var i=0; i<pos; ++i) {
    zoomElem.click();
}
Community
  • 1
  • 1
spenibus
  • 4,339
  • 11
  • 26
  • 35
  • First of all thankyou so much for this detailed break down. Now I understand how you approached this. However I did try the code and replaced the id number with mine on my tumblr, it is still not responding. At this point I do not even know if the issue is fundamentally with the scroll code or anything else. I am using a theme called okmove (okmove.tumblr.com) , which already has an inbulit script that creates an interesting scroll effect. Could this be a problem of conflict? Originally I thought it would not clash as the browser scroll is not affected. – Da Hao Oct 08 '15 at 16:14
  • @DaHao I'll need to test that. – spenibus Oct 08 '15 at 16:16
  • I'm afraid I do not have their code. Is it possible to use their site to scratchpad it? – Da Hao Oct 09 '15 at 02:34
  • @DaHao I've been trying things already, got a few ideas but it's more complex than a regular page. – spenibus Oct 09 '15 at 09:53
  • I see..would it be possible if we targeted specific percentage of the scroll bar? My tumblr posts are fixed. There will be no new posts. So I can confirm the % mark of the bar for each post. would tht be easier? – Da Hao Oct 09 '15 at 12:47
  • @DaHao That may be indeed considerably easier. I'm putting that on the list of things to test. – spenibus Oct 09 '15 at 12:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/91859/discussion-between-da-hao-and-spenibus). – Da Hao Oct 09 '15 at 13:44
  • @DaHao I updated with a solution for OKMove. It should work in a fairly *standard* way rather than being too specific. – spenibus Oct 09 '15 at 22:11
  • Alright, now I got it to work on my tumblr via scratchpad, however, when I try to insert the code into my button code, it does not work. Would you teach me how to implement this script as a button? Almost there! – Da Hao Oct 10 '15 at 05:27
  • @DaHao Get in the chat above, comments might be a bit tedious for that. – spenibus Oct 10 '15 at 09:33
  • Instead of grabbing the post ID from the link, you can change the theme code and use the variable [`{PostID}`](https://www.tumblr.com/docs/en/custom_themes#posts) to output the post ID wherever you need. – approxiblue Oct 13 '15 at 02:45
  • @approxiblue That does sound better. Clearly a trick to make me read more documentation. – spenibus Oct 13 '15 at 13:25