Having trouble getting a div to slide to the left on hover and collapse on mouseout using Jquery?

Hey there! I need some assistance in completing this code snippet.

I have a div with overflow:hidden and a margin-left of 82px. Inside it, there is a share link and a ul list containing three external links.

When I hover over the share link, everything works fine - the div slides to the left back to margin-left: 0.

But I want the same behavior when I mouseout of the parent div, not just the share link. Currently, if I move my mouse over the ul list, the whole layout becomes unstable.

What I'm looking for is when a link is hovered over, the div will slide to the left, revealing three links which users can click on. And then, when the user moves away from the div, it collapses again.

Below is the jQuery code I've written so far:

$("#shareBtn").mouseover(function(){
        var $marginLefty = $(this).parent();
    $marginLefty.animate({
        marginLeft: parseInt($marginLefty.css('marginLeft'), 40) == 0 ? $marginLefty.outerWidth() : 0
    });
}).mouseout(function(){
        var $marginLefty = $(this).parent();
    $marginLefty.animate({
        marginLeft: parseInt($marginLefty.css('marginLeft'), 40) == 82 ? $marginLefty.outerWidth() : 82
    });
});

And here's the corresponding HTML:

 <div id="player">


   <div id="share_feature">
      <div>
        <a id="shareBtn" href="#">share</a>
        <ul id="player_social">
            <li><a href="#">google</a></li>
            <li><a href="#" >facebook</a></li>
            <li><a href="#" >twitter</a></li>
        </ul>
    </div>
  </div>

I would greatly appreciate any help you can provide. Thank you!

Answer №1

there are numerous examples available on this particular subject.

You can try using the following code snippet:

$("#shareBtn").bind('mouseenter mouseleave',function(event){
    var $marginLefty = $(this).parent();
    if(event.type == 'mouseenter') {
        $marginLefty.animate({
            marginLeft: parseInt($marginLefty.css('marginLeft'), 40) == 0 ? $marginLefty.outerWidth() : 0
        });    
    } else if(event.type == 'mouseleave') {
        $marginLefty.animate({
            marginLeft: parseInt($marginLefty.css('marginLeft'), 40) == 82 ? $marginLefty.outerWidth() : 82
        });    
    } 
});

I have not personally tested it, but by utilizing this code, you can avoid unnecessary searching for $(this).parent() and achieve the desired functionality.

Answer №2

Give this a shot in conjunction with your existing code to make the div collapse when you leave it.

$("#share_feature").mouseleave(function(){
  $(this).animate({
      marginLeft: parseInt($(this).css('marginLeft'), 40) == 0 ? $marginLefty.outerWidth() : 0
  });
})

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

Would parallax stars be overwhelming for a webpage?

I'm a newcomer to the world of web development and I have an idea to make stars move across my website. However, I'm concerned about whether this might be too much for a simple website to handle. Is there a way to optimize the code? const canvas ...

Issue with Node/Express: Middleware addition to router does not function as expected

Here is the configuration of my router: app.get('/getReport', (req, res) => { res.send("This is the report"); }); Initially, this router functions properly and successfully displays the message This is the report in the browser However, ...

"Exploring the Power of Logarithmic Slider with Vue and Quasar

I'm currently working on a project utilizing Vue 2 and Quasar 1, where I am attempting to develop a logarithmic slider. Initially, I managed to create a basic example using a native input slider in this code pen: https://codepen.io/tonycarpenter/pen/Z ...

Is it possible to swap a <div> element with the content of another HTML page using the .innerHTML method?

I am currently working on a project that involves loading different webpages into a <div> on my page once specific links are clicked. I came across a thread about using jQuery for this purpose, but I'm not familiar with it. Is there a way to ach ...

How can you override the selected classes for menu items in Material-UI using React.js?

Hey there! I'm facing an issue where I can't override the class that displays the correct styling when a menuItem is selected. Below is my code: <MenuItem component={Link} to="/Acceuil" className={{root:classes.menuItem ,selected:cla ...

Avoid triggering the sum button on click after every successful AJAX call in jQuery

Hello, I apologize if my title question is not clear as I am a bit confused. Let me explain the scenario: I am creating a looping post timeline similar to Facebook, where each post has comments. The structure is as follows: ++++++++++++++++++++++++++ Po ...

Encountering a Forbidden Error with Superagent

Here is the content of my index.js file I am attempting to fetch a response from a sports data API. I can successfully send curl requests to it, but when trying this method, I encounter a 403 forbidden error. var express = require('express'); v ...

What could be causing my ajax request to not be successfully compiled?

I seem to be experiencing an issue with this part of my code. Despite trying multiple methods, it still isn't functioning correctly. What steps can I take to resolve this problem? $.ajax({ url: "https://corona-api.com/countries/BR", ...

Importing modules from another module in Node.js

I'm currently working on a nodejs app that consists of two files, index.js and ping.js. The main functionality is in index.js which handles routing and other tasks, while ping.js utilizes phantomJS. In my index.js file, I have the following line of co ...

There seems to be an issue with .children .val() returning an

I'm currently working on form validation, but encountering an issue where .val() is returning undefined. $(document).ready( function() { $('.formElem .input').each(function() { $(this).children(".inputField").on("keyup", function ...

All hyperlinks are displayed as a single entity

I have 5 image links displayed side by side, but when I hover over them, they act as one collective link. Can someone please assist me with resolving this issue? After updating my code to include the entire div, it seems that there is something within the ...

The AngularJS application appears to have trouble accessing a local text file, even when it is deployed on

Within the same directory of my application deployed on IIS, there are 4 files: home.html Angular.js data.txt web.config (Automatically generated by IIS for default document) I am looking to display the content of 'data.txt' on 'home.html ...

Using regular expressions in Javascript to extract decimal numbers from a string for mathematical operations

I'm currently working on a Vue method where I extract information from a WordPress database. The data retrieved sometimes contains unnecessary text that I want to filter out. Using the prodInfo variable, the input data looks something like this: 2,5k ...

Improper Alignment of Bootstrap 4 Navbar Link: Troubleshooting Required

Take a look at the image for the issue: https://i.stack.imgur.com/u9aCy.png As shown in the image, the NavBar links are stacked instead of being displayed in one row. This is the HTML code I have used: <!doctype html> <html> <head> ...

Exploring the correlation between aggregation operations in MongoDB and MapReduce techniques

For days, I've been grappling with translating this specific query into MapReduce. My task is to determine the number of different cars that have covered a distance of "N" kilometers. The Query: db.adsb.group({ "key": { "KM": true }, ...

Tips for creating shaped images using clip-path

Can an image be clipped to match a specific shape using the clip-path CSS property? Original Image https://i.stack.imgur.com/rYeuk.jpg Desired Result with CSS https://i.stack.imgur.com/6FYfn.png ...

In Javascript, swap out a particular colored region in an image with a different image

I've been scouring the internet in search of a solution to my problem, but it seems like I might not be looking in the right places. Imagine this image below, how can I replace the blue area with an image uploaded by the user? I'm struggling to ...

The login form using Ajax and PHP is experiencing an issue where the response is consistently empty

I am having issues with my login form. Every time I enter a login and password, whether correct or incorrect, the JavaScript script returns an error and the "response" is empty when I try to print it. Can anyone offer assistance? $(document).ready(funct ...

jQuery issue: Inability of "Read More" span to reappear after clicking "Read Less"

Currently in the process of creating a portfolio website that showcases project descriptions with an excerpt, revealing full details upon clicking "Read More." My experience with jQuery is limited, but I managed to implement functionality where clicking "R ...

What is the method for including a placeholder with sequential numbering?

When I click on the "Add String" button, it clones the first table row with an input in the table and adds it to the table. I also need to add a +1 number in the placeholder of the copied element. How can I determine the last placeholder before copying and ...