Utilizing a CSS property within jQuery

Here is the code snippet: 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'
        });
    })
})

At line 20 of the code, I'm looking for a way to reference the 'top' attribute of the selected list item in order to synchronize the animation of the pointer div with it. However, I'm struggling to find out how to retrieve that specific attribute value.

Any suggestions or solutions are appreciated!

Answer №1

Do you want it to look like this?

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

DEMO

Answer №2

VIEW DEMO

Take a look at this solution. You don't have to provide the top position as it varies 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
        });

    })
})

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to place a list within a <div> element?

There is a <div> with an image in the background. I now want to include a list within that div, but I'm having trouble positioning it. Check out this image for reference In the image above, you can see the background and the list, but I'm ...

Callback after completion of a for loop in Angular TypeScript

During the execution of my javascript async function, I encountered a situation where my printing code was running before the div creation. I needed to ensure that my print code would run only after the completion of the for loop, but I struggled to find a ...

Steer clear of directly altering a prop within reusable components

I am facing an issue with reusing a custom dropdown that I have created in my component file where props are the value options in the dropdown. When I try to select the dropdown, I get a Vue warning message: [Vue warn]: Avoid mutating a prop directly sinc ...

What is the best way to modify .js source files while using AjaxControlToolkit?

Currently, I am tackling the source code for AjaxControlToolkit in the VS2008 version. There are specific modifications I need to implement in the core javascript files like MicrosoftAjaxWebForms.js, but unfortunately, I am unable to locate the original so ...

Dimensions of Embedded Instagram Content

I attempted to integrate an Instagram photo into my iOS Swift application by following the instructions provided on the Facebook developers website. However, I encountered an issue where the Instagram post did not adjust its width to fit within the designa ...

Start the CSS3 animation in reverse right away

I am trying to achieve a "flashing" effect by adding the .shown class to my #overlay, causing the opacity to fade in for 2 seconds and then immediately reverse, fading out for another 2 seconds. After experimenting with removing the class, I found that it ...

Recurrence of solely the middle segment of the separator's background picture

I have a unique divider image with fading top and bottom parts. I'm wondering if it's possible to use a sprite and 3 divs to only repeat the middle section, considering that my height is variable. Do I need separate images for the top, middle, an ...

Utilizing React Material UI to divide the screen in half with one half being scrollable and the other half remaining fixed using UI Grid

My setup involves two Grid components, with one structured like this: <Grid container alignItems="stretch" spacing={3}> <Grid className="left-pane" item md={4} xs={12}> <Navigation /> </Grid> <Grid className="right-pan ...

Issue: "fourSquareService.retrieveLocations does not exist as a function"

Struggling with AngularJs - How to Utilize Factory Alongside Form Input in URL for Data Fetching? This is my first time asking a question here. I've been diving into Angular and wanted to organize my services by separating them into different files. ...

What are some tips for improving the smoothness and efficiency of my CSS @keyframes animation on SVG elements specifically in Firefox?

I noticed that this particular animation is causing high CPU usage on all browsers, although it runs more smoothly in Chromium. Is there a way to optimize its performance? Here's the SCSS code which is relatively simple: @keyframes laptop { from { ...

The Javascript code is not running due to the XSS security measures in place

Within my Wordpress website, I have crafted an HTML form that enables users to view database records corresponding to their input. Following this AJAX tutorial on w3schools, a JavaScript function was implemented to send a GET request to a PHP script on the ...

Invoke a function within the <img> tag to specify the source path

I have been attempting to achieve something similar to the following: <img id="icon" class="cercle icon" src="getIcon({{item.status}})" alt=""> This is my function: getIcon(status){ switch (status) { case 'Ongoing': ret ...

Retrieve the $scope value assigned to ng-model

Is there a way to extract only the value from an object created by ng-model in Angular JS and use it in my script rather than both the key and value? I'm on the verge of solving this issue that stems from my previous post here: Get a value from input ...

The Access-Control-Allow-Origin policy does not permit the origin <origin> to be accessed

Sorry, the XMLHttpRequest cannot be loaded from http://localhost:8080/api/test. The origin http://localhost:3000 is not permitted by Access-Control-Allow-Origin. After reading up on cross-domain ajax requests, I now understand the security concerns involv ...

The request included an unsupported media type of "text/plain;charset=UTF-8". This caused an error in the NextJS API when interacting with Django Rest Framework

Currently diving into the world of web development, I am endeavoring to construct a website utilizing NextJS and Django Rest Framework. While NextJS effectively proxies API endpoints for retrieving data, I find myself grappling with making it work for a PO ...

Using Blob to save CSV file on Safari

Here are the codes I am using to generate a download link for users to download a .csv file from my website. var link = document.createElement("a"); link.id = "csvDwnLink"; window.URL = window.URL || window.webkitURL; var csv = "\ufeff" + CSV, b ...

Having a concise way to submit forms with Javascript and JQuery

Seeking a more concise way to submit form data to Electron in JSON format as a beginner in JavaScript and jQuery. How can this code be rewritten for brevity? <section> <input id="server" type="text" placeholder="Server"> <i ...

The HTML webpage is optimized for desktop and tablet viewing, but has difficulty displaying correctly on mobile phones

Just starting out with HTML and created a website using HTML and CSS that is now live. It looks good on desktop and tablet (iPad) but not so great on mobile devices. Tried different solutions, including viewport settings, to fix the issue with no success. ...

Vue.js: Retrieving and Using Data from the API Repeatedly

<template> <h1 class="text-lg text-gray-400 font-medium">Tracker dashboard</h1> <div class="flex flex-col mt-2 w-3/4"> <div class="-my-2 overflow-x-auto sm:-mx-6 lg:-mx-8"> ...

Changing the Parent div style based on checked radio input in Reactjs

As a Reactjs beginner, I am looking for assistance in changing the background color of a faculty item (.faculty) to red when the respective radio input is checked. This should be achieved using the onChange method in Reactjs. Thank you in advance for you ...