Enable the expansion of a div by dragging and dropping an image onto it

Take a look at this fiddle. In this fiddle, I am able to drag and drop images onto the .drop-zone. However, I would like to enhance it so that when I drag an image onto it, the .drop-zone div expands to where I place the image. It's okay if the expanded area is scrollable.

<div class="drag"><img src="http://www.causingeffect.com/images/made/images/example/cow_50_50_c1.jpg" /></div>
<div class="drag"><img src="http://www.coffeecup.com/images/software/feature-icons/image_editing.png" /></div>
<div class="drag"><img src="http://www.neatimage.com/im/Neat-Image-Logo-64.png" /></div>

<div class="clear"></div>

<div class="drop-zone"></div>

JS

jQuery(function($) {

    $('.drop-zone').droppable({
        accept: '.drag',
        drop: function(event, ui) {
            var $clone = ui.helper.clone();
            if (!$clone.is('.inside-drop-zone')) {
                $(this).append($clone.addClass('inside-drop-zone').draggable({
                     containment: '.drop-zone'
                }));
            }
        }
    });

    $('.drag').draggable({
        helper: 'clone'
    });

});

CSS

.drop-zone{border: 1px solid #9C9898;height:350px;width:400px;margin:0 auto;margin-top: 10px; overflow:auto;}
.clear{clear:both;}

Answer №1

To identify the edges effectively, I have utilized child elements within the .drop-zone container. One challenge arises when you need to ensure that the .drop-zone element has a position: relative property which can interfere with drag-and-drop functionality. A solution to this issue is detailed in this guide found here. Moreover, it becomes apparent that during dragging, the mouseover event does not propagate across the .drag element, necessitating that the .drop-zone-edge elements have a higher z-index for proper display order. You can test the implementation here. Extend this technique to handle top, left, and right edges, while ensuring transparency for the .drop-zone-edge elements.

HTML Structure:

<div class="drop-zone">
    <div class="drop-zone-edge drop-zone-top"></div>
    <div class="drop-zone-edge drop-zone-bottom"></div>
</div>

CSS Styling:

.drag {
  z-index: 100;
}

.drop-zone-edge {
    width: 100%;
    height: 10px;
    background-color: red;
    position: absolute;    
    z-index: 200;
}

.drop-zone-top {
    top: 0;
}

.drop-zone-bottom {
    bottom: 0;
}

JavaScript Functionality:

var isClicked = false;

$(document).mousedown(function() {
    isClicked = true;
})
.mouseup(function() {
    isClicked = false;
});

$('.drop-zone').droppable({
    accept: '.drag',
    drop: function(event, ui) {
        var $clone = ui.helper.clone();
        if (!$clone.is('.inside-drop-zone')) {
            $(this).append($clone.addClass('inside-drop-zone').draggable({
                 containment: '.drop-zone'
            }));
        }
        var parent = $('.drop-zone');
        var leftAdjust = $clone.position().left - parent.offset().left;
        var topAdjust = $clone.position().top - parent.offset().top;
        $clone.css({left: leftAdjust, top: topAdjust});
    }
});

$('.drag').draggable({
    helper: 'clone'
});

var shouldEnlargedBottom = false;
$('.drop-zone-bottom').on('mouseover', function(e) {
    if(isClicked) {
        shouldEnlargedBottom = true;
    }
});

$('.drop-zone-bottom').on('mouseout', function(e) {
    shouldEnlargedBottom = false;
});

function enlargeArea() {
    if(shouldEnlargeBottom) {
        var newHeight = $('.drop-zone').height() + 3;
        $('.drop-zone').height(newHeight);
    }
}

setInterval(enlargeArea, 100);

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

Changing a variable within a method does not automatically reflect in the child component

Need help with Vue update process for props/child components. Consider this component: <template> <v-card> <Modification v-model="newObject"></Modification> <OtherComponent @close="resetObject">& ...

Angular JS Unveiled: Deciphering HTML Entities

I'm looking for a solution to decode HTML entities in text using AngularJS. Here is the string I have: "&quot;12.10 On-Going Submission of &quot;&quot;Made Up&quot;&quot; Samples.&quot;" I need to find a way to decode this u ...

Storing the returned JSON response from Jquery into a variable

URL: JSON response: { "USD-INR": { "val": 61.235 } } Javascript (jQuery): function convertCurrency(){ var selectedCurrency = $('#converter select').val(); jQuery.ajax({ type : 'GET', dataType: 'j ...

Exploring the best practices for organizing logic within Node.js Express routes

I'm currently utilizing the https://github.com/diegohaz/rest/ boilerplate, but I am unsure about the best practice for implementing logic such as QR code generation and additional validation. My initial thought was to include validation and password ...

Making an Ajax Request with Several Parameters

Currently delving into the world of AJAX calls within ASP.NET MVC, I am grappling with how to effectively pass specific parameters to my controller. In the LeadController: [HttpPost] public void SaveWebLeadforYear(string office, int year, int[] v ...

The submission of the form is not possible with jQuery

Here is my code: $(document).ready(function(){ console.log("jquery is functioning"); // **** Global Variables **** //starting ingredient number for new ingredients $ing_num = 3; var new_label; var new_input; $("#add_icon").click(funct ...

Cursor hovers over button, positioned perfectly in the center

I am facing a challenge with implementing a hover function for a set of buttons on the screen. The goal is to display the mouse pointer at the center of the button upon hovering, rather than the actual position of the cursor being displayed. I have tried u ...

Exploring the World of MVC 4: Delving into Modal Windows, Partial Views,

I am currently utilizing MVC4 along with Entity Framework to build a web application. Within my database, I have a table listing all the individuals. Each person can have their information edited through a modal window that is implemented as a Partial View ...

Visual feedback: screen flashes upon clicking to add a class with jQuery

I have successfully added a click event to my pricing tables in order to apply an animation class on mobile devices. However, I am facing an issue where every time I click on a pricing option on my iPhone, the screen flashes before the class is applied. Is ...

The flashing of Bootstrap tabs can be seen on various pages

Currently, I am encountering an issue with bootstrap tabs on my website. There seems to be a blinking problem with the tabs on certain pages. Check out this video showcasing the blinking tab in the search result page: Watch Here Interestingly, the same ta ...

Create a function that adds a new div element with a one-of-a-kind identification when

I am currently developing a web application on www.firstusadata.com/cash_flow_test/. The functionality involves two buttons that add products and vendors dynamically. However, the issue I'm facing is that the appended divs do not have unique IDs, maki ...

What could be causing the entire app to malfunction when the Redux Provider tag is used

I am facing an issue while trying to integrate a React Toolkit app into my project. The error message I encountered is as follows: Error: Invalid hook call. Hooks can only be called inside the body of a function component. This error may occur due to one ...

Utilizing data from a JavaScript array and merging it with basic HTML structure

Every day, a js array source on a remote server gets updated: var io = new Array(); nsi[0] = new Array('','Frank','','Factory worker','Mercedes',374.0,26.2,76,181,'',75,'Audi',1456.5,27 ...

Are there any debugging tools specific to Internet Explorer for JavaScript?

I need a reliable JavaScript debugger for Internet Explorer. I have tried using Firebug Lite, but it doesn't seem as detailed as the original Firebug when it comes to displaying JavaScript errors. Does anyone know how to pinpoint JavaScript errors in ...

Printing a React component using a button causes formatting related to className to be lost, while printing it inline preserves the formatting

I've hit a roadblock in trying to figure out the React and printing issue for the past week and a half, and it feels like I'm going in circles. Despite finding some helpful solutions on Stack Overflow, none of them seem to work completely for me ...

Extract the content of a textbox within an iframe located in the parent window

Is it possible to retrieve the value of a text box in an iframe from the parent page upon clicking a button? Below is an example code snippet showcasing the situation: <div> <iframe src="test.html" > <input type=text id="parent_text"> & ...

Every time I click a button, I am trying to add JSON objects into an array and then show the outcomes

Currently, my goal is to create a random selection feature from an array that users can contribute to by clicking a button. I am a bit unsure about how to proceed with this task. The application is developed in React and it utilizes the movieDB's API ...

Show a 1920x1170 / 183KB image as the background using CSS styling

I need help with displaying a background image that is 1920x1170 pixels and has a file size of 183KB. Here is the code I am using: .bground{ height:auto; left:0; position:fixed; top:0; width:100%; } <div class="bgrou ...

Unable to load the type [ProjectName].WebAPIApplication in the Web API application

I am currently working on a .Net Web Application that utilizes the Web.API Project Properties indicating it is built with .Net 4.5. Within my web page, the code snippet below is used to call one of the methods: $.ajax({ type: 'POST', u ...

Position the Bootstrap button on the right side

this is the code I am working with: <p class="h1 p-3 mb-2 bg-danger text-white text-danger">Lagerbestand unter Mindestmenge! </p> <br /> <div class="border text-center"> <p class="h5 text-center">Durc ...