Building a draggable modal that dynamically adjusts the window's width and height using HTML, CSS, JS (with JQuery)

I'm currently working on implementing a hidden modal in my application that only appears when a specific button is clicked.

Here is the code that I have used to achieve this functionality:

Modal HTML:

<div style="width:500px; display:none;" id="chatModal" class="panel panel-default chatModal">
    <div class="panel-heading">
        <h6 class="panel-title">Zworld Chat</h6>
        <div class="heading-elements">
            <ul class="icons-list">
                <li><a data-action="collapse"></a></li>
                <li><a data-action="reload"></a></li>
            </ul>
        </div>
    </div>

    <div style="overflow-y: scroll; height: 200px;" class="panel-body" id="chat-text">
        <div>Chat messages:</div>
    </div>
</div>

Modal show / draggable JS:

$('#chatBox').click(function()
{
    if ($('#chatModal').css('display') == 'none'){
        $('#chatModal').show();
        $('#chatModal').draggable();
    }
    else
    {
        $('#chatModal').hide();
    }   
});

However, I have encountered an issue where dragging the modal to the right or bottom side of the screen causes the layout to extend itself.

For a live example (Note: authentication credentials are stored):

Here is a screenshot example: https://gyazo.com/0b55ab2f8f71ef7526d0fe2474883dde

Question:

Is there a way to make the draggable function not extend the layout when the modal is dragged?


If you'd like to see the live preview (Note: not responsive yet), follow these steps:

  1. Open the live preview link.
  2. Click on the login button.
  3. Click on the small "message bubble" icon located on the right side of the input.
  4. Try dragging the modal to the right or bottom of the site.

Answer №1

To ensure your element covers the entire window, simply apply an overflow hidden to it.

#ui { overflow: hidden; }

Answer №2

Ensure the element stays within the boundaries of its parent window by using containment.

$("#chatModal").draggable({ containment: "parent" });

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

``Why Ionic 3 Popover Sizes Should Adapt to Different Screen

Currently in my Ionic 3 project, I am utilizing a popover with a set height using the following code snippet: editOpty(rw){ let popover = this.editOptyPopup.create(EditOptyPopoverComponent, rw, { cssClass: 'edit-opty-popover'}); popover ...

Eliminate the display of Last Modified date and file Size in mod_autoindex

Recently, while developing a file storage webpage for my website, I successfully implemented mod_autoindex for my Apache server. However, I now face the challenge of removing the Last Modified and Size fields from the table. I am hopeful that there is a w ...

How do I initiate PUT and DELETE requests from my HTML code?

I am currently in the process of developing a web application that will manage items within a list. Previously, I used buttons with event listeners and JavaScript functions to handle editing and deleting items. However, I am now transitioning towards build ...

What is the best way to show a background color on a heading?

I am new to website design and I have successfully created a slideshow. However, I am facing an issue with applying a background color to the heading so that it stands out over the image. Despite setting the background color in the CSS, it is not displayin ...

Developing an if-else statement to showcase a different div depending on the URL

Having trouble with an if/else statement to display one div or another based on the URL: No matter what I try, only "Div 1" keeps showing. Here's my code snippet: <script> if (window.location.href.indexOf("pink") > -1) { document.getElemen ...

Obtain a specific amount of values from arrayA[5000] and transfer them into arrayB[] grouped together

My array looks like this: arrayA = [1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,1,2]; I want to organize the numbers into groups like this: arrayB = [1234,5678,9123,4567,8912]; Essentially, I need to take the values from arrayA and create new numbers by group ...

Incorporating the "+ " icon in Vuejs Dropzone to indicate the presence of existing images

Looking to enhance my Vue-dropzone file uploader component by adding an icon or button labeled "Add more images" when there are already images present in the dropzone. This will help users understand that they can upload multiple photos. Any suggestions on ...

Why is it necessary to use quotations and plus symbols when retrieving values of objects from JSON?

Initially, when attempting to call the PunkAPI for the first time, I was trying to include images (urls are an object returned in the JSON) by append("<img src='beer[i].image_url'/>"); Unfortunately, this did not work because (according t ...

Winning awards through the evaluation of possibilities

I became intrigued by the idea of using a chance algorithm to simulate spinning a wheel, so I decided to create some code. I set up an object that listed each prize along with its probability: const chances = { "Apple" : 22.45, & ...

Adding elements to the <Head> section in Next.js

I am facing an issue where I need to change the page title dynamically based on the route of the web pages. I have been assigned the task of creating and importing a title component into the layout file, but despite my efforts, nothing seems to change. con ...

Why isn't the background-image displaying with the use of the url() function?

I've been attempting to set an image as the background using background-img:url(imageLing), but it's not working properly. When I inspect the element, it shows me background-image: url(assets/backgrounds/5.jpg);. What could be causing this issue? ...

The z-index property in CSS is ineffective on Firefox browsers

My current project involves implementing a dual slider input range. The HTML code I have written looks like this: .acCalcInputRange input[type="range"] { pointer-events: none; position: absolute; bottom: 0.6rem; border: none; outline: none; ...

Ways to eliminate spacing around lists?

I am trying to remove the margin on the left side of a list that contains 3 items. Here is my code: ul li { display: inline; list-style: none; } <ul> <li>I have margin space on my left side</li> <li>List 2</li> & ...

Looking to clean up unnecessary <b></b> tags from my Wordpress website - how can I do this?

One of my sites is located at . When inspecting the source code through the browser, I noticed the presence of empty tags. Does anyone know why these empty tags are being generated and how they can be removed? I have thoroughly searched through all files ...

What steps should I take to set up search paths for node modules in Code Runner within Visual Studio Code?

Just recently, I embarked on a Javascript course and successfully configured my Visual Studio Code to run JavaScript. Check out the code snippet that I came up with: const prompt = require('prompt-sync')(); var fname = prompt("First name please : ...

What is the Significance of the Height in This Sample Menu?

I came across this interesting menu sample on W3Schools while working on my MVC layout page. My menu was looking messy, and I really liked the clean look of this one. When I pasted it into my website, it worked perfectly, but I'm puzzled about how it& ...

In Reactjs, it is important that each child in a list is assigned a distinct "key" prop

I am currently working on fetching data in Nextjs/Reactjs after clicking a button, but I am encountering the following error in my console: Each child in a list should have a unique "key" prop Below is my current code where data is displayed wit ...

Exploring the Power of Multiple FindOne Queries in MongoDB

I have been working on expanding the data fields returned by our API. Currently, the API retrieves student information using the find method, and also includes some project details by retrieving the student info and using findOne to obtain information abou ...

Achieving a child div to occupy the entire available space when the parent is specified as `mx-auto` can be accomplished using Vue and Tailwind CSS

Sorry if this seems like a basic question, I'm still getting the hang of Vue and tailwind. In my parent template, it looks like this: <template> <div class="mx-auto mt-4 max-w-3xl"> <slot></slot> </div> ...

Determine the presence of a JSON Value/Array in a web application using JavaScript and visualize the information in a dynamic

Utilizing the Ticketmaster API has provided me with a sample dataset from their platform, Data 1 - Including information on "presales." "sales": { "public": { "startDateTime": "2019-11 ...