Create movement for an object when it is clicked and when the mouse cursor enters or leaves the object, as well as other

I am struggling with the layout of my webpage, which consists of 3 main div elements.

https://jsfiddle.net/wpztofb7/

    <body>
  <div id="topBox" class="otherBox">
    TEST TOP
  </div>

  <div id="middleBox" class="middleBox">
    SECTION
  </div>
  <div id="tab" class="tab"><span>New Comment</span></div>

  <div id="bottomBox" class="otherBox">
    TEST BOTTOM
  </div>
</body>

.otherBox {
  border: 2px solid #73AD21;
  width: 100%;
  height: 150px;
}

.middleBox {
  border: 2px solid #73AD21;
  width: 100%;
  height: 25px;
}

.tab {
  border-left: 2px solid #73AD21;
  border-right: 2px solid #73AD21;
  border-bottom: 2px solid #73AD21;
  border-bottom-left-radius: 0.5em;
  border-bottom-right-radius: 0.5em;
  height: 15px;
  width: 120px;
  margin-bottom: 20px;
}

$(document).ready(function() {

  click = 0;

  $("#tab, #middleBox").click(function() {
    if (click === 0) {
      click = 1;
      $("#middleBox").animate({
        height: '400px'
      }, 500);
    } else {
      click = 0;
      $("#middleBox").animate({
        height: '30px'
      }, 850);
    };
  });

  if (click === 0) {
    //Do wiggle
    $("#tab, #middleBox").mouseenter(function() {
      $("#middleBox").animate({
        height: "40px"
      }, 800);
    });
    $("#middleBox").mouseleave(function() {
      $("#middleBox").animate({
        height: "30px"
      }, 800);
    });
  }
});

The configuration of the middle div and tab elements is causing a problem for me. The objective is to have the middle div expand when clicked, while also providing a "wiggle" animation on mouse enter/leave to draw user attention.

Unfortunately, there is an issue when moving the mouse inside the expanded div after clicking it. Any movement collapses the div. I seek guidance on how to resolve this problem since I'm relatively new to web development.

I attempted using the .off("mouseenter mouseleave") method on the elements but to no avail.

Ultimately, I want users to be able to trigger the "wiggle" animation by hovering over the tab or div. If they click in the tab or div, the expansion animation should occur and persist until another click, even during mouse movement.

Answer №1

After realizing my premature question, I took some time to persevere and successfully resolve the issue.

I've now mastered the proper use of .off()

If anyone knows of a more effective way to accomplish this task, please share it. Or if there are any improvements you can suggest for my code...


<script>
    $(document).ready(function () {

        click = 0;

        $("#tab, #middleBox").click(function () {
            if (click === 0) {
                click = 1;
                $("#middleBox").animate({ height: '400px' }, 500);
                $("#middleBox, #tab").off("mouseenter mouseleave");
            }
            else {
                click = 0;
                $("#middleBox").animate({ height: '30px' }, 850);
            };
        });

        if (click === 0) {
            //Do wiggle
            $("#tab, #middleBox").mouseenter(function () {
                $("#middleBox").animate({ height: "40px" }, 800);
            });
            $("#middleBox").mouseleave(function () {
                $("#middleBox").animate({ height: "30px" }, 800);
            });
        }
    }); </script>

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

Implementing custom click event for selecting checkboxes in Material-UI table rows

I have been working with the material-ui data table to implement sorting functionality. One feature I am trying to add is a checkbox on each row. The challenge I am facing is that when clicking on the checkbox, it also triggers the row link, which is not t ...

Error caused by MongoClient TypeError

As a newcomer to NodeJS and someone exploring Dependency Injection for the first time, I encountered an error that led me to seek help. Before asking my question, I reviewed some similar threads on Stack Overflow: [1][2] Upon running my main code, I recei ...

Creating a personalized confirmation function using JavaScript

Here is a function I've created to generate a confirmation box. The issue I'm facing is that the function doesn't wait for the user to click before returning true/false based on the onclick events. So, how can I solve this? function Confi ...

Tips on showcasing information with ng for in Ionic 2

https://i.sstatic.net/MKIcJ.jpgI am currently working on the thank you page for my project. When the success call is made, it will display both ORDER DETAILS and Delivery Details. However, in the event of a failure call, only the ORDER DETAILS are displaye ...

PHP/Laravel Method for Tracking Date Changes Every 24 Hours

I have a task where I need to generate a report that fetches data totals from a MySQL database and displays them in table rows. Each row should represent the totals for a 24-hour time frame (from 9 pm one day to 9 pm the next day). The timestamps in my dat ...

Retrieve metadata using the jQuery $.post() method

I've been working on extracting meta tag information using the following code snippet. $(pageDetailsSecond).('head').find('meta[name="description"]').attr("content"); I also attempted this approach: $(pageDetailsSecond).('m ...

Encountering the error "Vue 2.0 encounters an undefined push during router.push

I'm currently working on a project that involves implementing a function to route the list of subMenus displayed by the mainMenu. Each subMenu is identified by its specific name, which I would like to append and use as the route path that I am aiming ...

A beginner's guide to handling multiple events with @click in Vue.js

In my project, I am developing a task manager application utilizing Vue.js, Vuetify, and Firebase. When the user clicks on "Add new note," a Vuetify dialog box pops up, prompting them to input data. Once they click save, the dialog box closes, and the inpu ...

Problem with Bootstrap 3 navbar on mobile devices - not tappable or responsive

After years of using Bootstrap, I've come across a new issue with my implementation of a Bootstrap 3 Nav. While testing on a desktop browser with device emulation, the nav collapses and functions properly. However, when tapping on the header on an ac ...

Mixing up letters using a shuffle function

Seeking assistance as a newcomer here. I have a shuffle function triggered by pressing the reset button with class="reset" on the img tag. How can I make it load shuffled from the start? So that when loading this page, the letters do not appear in alphabet ...

Validation of forms in AngularJS/HTML5 that are nested within one another

Just starting out with AngularJS and experiencing issues with HTML5 nested form validation. I currently have 2 forms; mainFrm (parent form) and stateFrm (a child form). I am struggling to validate each form within its own scope. <form id="mainFrm" ng- ...

Prevent user input when an alert window is open

Users keep pressing the enter key multiple times when an alert box pops up, causing them to accidentally dismiss the message by hitting 'ok'. Is there a simple way to prevent key presses on alert windows and only allow mouse input? ...

Creating a robust system for resetting passwords using the powerful combination of Django Rest Framework, Serializers, and Vue.js

I am currently in the process of implementing a reset password feature, but I am struggling to find comprehensive tutorials on how to do so using DRF and Vue.js. My approach involves utilizing serializers for data passing without incorporating HTML views. ...

Combining divs with identical values in Angular

I am working on creating my very own custom Calendar. Take a look at the full example of my component here I need help figuring out how to combine week divs that share the same value {{day.weekNumber}} so that there is only one div for each shared value, ...

Using npm as a build tool to merge files together

Upon discovering the flexibility of using npm as a task runner instead of gulp or grunt, I have successfully implemented various tasks such as linting, stylus processing, jade compilation, uglifying, and even watching files. However, I am facing difficulti ...

Accessing video durations in Angular 2

Can anyone help me with retrieving the video duration from a list of videos displayed in a table? I attempted to access it using @ViewChildren and succeeded until encountering one obstacle. Although I was able to obtain the query list, when attempting to a ...

Getting the width of an element using a React ref is a helpful technique that allows you

I am struggling to retrieve the width of a select field using React Ref. https://i.sstatic.net/JJmqF.png this.selectRef.current is returning an object, but I can't seem to find a way to access the width of the element. <Select ful ...

Need to split your screen into two horizontal DIVs, each with their own scrollbars? Check out this example link for a demonstration!

Hey there! I have a question about creating something similar to this cool website: I'm working with Wordpress and using a child theme for my website. I want to include a fixed header as well. Currently, I've managed to give each div its own sc ...

Unexpected Facts about Refresh Flicker (Tumblr)

I've been attempting to implement a 'random background on refresh' feature for my Tumblr theme. As some may not be aware, Tumblr themes are comprised of HTML alongside embedded CSS and JavaScript. At the moment, I have been utilizing this p ...

What is the proper placement for index.html <head/> class helper functions within a ReactJS Component?

My custom helper functions are stored in a JavaScript file called classie.js: ( function( window ) { 'use strict'; function classReg( className ) { return new RegExp("(^|\\s+)" + className + "(\\s+|$)"); } var hasClass, ...