Automatically adjusting jQuery script now functions on all hyperlinks for seamless resizing

I am attempting to create a rollover script that will resize my social network links when hovered over. The desired effect is for the 32px square icon to expand and display the trailing address. For example, when hovering over the Facebook link, it should transition from

[f] [t] [g]

to

[f /facebookpage] [t] [g]

Similarly, the Twitter link should change from

[f] [t] [g]

to

[f] [t @twitter] [g]

Below is the current script I am using:

$(function(){
    var network = $('.social-networks li a'),
    animateTime = 75,
    navLink = $('.social-networks li a');
    navLink.hover(function(){
        if(network.width() === 32){
            autoWidthAnimate(network, animateTime);
        } else {
            network.stop().animate({ width: '32' }, animateTime);
        }
    });
})
function autoWidthAnimate(element, time){
    var curWidth = element.width(), // Get default width
    autoWidth = element.css('width', 'auto').width(); // Get auto width
    element.width(curWidth); // Reset to default width
    element.stop().animate({ width: autoWidth }, parseInt(time)); // Animate to auto width
}

While this script works fine for individual icons, I encounter issues when there are multiple icons as it resizes them all together. I have attempted

$(this).find('.social-networks li a')
, but it returns the same result. Similarly,
$(this).children('.social-networks li a')
does not provide the desired outcome. Is there a way to select only the specific element being hovered over?

Answer №1

When utilizing the hover function, it is essential to animate $(this) rather than caching network. The this variable pertains to the element being hovered over, so wrap it in a jQuery object for jQuery animation purposes. It is likely that your network variable consists of an array containing all social icons.

$(function(){
                var network = $('.social-networks li a'),
                    animateTime = 75,
                    navLink = $('.social-networks li a');
                navLink.hover(function(){
                    var $this = $(this);
                    if($this.width() === 32){
                        autoWidthAnimate($this, animateTime);
                    } else {
                        $this.stop().animate({ width: '32' }, animateTime);
                    }
                });
              })
              function autoWidthAnimate(element, time){
                  var curWidth = element.width(), // Get default width
                      autoWidth = element.css('width', 'auto').width(); // Get auto width
                  element.width(curWidth); // Reset to default width
                  element.stop().animate({ width: autoWidth }, parseInt(time)); // Animate to auto width
              }

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

The issue of height and width always being zero in Chrome when using Classic ASP with JavaScript

Within my markup, I have an image field that I am setting the source for using JavaScript. I am in need of its height and width, so I utilized the image.height() and image.width() methods. Despite working properly in Internet Explorer, these methods do not ...

Background Image Division (Twitter Bootstrap)

I am facing a challenge while creating a module with a span8 width and varying height. The div contains an image that dictates its height, with text overlaid on the image. My issue lies in preventing part of the image from getting cropped when Bootstrap re ...

Achieving Height Diversity in Floating HTML Divs

I have a unique set of dynamically generated divs inside a container div with different heights. <div id="container"> <div id='d1'>Varying content...</div> <div id='d2'>Varying content...</div> < ...

The interactive div element doesn't function properly with the mouseover feature

When my mouse over feature is active, the clickable image fails to send the value to my text file. However, if I change the div tag to (this), then the value is successfully sent but the mouse over function stops working. This occurs when the mouse over f ...

Modify the color of certain terms within the input field

I'm currently working on an editor for Cucumber feature files. As I write step definitions, I'd like the keywords Given, When, Then, etc. to appear in blue text, and any text between < > brackets to show up in red. Is there a way to change ...

Dealing with AngularJS memory leaks caused by jQuery

How can I showcase a custom HTML on an AngularJS page using the given service? app.service('automaticFunctions', function ($timeout) { this.init = function initAutomaticFunctions(scope, $elem, attrs) { switch (scope.content.type) { ...

Guide for positioning buttons in front of an image using HTML and CSS

I need assistance placing buttons in specific positions above an image carousel using HTML and CSS code. Despite researching various resources, I am unable to resolve this issue. This is the HTML implementation: <!-- Image where buttons should be placed ...

Progressive reloading page with Jquery Ajax upload

I've encountered a strange issue with a Jquery form submit. After the upload completes, the page reloads even though the server hasn't finished processing. The server only returns a JSON success status, so it's not an issue on the server s ...

Identify and eliminate the shared segment between two strings to remove it

Can anyone offer advice on how to identify and remove common parts from two strings? Here is an example of my code, which doesn't always work as intended: var link = $('.some a').attr('href'); window.location.hash = link.replace( ...

Implementing a custom replication control in ASP.NET MVC 3

Is there a control available in .NET MVC 3 that functions similarly to an asp:Repeater? Any HTML helper method or other solutions? I have looked online but only found some custom solutions. Here is what I require: Users will select a number from a drop-d ...

Recognize when the DOM undergoes modifications

After taking Matt's suggestion, I have made changes to the .ready() call. In my workflow, I utilize jQuery to set up certain configurations like this: $(function () { $('.myThing').each(function() { ... configuring happens he ...

The positioning of jQuery UI elements seems to be off

I'm having an issue with the positioning of a dropdown box under its button using the jQuery UI position function. The dropdown box is not aligning correctly. Check out the fiddle for reference. $(".dropDown .dialogueBox").position({ my: " ...

Iterate through the form fields and save the information into an object

I am attempting to create a JavaScript object by looping through form inputs. const data = {}; // loop through each input found in form $('#form_name').filter(':input').each(function() { const $id = $(this).attr('id'); c ...

Wordpress Bubble Notification - Conceal at Zero / Reveal at One or More

I am a beginner in PHP and CSS. After extensive reading and learning little by little, I successfully implemented notifications in my project using PHP and CSS. It functions properly, displaying the counts (friends, messages, and notifications) and redire ...

What method can be employed to eliminate choice selection lacking any value?

Currently, I am encountering difficulties in my attempt to remove or hide the first option value from a ransack code. Would you be able to assist me in addressing this concern? Here is the HTML CODE that I am working with: <select id="q_c_0_a_0_name" ...

Moving list elements by dragging and dropping them within table cells using jQuery

I'm currently working on a project that requires the functionality of dragging and dropping list items between table cells. This means allowing users to drag an item from one cell and drop it into another, with the new location being saved. Does anyon ...

I'm having trouble getting my modal to work and display properly; I need to send it to the code behind for further assistance

I'm having trouble with my modal. I'm trying to create a sign-up modal and send it to my code behind page. I've incorporated bootstrap lumen and jquery, but I could use some assistance. Any help would be greatly appreciated. Thank you in adv ...

What is the best way to keep calling an AJAX function until it receives a response from a previous AJAX call

I am looking to continuously call my ajax function until the previous ajax call receives a response. Currently, the page is loading every second and triggering the ajax function, but I want it to keep calling the ajax function until the previous one has c ...

Is there a way to determine if my cursor is within a specific zone?

Is there a way to check, using a .hover function, if my cursor pointer is inside or outside a specific zone? You can find an example here. HTML <ul class="menuLeft"> <li>1</li> <li>2</li> <li>3 ...

Aligning container divs at the center in various screen resolutions

I recently constructed a portfolio website using Bootstrap and Isotope JS. In my layout, I have a container div which works perfectly when viewed on a desktop browser - displaying 3 divs in one line. However, the issue arises when the resolution changes an ...