-1

I am trying to add a jQuery animation into my Wordpress posts.

The animation works fine, but what is hard is how to do it for each individual post. Meaning, each post is assigned a separate ID (e.g. post-xxxx) and the only way of knowing the post ID (that I know of) is through Wordpress' the_ID() function.

I have my jQuery running in script tags in the index PHP file. How would I go about doing this? My only good idea was to add an onclick HTML listener to the DIV tag for the post and then use the this reference in the jQuery code but it never executed (not sure if my code was wrong).

Here is the code for that thing I tried:

<script>
    function('test') {
        $(this).animate({
            height: '+=350'
        }, {
            duration: 500
        });

        $('.review-thumb').animate({
            opacity: 0
        }, {
            duration: 500
        });
    },
</script>

With this being my HTML tag:

<div onclick="test" class="post number-<?php echo $number; ?> colour-<?php echo $colour; ?>" id="post-<?php the_ID(); ?>">

The website theme can be found at http://vusavusa.com/blog/?theme=vusavusa2. Please excuse the messiness of content right now.

1
  • And what is your question? Commented Feb 4, 2013 at 23:45

2 Answers 2

2

You can do something like this assuming that all the Posts starts with post- identifier. The selector below will grab all inputs that has an id that starts with post-.

$('input[id^="post-"]').animate({ height: '+=350' }, { duration: 500 });

The concept can be adapted to whatever you're trying to do such as:

/** Assign an animation to each element */
$('input[id^="post-"]').click(function() {
    $(this).animate({ height: '+=350' }, { duration: 500 });
    return false;
});

Another option is to use slideToggle to animate to expand your Post into a full view. your full view will be preloaded but is hidden by default and will have a class of post-full.

CSS:

.post-full { display: none; }

JS:

$(function() { 
    $('[id^="post-"]').click(function() {
        $(this).find('.post-full').slideToggle(500, function(){});
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

18 Comments

Hi, thanks for the quick response! I tried that and it doesn't seem to do anything. I have it live right now on my website link, so you can see.
You need to remove that onclick="test" and add what I have above somewhere you your JS file.
Sorry, I noticed that too. I did that, and your code is in the website. Line 430 on page source.
Well I'll be damned... I didn't realize jQuery could target elements that way. +1 for you, good sir.
Anything more to add anyone?
|
0

Why don't you add a class (e.g. "postClass") and use $('.postClass')

1 Comment

Then every box would animate when you click just one of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.