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 error property is not found in the type AxiosResponse<any, any> or { error: AxiosError<unknown, any>; }

As a beginner with typescript, I am encountering some issues with the following code snippet import axios, { AxiosResponse, AxiosError } from 'axios'; const get = async () => { const url = 'https://example.com'; const reques ...

Focusing on the initial element of every line within a flex container that spans multiple lines

Looking for a solution to style the first item on each line of a multiline flexbox container with display: flex; flex-wrap: wrap. Preferably seeking a CSS-only approach rather than Javascript iteration. It's uncertain how many items there will be or t ...

Adjust the dimensions of an image post-creation

let displayedImage = Ti.UI.createImageView({ image : somefile.png, height:'100', width:'100' }); The code above shows how I created the image. But now, I am wondering how I can resize the image after its initial creation. ...

Tips for activating and setting up Bootstrap popovers

I've been trying to add some popovers to my webpage, but I'm facing a hurdle. I added a few button popovers in the footer, but nothing happens when they're clicked. I created a js file for initialization and imported it at the end of my pa ...

Communication between the front-end (react) and backend (nodejs) is unable to retrieve only the specific value via API call

As a newcomer to backend/frontend development, I'm facing an issue that I can't seem to resolve... I have a store.js file that stores user interaction data (using zustand). One of the variables, 'feed', returns a value like "0xD4a3386 ...

Is there a way to adjust the orientation of a <video> element without affecting the orientation of the video controls?

<video controls> </video> video { transform: scaleX(-1) } This CSS code flips the video horizontally, but it also flips the control buttons along with it. I attempted to flip the wrapper element containing the video using .wrapper {transfor ...

When working with VueJS and Vuex, using the splice method to replace an item (object) in an array stored in Vuex does not trigger a re-render of the

I have an array of records. Each record consists of an object with _id (mongo id), title, and value (value is an object with amount and currency). When displaying the list of records using v-for, the ':key' for each item in the list is set to th ...

Having trouble with expressJs router.post() not functioning properly with multiple middleware, resulting in an [object Undefined] error

I have been working on developing a REST API for my users, utilizing express-validator for validation before adding the user to the database. However, I encountered an issue while chaining my middleware in the router.py file which resulted in the error Err ...

Pass a variable value as a parameter to a jQuery function using code-behind

Within my code behind, I am retrieving the [IDphoto] from an SQL database. My goal now is to pass this IDphoto as a parameter to a jQuery function upon onClick. How can I achieve this? Code behind sb.AppendFormat("<a onclick='popup()' href=& ...

Ways to invoke a C# server-side function using JavaScript

I'm looking to invoke a C# server method from JavaScript. Within my gridview, there is a column containing dropdown lists. When the value of the dropdown list changes, I need to use JavaScript to call a server-side method and update the value of anoth ...

Why isn't the router returning JSON data?

Why am I not receiving a response from this code? const express = require('express') const mongoose = require('mongoose') const authRouter = require('./authRouter') //importing the router const PORT = process.env.PORT || 3000 ...

Struggling with aligning content within a div using CSS grid techniques

I am experimenting with CSS Grid and running into issues with overlaying content. The structure consists of a top-level container defined as a CSS grid (class="container"), followed by a content grid (class="content") that divides into 3 rows (header, labe ...

When choosing from multiple dropdown menus, the selected option should be hidden from the other dropdowns

I'm currently working on a project that involves designing a web form incorporating 3 select boxes with multiple questions. The concept is such that, if I choose the 1st Question from the 1st select drop-down, it should not be available in the 2nd sel ...

Issues occur with Google visualization when the screen is resized

When I resize the browser, an error code google-visualization-errors-8 appears in my Google chart's CSS. https://i.stack.imgur.com/4LX1C.png What could be causing this error to occur? Note: I am redrawing the graph every time the screen is resized. ...

Populate HTML Table with Array Elements

I need assistance with inserting array values into an HTML table. The table should have 5 columns and enough rows to accommodate all the information. I envision it looking like this: 1 | 2 | 3 | 4 | 5 11 | 22 | 33 | 44 | 55 111 | 222 | 333 | 444 | 555 ...

ajax duplicator and reset form tool

Hello everyone, I have a website where users can add their experiences. While adding an experience, they can dynamically add and remove more fields. One of the input fields is for a date, but when the data is submitted, the same date appears for all entrie ...

Issue where CSS modal does not appear when clicked after being toggled

I've been working on a custom modal design that I want to expand to fill the entire width and height of the screen, similar to how a Bootstrap modal functions. The goal is to have a container act as a background with another inner div for content How ...

Ways to create a unique animation for reducing the width of a div in a specific direction?

Currently, I have managed to animate the decrease in width of a div element. However, it is currently decreasing from right to left, and I would like it to decrease from left to right instead. Can someone please provide guidance on how to achieve this? ...

displaying selected value on change in JSP

<html> <head> <title>Displaying HTML Request Parameters</title> </head> <body> <h3>Select an option:</h3> <form method="get"> <input type="text" name="usrname"><br> <select ...

Divider displayed between images in Internet Explorer 8

On my website, I have arranged four images in a square using the code below: <div id="tempo_main"> <div id="tempo_content"> <div style="text-align: center;z-index: 3;position: absolute;right:350px; left:350px; t ...