Make an element in jQuery draggable but always within the viewport

My issue is that when I resize the window, the element stays in its position until I start dragging it. Once I drag it and then resize the window again, it doesn't stay within its container.

To better illustrate my problem, you can view a demo on Fiddle. Here is the code snippet:

$(document).ready(function() {
  $(".MENU").draggable({
    containment: ".MENU-CONTAINER",
    snap: ".MENU-CONTAINER",
    snapMode: "inner",
    snapTolerance: "16",
    scroll: false
  });
});
* {
  box-sizing: border-box;
}

body {
  background: hsla(0, 0%, 100%, 1.00);
}

.MAIN {
  padding-top: 32px;
  padding-left: 64px;
  width: 100%;
  height: 100%;
}

.MENU-CONTAINER {
  position: fixed;
  width: calc(100% - 64px);
  height: calc(100vh - 64px);
  background-color: hsla(120, 100%, 25%, 0.3);
}

.MENU {
  position: fixed;
  bottom: 32px;
  right: 32px;
  width: 128px;
  height: 64px;
  background: hsla(0, 0%, 0%, 1.00);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>

<div class="MAIN">
  <div class="MENU-CONTAINER">
    <div class="MENU"></div>
  </div>
</div>

My question is, how can I ensure that this element remains contained within its container even after resizing the viewport?

Answer №1

Implement reloading functionality after resizing the window

$(window).resize(function(){
    location.reload();
});

Demo:

$(".ELEMENT").draggable({
  containment: ".CONTAINER",
  snap: ".CONTAINER",
  snapMode: "inner",
  snapTolerance: "16",
  scroll: false
});

$(window).resize(function(){
    location.reload();
});
* {box-sizing: border-box;}

body {background: hsla(0, 0%, 100%, 1.00);}

.MAIN {
  padding-top: 32px;
  padding-left: 32px;
  width: 100%;
  height: 100%;
}

.CONTAINER {
  position: fixed;
  width: calc(100% - 64px);
  height: calc(100vh - 64px);
  border: 1px solid blue;
}

.ELEMENT {
  position: fixed;
  bottom: 32px;
  right: 32px;
  width: 128px;
  height: 64px;
  background: hsla(0, 0%, 0%, 1.00);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<div class="MAIN">
  <div class="CONTAINER">
    <div class="ELEMENT"></div>
  </div>
</div>

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

Empty input fields in Javascript calculations will result in a NaN output

I need to perform a calculation using values entered into form fields and JavaScript. The formula I'll be using is as follows: totalEarnings = income1 + income2 * 0.7 + income3 / 48 + (income4 * 0.7) / 48; The variables income1, income2, income3, an ...

What is the best way to configure custom CSS styles in a React project?

I've been trying to position my Grid component from Material-UI, but so far, I haven't had any luck. What could be the issue here? class Scene extends React.Component { render() { const mystyle = { backgroundColor: "DodgerBlue", ...

Storing HTML in a Database

I've encountered an issue while attempting to parse through an HTML file that consists solely of tags. I wrote a function to extract the content between these tags, and while I'm able to display it on the page without any problems, inserting th ...

Ensuring that your content spans the entire viewport is crucial for optimizing

https://gyazo.com/1440b13007c6e011ec46218ceecabb6a Upon examining the screenshot, it is evident that there is a white border surrounding the main content of the page. Despite my attempts to adjust everything to 100% width, I have not been success ...

Adding dynamic images to Bootstrap Popover on the fly

Does anyone have suggestions on how to effectively retrieve the image from the data-img attribute in order to use it for a popover displaying product images? Thank you! <a href='#' class='toggle-popover' title='Hello World&apos ...

Javascript/Jquery - Eliminating line breaks when transferring text to a textarea by copying and pasting

Is there a method to paste text into a textarea that will automatically remove any line breaks or whitespaces? For instance, if I copy the following text and paste it into the textarea: abcdef, ghijkl, mnopqrs I would like it to appear in the textarea as ...

The pagination in React using React Query will only trigger a re-render when the window is in

Currently, I am utilizing React-Query with React and have encountered an issue with pagination. The component only renders when the window gains focus. This behavior is demonstrated in the video link below, The video showcases how the component re-renders ...

Tips for updating an anchor link within a Textarea using jQuery

I have a simple inquiry about jQuery. I am interested in changing the "a" tag within a Textarea. I understand that "a" tags do not work within a textarea, which makes it impossible to target them with jQuery selectors. For example: <textarea><a ...

Ways to evenly distribute divs into rows

Greetings if this question has already been inquired about, but my search turned up nothing quite like it. My current setup is as follows: .main{ display: inline-flex; flex-direction: row; } <div class = "main"> <div class="sub-div ...

Necessary within a JavaScript Class

As a newcomer to using classes in JavaScript, I've been exploring the best practices and wondering about how 'requires' work when used within a class. For example, let's say I want to craft an IoT Connection class for connecting to the ...

The success method in the observable is failing to trigger

Could someone explain why the () success method is not triggering? It seems to be working fine when using forkjoin(). Shouldn't the success method fire every time, similar to a final method in a try-catch block? Note: Inline comments should also be c ...

Tips for obtaining the correct Javascript output for the number pyramid

I'm currently in the process of creating a half pyramid of numbers, but I'm facing an issue with getting the output to show the total for each line while maintaining everything except the * sign between the numbers. If anyone is able to offer som ...

The Material-UI dialog is experiencing a flickering issue when incorporating Redux to manage the open

I am currently facing an issue with moving the open state of a material-ui dialog to redux in order to prevent it from closing during a rerender. However, I am encountering difficulties with the dialog's behavior when a rerender occurs. Even though th ...

Is there a way to show a loading icon during the image change process without using jQuery?

How can I add a loading icon to an image change event without using jQuery? I'm new to the coding world and Stack Overflow community, so please bear with me. I've been searching for a solution to my specific situation but haven't been able ...

Utilizing jquery's .find() method to tally up elements, while excluding any elements located inside a specific div

I am looking for a solution to tally specific child elements, excluding those housed within an element with a certain class. To count elements, I currently use $('.search-me').find('[class*=seI\\-]').length within a div conta ...

Is your Jquery AJAX request not functioning asynchronously as expected?

I recently started delving into web design and am just beginning to explore jQuery. In my quest to implement a star rating system on my website, I encountered an issue where the user needs to refresh the entire page in order to see the updated star ratings ...

Having trouble with CSS basics? Seeing properties change across multiple classes?

My CSS skills are still in the beginner stage and I am currently struggling to troubleshoot an issue with my code. The HTML: <div id="loginForm"> <span class="dottedLink"><a href="resetlogin">Recover login details</a></span ...

Struggling to set a background image for multiple div elements

Hey everyone! I'm working on a small school project website and I've run into an issue with the background on multiple div elements. Here's my HTML code snippet: <div class="bloc-1-text"> <h3> </h3&g ...

Fill the list items with values from the given array

I have an array called var array = ["one", "two"]; and I want to display each element in a separate list item. The desired output is: <li>Number one</li> <li>Number two</li> However, the current output is: <li>Number onetw ...

I am looking to enhance my JSON output using JavaScript

My JSON output appears as follows: {"intent":"P&P_Purchase","value1":{"date1":"30-Dec-19","prd_desc":"NEEM UREA OMIFCO (45 KG)","qty":"18MT","inv_no":"NRKT07003160"},"value2":{"date1":"25-Dec-19","prd_desc":"NEEM UREA IMP (45 KG)","qty":"18MT","inv_no ...