1

I have this code: http://jsfiddle.net/cmac2712/dnLgD/

$(document).ready(function () {
    var setVisibility = function(n){
        $('#content1').css('visibility', 'hidden');
        $('#content2').css('visibility', 'hidden');
        $('#content3').css('visibility', 'hidden');
        $('#content4').css('visibility', 'hidden');

        $('#content' + n).css('visibility', 'visible');
    };    
    $('#op1').click( function () {
        setVisibility("1");
        $('.pointer').animate({
            top: '16px'
        });
    })
    $('#op2').click( function () {
        setVisibility("2");
        $('.pointer').animate({
            top: '38px'
        });
    })
    $('#op3').click( function () {
        setVisibility("3");
        $('.pointer').animate({
            top: '60px'
        });
    })
    $('#op4').click( function () {
        setVisibility("4");
        $('.pointer').animate({
            top: '82px'
        });
    })
})

On line 20 I want to pass in a value that references the 'top' attribute of the option list item so that the pointer div animates to the same position as the list item that is clicked, however I'm not sure how to reference that attribute.

Any ideas?

2 Answers 2

1

Like this?

top: $(this).offset().top

DEMO

Sign up to request clarification or add additional context in comments.

Comments

0

DEMO HERE

Check it. You need not give the top position because it differs for each screen resolution

$(document).ready(function () {


    var setVisibility = function(n){
        $('#content1').css('visibility', 'hidden');
        $('#content2').css('visibility', 'hidden');
        $('#content3').css('visibility', 'hidden');
        $('#content4').css('visibility', 'hidden');


        $('#content' + n).css('visibility', 'visible');
    };

    $('#op1').click(


    function () {
        setVisibility("1");
        $('.pointer').animate({
            top: $(this).offset().top
        });

    }

    )


    $('#op2').click(

    function () {
        setVisibility("2");
        $('.pointer').animate({
            top: $(this).offset().top
        });

    })

    $('#op3').click(

    function () {
    setVisibility("3");
        $('.pointer').animate({
            top: $(this).offset().top
        });
    })

    $('#op4').click(

    function () {
    setVisibility("4");
        $('.pointer').animate({
            top: $(this).offset().top
        });

    })
})

Comments

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.