Continue moving the division to the left

Below is the HTML structure that I am working with:

<div class="container">
  <div class="menu"></div>
</div>

Accompanied by jQuery:

$(document).ready(function() {

    $(".container").click(function() {
        $(".menu").css("left", "+=50px");
    });
});

And here is the CSS styling:

.container{
  position:relative;
  height:50px;
  width:auto;
  background:lightgray;
}

.menu{
  position:absolute;
  width:50px;
  height:35px;
  background-color:green;
  bottom:0px;
  left:0px;
  transition: left 1s;
}

You can view a demonstration of this code in action on JSFiddle.

The objective here is to move the .menu div to the left, as if being pulled by gravity. However, when clicked, it should move to the right. The desired effect is similar to the movement in the game Flappy Bird, but horizontal instead of vertical.

Answer №1

To reset the position back to the right, you can utilize the animate callback function.

$(".container").click(function() {
    $(".menu").stop().animate({left : "+=50px"},1000,function(){
        $(".menu").animate({ left : "0" },1000);
    });
});

Make sure to eliminate the transition property from your css.

Here is a demo of this solution: http://jsfiddle.net/85QCk/14/

Answer №2

Do you need help with this solution? To prevent the click event in the children of the container or the menu from triggering the parent, you must use e.stopPropagation();.

var menuOpen = false;

$(document).ready(function () {

    $(".container").click(function () {
        if (!menuOpen) {
            $(".menu").css("left", "+=50px");
            menuOpen = true;
        }
    });

    $(".menu").click(function (e) {
        e.stopPropagation();

        if (menuOpen) {
            $(".menu").css("left", "-=50px");
            menuOpen = false;
        }
    });
});

You can view a demo of this code in action on JSFiddle.

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

Hello there, I'm currently attempting to use the Material UI IconButton along with an input ref to upload an image, but I'm experiencing some

click to view the UI image here Hi! I'm attempting to include a Material UI button to upload an image, but it's not functioning as expected. Below is the code snippet and a link to the sandbox where I'm trying to achieve functionality simil ...

Issue with HighCharts Series Data Points Not Being Added

I am currently facing a major challenge in dynamically changing data for highcharts based on date. To provide context for my project, it involves logging system data with timestamps. I have implemented a date and time picker to specify the start and end da ...

Generating a download link with an expiration feature in node.js or express for both frontend and backend operations

Hello everyone, I'm a beginner here and have recently started working with node.js and express.js. I am looking to implement a download link on my website that expires after a certain time, but I've been struggling with the code for about a week ...

Unique Version: When utilizing custom window.FileReader methods, Angular zone execution is bypass

Upon transitioning an Ionic/Angular project from Cordova to Capacitor, I found it necessary to override the default window.FileReader in order to successfully execute the onload() method. Here is the approach I took (https://github.com/ionic-team/capacitor ...

What could be causing the function to not work properly within the React component?

Having trouble with a React component utilizing speech recognition for converting speech to text. Initialized the recognition functions within the component but encountering errors. Need assistance in troubleshooting this issue. const speechRecognition = w ...

Tips for dividing the AJAX response HTML using jQuery

I have a piece of code that is functioning properly. However, I am having trouble splitting the HTML response using AJAX jQuery. I need to split it into #div1 and #div2 by using "|". Could you please provide a complete code example and explanation, as I am ...

Combine two JSON objects which share a common attribute

I am currently working with two JSON feeds. One feed contains the basic information about a course, while the other contains more administrative details. Here is an example to illustrate what I mean. FIRST {"courses": {"course":{"id":"4","title":"Usi ...

Is there a way for me to retrieve the widths of all child elements within an HTML document?

I have been working on a JavaScript (jQuery) function to calculate the maximum width of all child and children's-child elements within a specific div element. Here is the code I have written so far: function setBodyMinWidth(name){ var max = 0; $(nam ...

An issue occurred while attempting to access the Stripe checkout page

While working on my e-commerce website using next.js and stripe checkout, I encountered an error during the checkout process. I am utilizing the use-shopping-cart package, but suspect it may be causing the following response error from stripe-checkout: & ...

The element 'Listitem' is unrecognized and cannot be found in the current context

I am currently working on an ASPX project in Visual Studio and have encountered an issue with a Drop Down field. It appears that the List Items are being ignored in my code. Can anyone advise me on what might be missing? <form runat="server"> &l ...

Sinon.js: How to create a mock for an object initialized with the new keyword

Here is the code that I am working with: var async = require('async'), util = require('util'); var Parse = require('parse/node'); function signup(userInfo, callback) { var username = userInfo.username, email ...

Can the Automator tool capture a specific section of a website in a screenshot?

Is it possible to use automator to navigate through a store's website category and open each product in a new tab? Alternatively, is there a software that can extract all the product URLs from a specific website? For example, this website has 14 prod ...

Grid sidebar using tailwindcss

I just started using Tailwind CSS, but I'm having trouble understanding how the grid system works! I want my layout to look like this: https://i.sstatic.net/hlHmy.png Here is what I tried, but it's not working as expected: <div class=" ...

"Unexpected issue with jQuery AJAX Post functionality on a shared server causing data not to

I'm currently facing an issue with my code that allows comments to be added to a php page without needing to refresh the page. While everything is functioning properly on localhost, I encountered a problem when testing it on my justhost account. The ...

How can you ensure an image is centered properly when padding is applied?

Is there a way to center an image without affecting padding around it? Check out this sandbox for reference HTML <div class="imageParent"> <figure id='img-div'> <img class='image card' ...

Having trouble with protractor's sendKeys function when trying to interact with md-contact-chips

Does anyone know how to set a value using sendKeys in Protractor for md-contact-chips? I attempted to use element(by.model('skills')).sendKeys('Java'); but it doesn't seem to be working. Any suggestions on how to approach this in ...

Display a "busy" indicator using Ajax, specifically for requests that take longer to process

Is there a way to delay the appearance of the busy indicator until a certain amount of time has passed? The code I'm using for the busy indicator is quite simple: jQuery.ajaxSetup( { beforeSend:function () { jQuery( "#busy-indicator" ...

Form that adjusts input fields according to the selected input value

I am in need of creating a dynamic web form where users can select a category and based on their selection, new input fields will appear. I believe this involves using JavaScript, but my searches on GitHub and Stack Overflow have not yielded a suitable sol ...

Unable to append dynamic data to elements using jQuery

I'm facing an issue with binding events to select boxes that are added dynamically to the page through Ajax or DOM after the initial loop. Below is the view part of my code: <div class="row" id="dsp"> <div class="col-md-2"> & ...

Node.js is unable to handle the contents of an SNS event message

I am encountering an issue while attempting to extract content from a Message in an SNS event within a Node.js Lambda project Below is the code snippet for processing the message: exports.handler = (event, context, callback) => { var message = event. ...