A guide on effortlessly reducing a flying draggable popup using jQuery

I have created a pop-up window that appears on the viewport as you scroll, and it can also be dragged around.

You can see the code snippet below:

var jq = jQuery.noConflict();
var $el = jq(".Box");
var $btnmini = jq(".sessionCD #btnmin");
var isMini = false; // Panel is minimized.

$el.draggable({
  cursor: "grab",
  containment: 'window',
  drag: function(event,
    ui) {
    jq(this).css('cursor',
      'grabbing');
  },
  stop: function(event,
    ui) {
    jq(this).css('cursor',
      'grab');
  },

});

jq("#btnmin").click(function () {
if (isMini) {
    jq('.Box #content #msg').show();
        jq('.Box #content #note').show();
isMini = false;
} else {
        jq('.Box #content #msg').hide();
        jq('.Box #content #note').hide();
isMini = true;
}
});


jq('#btn').click(function() {
  $el.show();
});

jq('#btntxt').click(function() {
  var i = 0;
  for (i = 0; i < 50; i++) {
    jq('#txt').append('<p>THIS IS SPARTA!!!</p>');
  }
});

jq(window).scroll(function() {
  $el.stop().animate({
    "top": (jq(window).scrollTop() + 10) + "px"
  }, "fast");
});
.Box {
  cursor: "grab";
  position: absolute;
  left: 2px;
  top: 2px;
  width: 300px;
  height: auto;
  display: none;
  margin: 1px;
  padding: 2px;
  z-index: 100000;
  border-width: 1px;
  border-style: solid;
  border-color: #AAA #CECECE #E6E6E6;
  background: none repeat scroll 0% 0% #F1F1F1;
  box-shadow: 0px 1px 1px 0px #CECECE inset;
  border-radius: 8px;
  text-align: center;
}
.Box #header {
  height: 20px;
  background: none repeat scroll 0% 0% #D4E8CD;
  border-bottom: 1px solid #83A478;
  color: #416833;
}
.Box #header #title {
  text-align: center;
  font-weight: bold;
}
.minibtn {
  margin-right: 2px;
}
.minibtn:hover {
  cursor: pointer;
  font-weight: bold;
}
.sessionCD #time {
  font-family: Georgia, Times, Times New Roman, serif;
  font-size: 19px;
  font-weight: bold;
  text-align: center;
  margin-left: 5px;
  margin-right: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.min.js"></script>
<div class="Box" title="">
  <div id="header">
    <table cellpadding="0px" cellspacing="0px" border="0px" width="100%">
      <thead>
        <col width="20px" />
        <col width="88%" />
        <col width="10%" />
      </thead>
      <tbody>
        <tr>
          <td id="piccha">
            <img src="dragger.png" />
          </td>
          <td id="title">
            User!
          </td>
          <td id="btnmin" class="minibtn">
            Mini
          </td>
        </tr>
      </tbody>
    </table>
  </div>
  <div id="content">
    <p id="msg">text text text
      <br/>text text:</p>
    <span id="time">Super Numbers 12312</span>
    <br/>
    <p id="note">The note</p>
  </div>
</div>
<button id="btn">show box</button>
<button id="btntxt">add 30 text</button>

<div id="txt">

</div>

The functionality is also available on JSfiddle.net

How can I achieve smooth minimization of this window using the "Mini" button? By "minimize", I mean smoothly hiding the #msg and #note elements, followed by a smooth resizing of the window. The same applies to "Maximize", but in reverse for showing.

Answer №1

To create a smooth showing and hiding effect for div elements, you can utilize the jQuery methods slideUp and slideDown.

With the .slideUp() method, the height of the targeted elements is animated, causing them to slide up and appear hidden. Once the height reaches 0 (or the CSS min-height property if set), the display style property is changed to none, removing the element from the layout.

Conversely, the .slideDown() method animates the height in reverse, revealing the hidden items by sliding them down.

For a demonstration, check out this Demo.

You have control over the speed of the animation by passing parameters like "slow," "fast," or setting a specific duration in milliseconds after the click event triggers the display of the div.

JQuery Usage Example:

jq("#btnmin").click(function () {
    if (isMini) {
        jq('.Box #content #msg').slideDown();
        jq('.Box #content #note').slideDown();
        isMini = false;
    } else {
        jq('.Box #content #msg').slideUp();
        jq('.Box #content #note').slideUp();
        isMini = true;
    }
});

Answer №2

Give this a shot, it should work like a charm:

jq("#btnmin").click(function () {
    if (isMini) {
        jq('.Box #content #msg').show(500);
        jq('.Box #content #note').show(500);
        isMini = false;
    } else {
        jq('.Box #content #msg').hide(500);
        jq('.Box #content #note').hide(500);
        isMini = true;
    }
});

If you want to speed things up, lower the value. For example: 500 -> 300.

http://jsfiddle.net/johndoe/7x3nmtq2/

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

How to Export Data from MySQL Database to a .TXT File Using PHP

I am working on a project that involves using an HTML5 form to submit data, which then triggers a PHP script to connect to a MySQL database. The script inserts the data into a table and writes all the table lines to a .txt file. However, I am encountering ...

Encountering an NPM ELIFECYCLE error when attempting to start the node server with the

After following the instructions on deploying test-bot on IBM Watson from this link https://github.com/eciggaar/text-bot, I encountered errors while attempting to deploy the code locally using CLI foundry. My setup includes Node.js version 6.10.3 and npm ...

The text is obscured by the overlapping div within the parent div

Here is the code snippet I am working with: .pa { width: 400px; background: #008000; position: relative; } .icon-wrap { height: 100%; float: right; width: 30px; background: yellow; padding: 5px; position: absolute; top: 0px; right: ...

When converting text to html, replace words with hyperlinks. When multiple words can be linked, prioritize the longer

Struggling with a task at hand: My goal is to enhance a string that contains HTML by linking certain words to corresponding "read more" pages. The challenge arises when these search terms overlap, resulting in nested links or terms not being replaced prop ...

What is the best way to create space between two flex elements that doesn't follow a

Dealing with a challenge in Flexbox. I am trying to establish a specific distance between two flexible elements. I understand that I should use justify-content, but unfortunately cannot set an exact distance using it. Avoiding the use of margins or othe ...

When inserting <img>, unwanted gaps appear between div elements

When I have <img> in the child divs, there is some space between them and a blue stripe appears under the image. How do I make them stack perfectly without any gaps? I am new to HTML and CSS and trying to learn for marketing purposes. Any help or ad ...

Problem with one image not displaying in jQuery slider on Internet Explorer

I have a jquery template that includes 3 slides, and I recently replaced one of the image templates with an image of the same size. This change works perfectly on Chrome but not on Internet Explorer. Below is the code snippet: <script src="https://ajax ...

JavaScript slice() method displaying the wrong number of cards when clicked

In my code, I have a section called .registryShowcase. This section is designed to display logos and initially shows 8 of them. Upon clicking a button, it should load the next set of 8 logos until there are no more left to load (at which point the button ...

the replication of a column within one single column

I have discovered that in order to arrange items in a column using bootstrap-5, the code should look like this: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a3c1ccccd7d0d7d1c2d ...

Issues arise when using the "placeholder" attribute within a <p:inputText tag while the inputText is devoid of any value

A current project involves implementing JSF Mojarra 2.3.9.SP02 alongside PrimeFaces 7.0 on Wildfly 17 using the Sapphire template provided by PrimeFaces. However, I've encountered a significant issue with a specific <p:inputText element within my f ...

Blunder! Error code EINVALIDTAGNAME encountered while trying to install a package

I'm encountering an issue while trying to add a new package to my React application. The error I'm receiving is: $ npm install xlsx npm ERR! code EINVALIDTAGNAME npm ERR! Invalid tag name "react-scripts start": Tags may not have any characters th ...

React Autocomplete Input Not Functioning as Expected

I've been working on a form that needs autocompletion for emails, allowing users to select from browser suggestions. In HTML, I would typically use autocomplete='on' in the inputs or forms and it worked perfectly. However, when trying to imp ...

Employing the include functionality within PHP

Currently, I am in the process of developing an eCommerce website and have my own host/domain. To ensure security, I understand that it is advisable to store PHP files in a location separate from public_html. The index.html file is located in /home/user/pu ...

Is there a way for me to include .js or .cpp prior to using the Vim shortcut key gf?

When using a programming language like nodejs, the require function does not require the addition of the .js extension. For example: var Assert = require('./app_base'); However, in vim, when using gf, it cannot find app_base without the .js ext ...

Troubleshooting a Mystery JavaScript Reference Error in a Date and Time Application

Currently, I am working on developing a JavaScript file to create variables for dates and times using the shortcut method. Although everything seems to be in order from what I can see, it does not display correctly, and when checked with Google Chrome&apos ...

Enhance Your jQuery Skills by Adding Custom Directories to Anchor Links

Can jQuery be used to add a custom folder name in front of all links on a webpage? For example, if the website has these links: <a href="/user/login">Login</a> <a href="/user/register">Register</a> <a href="/user/forum">Foru ...

Why isn't my textarea in jQUERY updating as I type?

On my website, I have a comment script that is not functioning correctly in some parts of the jQuery/JavaScript code. Instead of posting an edited comment to PHP, I created a notification window to test if the value passed through is actually changing. W ...

Retrieve the name of the selected item in the antd dropdown component

I am facing an issue where I cannot retrieve the name of the select component in Ant Design to use the handleInputChange function on its onChange event. As a workaround, I decided to update the state directly in the onChange event by adding it to an obje ...

Retrieve the total number of users in a Node.js application by querying the MongoDB database

Within my node application, I have the ability to register users and showcase their profiles. My aim is to exhibit the total number of users in the database on the homepage exclusively for the logged-in user. The pivotal route in my route.js file reads as ...

Certain Bootstrap 5 icons are failing to appear on the screen

I'm encountering an issue with Bootstrap 5 where certain icons are not displaying correctly. The bi-search icon shows up perfectly, but the bi-send icon is not being shown at all. I would usually include this in a code snippet, but it seems that featu ...