Preserve the original position of the inner <div> when animating the container <div> using JQuery

I am currently working on a small website and encountering some challenges with jQuery animation. My issue involves placing a single character text inside a circle and wanting it to grow when the user hovers over it, while keeping the inner text in its original position. The circle should shrink back to its original size upon the mouse leaving the area. Although the growing/shrinking aspect is functioning properly, the problem lies with the inner text changing its position upon hover.

Below is the HTML code:

 <body>
 <div class="steps">
    <div id="one" class="number">
        <div id="num-text">
            <p><strong>1</strong>
            </p>
        </div>
    </div>
 </div>
 </body>

In this setup, 'steps' acts as a container and 'number' represents the circle. You can find a JSFiddle link for this question here: http://jsfiddle.net/Rockr90/hZSKA/

Thank you!

Edit: It seems that the flickering issue only occurs in Chrome. The example using CSS3 works fine on IE and Firefox, leading me to believe that it might be related to webkit?

Answer №1

You can achieve this effect using just CSS! There is no need for jQuery in this case. Let me show you how it's done with an example. I utilized the display table property to create a circle and used display table-cell to center the text perfectly within it.

HTML

<div class="circle">
    <p>1</p>
</div>

CSS

.circle {
    position:relative; //Setting up position (not necessary, but included for demonstration)
    top:100px;
    left:100px; // Setting width and height
    width:100px;
    height:100px;
    display:table; // Using table display property for centered <p> with table-cell
    background-color:blue;
    border-radius:50%; // Creating a circular shape
    -webkit-transition: all 0.5s; // Adding transition effects
    -moz-transition: all 0.5s;
    -ms-transition: all 0.5s;
    -o-transition: all 0.5s;
    transition: all 0.5s;
}

.circle:hover {
    margin-left:-10px; // Increasing height and width on hover
    margin-top:-10px; // Setting negative margins to keep it in place
    width:120px;
    height:120px;
}

.circle p {
    display:table-cell; // Magic of table-cell display
    vertical-align:middle; // Centering the text vertically
    text-align:center;
    font-size:2em;
    color:white;
}

FIDDLE

http://jsfiddle.net/n6D46/

Answer №2

To center #num-text vertically by giving it a height, you can utilize the absolute positioning already in place:

#num-text {
    position:absolute;
    text-align:center;
    color:#eee;
    font-size:24px;
    width:100%;
    height: 24px;
    top:50%; margin-top:-12px; }

Check out the demonstration here: http://jsfiddle.net/hZSKA/1/

It may be possible to achieve the same effect using CSS3, but this approach may not be compatible with older browser versions.

Answer №3

If you're looking for a quick solution to add some animation, consider incorporating #num-text into the animation:

     function () {
        $(this).animate({
            height: '-=10px',
            bottom: '-=5px',
            width: '-=10px'
        }, 50);
        $('#num-text').animate({'top': '-6px'}, 50)
    });
});

Check out the demonstration on JSFiddle: http://jsfiddle.net/hZSKA/5/

Keep in mind that there may be more optimized solutions available as well.

UPDATE: Apologies for linking to the incorrect JSFiddle before.

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 enable my search function to filter out specific items from a table?

Currently, I have created a table populated with data fetched from a JSON file. Now, my focus is on implementing a search functionality that filters out items based on user input and displays only those table rows matching the search criteria. The code sni ...

Show values in an angular template using an array

I want to showcase values of an array using *ngFor const blockData = [ {text: sampleText1, array: [val1]}, {text: sampleText2, array: [val2, dat2]}, {text: sampleText3, array: [val3, dat3]} ] <div *ngFor="let data of blockData"> ...

What is the process of creating and customizing popovers in Bootstrap 5 with jquery?

Is there a way to dynamically create and add content to Bootstrap 5 popovers using JavaScript or jQuery? In Bootstrap 3, I used the following method: $('#element').popover({ placement : 'left', trigger : 'focus', html: true } ...

Error message: The Bootstrap .dropdown() method failed because it encountered an "Uncaught TypeError: undefined is not a function"

I've encountered an issue that seems to be a bit different from what others have experienced. Despite trying various solutions, I still can't seem to fix it. I suspect it might have something to do with how I'm importing my plugins. The erro ...

Having trouble storing the message ID within the user Object using Mongoose

My goal is to add a message to the messages property (an array) of a user object using Mongoose. However, I encounter an error when trying to save the user (user.save()). To troubleshoot, I added three console.log statements in the code below. Can anyone h ...

What is the best way to deploy a Vue Webpack application using Sails.js?

I am currently managing a Sails.js server alongside a Vue Webpack application, running as separate entities. Here's how my folder structure is laid out: /application-root/server/ /application-root/client/ Within the /server/ directory lies my Sails ...

Having trouble with JavaScript concat function not behaving as it should? Can you provide more details to

I am trying to extract all the cities from an object that contains country names as keys and arrays of cities as values. However, my approach seems to be failing and I can't figure out why. var cities = { "United Kingdom": ['london'], ...

When using iOS, inserting an iFrame with a source URL (SRC) on a webpage will automatically open the URL

Currently facing a peculiar issue within a Cordova app, specifically on iOS. The scenario is: I have an HTML page with existing content. At a later stage, I fetch additional HTML from a remote source and inject it into the original HTML page. However, whe ...

Excluding node modules when not included in tsconfig

Within my Angular project, there is a single tsconfig file that stands alone without extending any other tsconfigs or including any additional properties. Towards the end of the file, we have the following snippet: "angularCompilerOptions": { ...

Displaying iframes in AngularJS using a service

I am currently developing an Angular app and encountering some difficulties with rendering a Soundcloud embed iframe in my HTML. The issue arises when I try to display the tracks stored in the array generated by my getTracks function. Despite successfully ...

Trigger an AJAX request to execute a PHP script when a button is clicked

When I click a button, I want to run an ajax function that calls a PHP script and displays the resulting data under a specific div. However, it is not currently working as expected. Upon checking the console, I noticed that no value is being passed to the ...

Exploring an Array of Arrays with jQuery

I have this code snippet and I am attempting to understand how to search through an array of objects where the call() function is invoked multiple times? var arr = []; var newData; function call() { newData = $('a').attr('href'); ...

Feeling trapped during the process of setting up a intricate jQuery Image Slider

I've hit a roadblock while trying to make changes to the slider located at THIS URL. In the current setup, each thumbnail corresponds to one main display image. Clicking on a thumbnail displays a single image and then proceeds to slide to the next th ...

Rails: Problematic Jquery Ajax behavior when using slidetoggle

Currently, I am facing an issue with the comment section on my micropost. I want to be able to click a button within each micropost so that the corresponding comment section (which is initially set to display:none in the CSS) slides and becomes visible. Ho ...

Proceed to the next request after the initial request has finished executing in node

Imagine there is an endpoint called /print. Each time a request is sent to this endpoint, it triggers the function printSomething(). If another user hits this endpoint while printSomething() is still processing, it will run the function again simultaneousl ...

Why isn't my onScroll event triggering in my React.js application? What mistake am I making?

I am facing an issue with my onScroll event in react js. My goal is to implement infinite scrolling in react js, but unfortunately, the onScroll event is not triggering as expected. The process involves fetching posts from an API and passing them to the ...

"Implementing AngularJS bidirectional data binding to dynamically link user inputs with corresponding fields

Having trouble automatically outputting data with angularJS. One of the great features of angular is two-way data binding, but I can't seem to bind input with a JSON file. What I want to achieve is if the user's input matches a key, the correspon ...

What steps should I take to incorporate a timer into my bot similar to the timer used by other giveaway bots

I am looking to add a timer to my bot giveaway embed message that continues to update itself even when the bot is offline, without showing that the message was edited. Here's what I currently have in my embed: const embed = new MessageEmbed(); ...

Maintain consistent spacing after receiving a value

I have a content editable span where you can write whatever you want. For example: Hello, My name is Ari. However, when I try to retrieve the text and alert it using my program, it displays without any line breaks or spacing like this: "Hello,My name is ...

Creating a new route in a Express JS server to specifically handle POST requests

Just starting to learn the ropes of Javascript, and I'm diving into express to create an application that will enable users to craft new recipes, explore existing ones, and view details about each recipe. To get things moving, I've launched my s ...