My draggable item seems stuck in place. Any ideas on how to get it moving again?

I have been trying to implement some code I found on this link. Despite adding all the necessary file links, the code is not functioning as expected. It should be able to move within the bounds of a container, but it's not working properly.

var maxDragX = 200 - $('.slide').outerWidth();
var maxDragY = 200 - $('.slide').outerHeight();

Draggable.create('.slide', {
  bounds: $('#container')
});

$(window).resize(function() {
  Draggable.get('.slide').applyBounds("#container");
});
#container {
  display: block;
  position: relative;
  height: 200px;
  width: 70%;
  border: solid 1px red;
}

.slide {
  display: block;
  position: absolute;
  height: 20px;
  width: 20px;
  background: red;
}

.green {
  background: green;
}
<!DOCTYPE HTML>
<html lang="en">
  <head>

        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/utils/Draggable.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/TweenMax.min.js"></script>
        <script type="text/javascript" src="grabbb.js"></script>
        <link rel="stylesheet" href="grabb.css">
            <!-- <script type="text/javascript">
                var maxDragX = 200 - $('.slide').outerWidth();
                var maxDragY = 200 - $('.slide').outerHeight();

                Draggable.create('.slide', {
                  bounds: $('#container') 
                });

                $(window).resize(function(){  
                  Draggable.get('.slide').applyBounds("#container");
                });
            </script> -->

        
  </head>

  <body>
  <div id="container">
  <div class="slide one"></div>
  </div>
    <button>Change bounds</button>
</body>
</html>

The draggable item is not moving at all. I need help in identifying the error. Thank you.

Here are the console errors:

Uncaught TypeError: Cannot read property 'applyBounds' of undefined at grabbable.html:20 at dispatch (jquery.min.js:2) at y.handle (jquery.min.js:2)

https://i.stack.imgur.com/ln0Tm.png

Answer №1

For optimal functionality, ensure these key libraries are integrated:

var maxDragX = 200 - $('.slide').outerWidth();
var maxDragY = 200 - $('.slide').outerHeight();

Draggable.create('.slide', {
  bounds: $('#container') 
});

$(window).resize(function(){  
  Draggable.get('.slide').applyBounds("#container");
});
#container{
  display: block;
  position: relative;
  height: 200px;
  width: 70%;
  border: solid 1px red;
}

.slide{
  display: block;
  position: absolute;
  height: 20px;
  width: 20px;
  background: red; 
}

.green{
  background: green;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/utils/Draggable.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.20.2/TweenMax.min.js"></script>
<div id="container">
  <div class="slide one"></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

The pagination component in React with Material-ui functions properly on a local environment, but encounters issues when deployed

Looking for some assistance with a persistent issue I've run into. Can anyone lend a hand? [x] The problem persists in the latest release. [x] After checking the repository's issues, I'm confident this is not a duplicate. Current Behavior ...

Regex fails to recognize repeated instances of a specific pattern

Currently, my goal is to create a JavaScript regex that can interpret instances of patterns like \123 and convert them into their corresponding ASCII values. For example, 65 should be replaced with A. If the backslash \ itself needs to be includ ...

Generating step definitions files automatically in cucumber javascript - How is it done?

Is there a way to automatically create step definition files from feature files? I came across a solution for .Net - the plugin called specflow for Visual Studio (check out the "Generating Step Definitions" section here). Is there something similar avail ...

Error occurred while trying to parse JSON due to an abrupt ending

While attempting to reinstall one of my old vue projects on my new computer (running Windows 10) using npm, I encountered the following error: npm ERR! Unexpected end of JSON input while parsing near '...n":"0.8.1","devDepend' ...

Learn how to customize the appearance of tooltips by using the jQuery qTip Plugin to split the 'title' attribute and apply unique styles

Currently experimenting with the qTip jQuery plugin showcased at this link. Managed to get the code functioning based on the demo, but aiming to "explode" the content from the TITLE attribute. The objective is for the first item in the array to serve as t ...

Webpack is failing to recognize certain CSS files

My development stack includes Vue.js 2.5.15, Webpack 4.12.0, css-loader 0.28.11, ASP.Net Core 2.1 in Visual Studio 2017. Starting with the Visual Studio asp.net core template project for Vue and Typescript, I prefer to have individual small CSS files with ...

What's the point of repeatedly confirming prompts?

I am using Jquery ajax call to delete data from my HTML table. Everything works fine, but the alert message keeps showing repeatedly. What could I be doing wrong? Delete button "<a href='#my_modal' class='delete-Record'>Del ...

My jQuery carousel seems to be malfunctioning. Check out the issue here on JSFiddle

I found this amazing resource for jQuery carousel functionality: If you'd like to see it in action, check out the JSFiddle demo I created: http://jsfiddle.net/svQpc/ However, I've encountered a small issue with the Horizontal version of the car ...

Connecting to mongodb using mongoose and performing desired actions

I am working with a collection called foo and using ajax to access it. In the success portion of my code, I have a for loop: function(data){ for(var i in data){ var project = data[i] } } Now that the collection is connected to 'project' ...

Is it possible to reset a form without using JavaScript? (The input type=reset function is not functioning correctly

So, basically I need a quick way to clear all the fields in my form. I've experimented with a couple of options: <input type="reset" value="Clear all fields"> And <button type="reset">Clear all fields</button> Unfortunately, none ...

Dealing with a section that won't stay in place but the rest of the webpage is

I recently came across the angular-split library while trying to address a specific need. It partially solved my problem, but I still have some remaining challenges. In my setup, I have divided my content into 2 sections using angular-split. The goal is f ...

Modify the tooltip of the selected item in an ng-repeat loop in AngularJS

Upon clicking an element, a function is executed which, upon successful completion, should change the tooltip of that specific element. Within an ngRepeat loop, I have multiple elements displaying the same tooltip. However, I only want to update the toolt ...

Declining Effects of AJAX using jQuery

Hi there, I am currently working with an AJAX script that is a combination of jQuery 1.4.4 and jQuery address 1.3.2. What I'm trying to achieve is to make the transitions between divs more smooth by fading them in and out instead of just changing abru ...

What is the best method to adjust the width of the <option> tag within the <select> tag using CSS?

<!DOCTYPE html> <html> <head> <title>Page Title</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="s ...

designing icon buttons

I am interested in adding big buttons with icons to my website. I have images named largebut_hover.png, largebut.png, and icon.png. The icon image is supposed to be displayed inside the light grey background of largebut.png, and when hovering, it should sw ...

Understanding the getJSON MethodExplaining how

$.getJSON( main_url + "tasks/", { "task":8, "last":lastMsgID } I'm a bit confused about how this code functions. I'm looking for a way to retrieve messages from a shoutbox using a URL or some sort of method that the function here ...

When using JSON.stringify, the output is null. However, when using document.write, the data

I am currently working on a plugin for crawljax that involves executing some JavaScript code, similar to the following: String result = browser.executeJavaScript(script).toString(); The script code is as follows: function getElementPosition(id) { var el ...

Extracting information from a checkbox list displayed within an HTML table

I have a table with multiple rows and two columns in each row. The first column contains text, and the second column consists of checkboxes. While I was able to retrieve the values from the first column, I am struggling to fetch the values of the selected ...

Clicking on the icon reveals the current date

I need some help with the input field that displays the calendar date. Currently, I can only click on the input field to show the calendar date. What I actually want is for users to be able to click on a calendar icon to display the calendar date, which sh ...

Using CSS to show only the central portion of an image

How can I display only the center part of an image with dimensions height=300px and width=520px using CSS or jQuery? I tried searching on Google, but didn't find a solution. Is it possible to show only a 200x130 section from the center of an image us ...