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 Facebook like button seems to be missing from the webpage

Hi everyone, I'm having trouble getting the like button code to work. I used the wizard on Facebook but the button still isn't showing up, even with a simple HTML setup like this: <!DOCTYPE html> <html> <head> </head> < ...

What is the best way to use jQuery to select a group of table rows based on the value of a

My current issue involves using a jQuery selector to move table rows "UP" and "Down." Here is the Table structure in HTML: jQuery Portion : $(".up,.down").click(function() { var row = $(this).parents("tr:first"); if ($(this).is(".up")) { ...

Having difficulty retrieving JSON data using Jquery

After receiving a JSON string as a response from the server, I encountered an issue when trying to access the objects within the response, only to receive an "undefined" message. Here is the AJAX request being made: $.ajax({ url: 'somefi ...

Partial data is being received from the Ajax call

I currently have a textarea and a button on my webpage <textarea id="xxx" class="myTextArea" name="Text1" cols="40" rows="15">@ViewData["translation"]</textarea> <input type="button" id="convert-btn" class="btn btn-primary" value="Convert t ...

Sharing information using Fancybox 2

I have encountered an issue while using a form within a fancybox window to post data via Ajax to a php page. The form works perfectly fine when run outside of the Fancybox - insertion and response are both successful. However, when I run the same page thr ...

When jQuery inserts JavaScript into the DOM, it does not automatically execute the code

UPDATE: I initially used filter(), which was incorrect. I have since switched to find(), but unfortunately the issue persists. This snippet of code functions properly for me: $('#content') .replaceWith($( '<div> <d ...

Passing variables to the ajax.done() function from within a loop - what you need to know

How can I pass a variable to the .done() method of an ajax call that is inside a loop? The code snippet below shows my initial attempt: <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </ ...

The issue of Jquery selectors not functioning properly when used with variables

Currently working on a script in the console that aims to extract and display the user's chat nickname. Initially, we will attempt to achieve this by copying and pasting paths: We inspect the user's name in the Chrome console and copy its selec ...

How can Data Value be displayed instead of percentage in Gebo Pie Charts?

$.plot(elem, data, { // // series : { pie : { show : true, highlight : { opacity ...

When the user clicks on the login text field or password field, any existing text will

Currently, I am working on the login section of my website and I would like to implement a similar effect to Twitter's login form, where the Username and Password values disappear when the Textfield and Password field are in focus. I have attempted to ...

The Gulp task is not being recognized when I open the project in Visual Studio Code

Whenever I open gulp, I encounter an error related to minifying css and js files using gulp tasks. Despite my efforts to find a solution, the problem remains unresolved. Error Message: assert.js:374 throw err; ^ AssertionError [ERR_ASSERTION]: Task fu ...

What is the best way to remove an instance of the MagicSuggest JQ plugin?

Recently, I've been utilizing MagicSuggest in my forms and have encountered situations where I need to dismantle it and reinstate the input field. I have carefully reviewed the entirety of the documentation but am unable to locate a method to achieve ...

What is the proper way to send an AJAX request with the data type set to

I am currently working on creating my own POST request. Below is the function I have written: function sendPost(o) { var h = new XMLHttpRequest(); h.onreadystatechange = requestComplete; function requestComplete() { if (h.readyState = ...

Creating a CSS auto center for elements, with a unique twist

I have a design layout like this: Click here to view the image My auto-centered content is styled with the following CSS: width: 960px; margin: 0px auto; Within this auto-centered content, there is a DIV element that needs to span the full width of the ...

The art of perfect alignment: Mastering the positioning of Material-UI Grid

This issue has been resolved Hey there, I'm currently working on a React App with Material-UI and I'm trying to center a Grid item that includes a Card along with two additional Grid items. Below is the relevant code snippet. Although the Cards ...

Creating a 3-column div with varying sizes of text in another div at the center position

I am attempting to create a 3-column layout using only DIVs, but I am facing some challenges. If I were to use tables in the traditional HTML 4 way, I could achieve this: <div style="width:100%"> <table width="50%" align="center"> ...

The issue of duplicate CSS arising during the compilation of SASS into a single CSS file with G

Just getting started with Stack Overflow and Gulp (using version 3.9.1). My goal is to compile all of my scss files into a single css file for my website. Here's what I have in my gulpfile so far: var gulp = require('gulp'); var sass = requ ...

Having trouble with Viewport CSS being restricted by Blogger?

I've been working on configuring my personal blog, which is hosted on Blogger, to properly adapt to the Windows 8 IE snap feature. Through my research online, it has been suggested that I include a @viewport in the CSS. However, it seems like the CSS ...

Issue with Backbone Event Dropping Functionality

I am facing an issue with my dashboard that has two backbone Views. One of the views consists of various drop zones while the other contains items with draggable="true". Surprisingly, the drop event is not being triggered in these drop zones; however, they ...

The basic AJAX does not seem to be functioning

Exploring the world of ajax, I'm having trouble with the following code: $(document).ready(function() { $("button").click(function() { $.post("\tests\test1.php", function(data) { alert(data) }); }) }) <script src="htt ...