Is there a way to trigger a Random Number function once the <div> element returns to its initial position?

For this single serving site, the concept involves swiping a card over a specific area to receive different messages depending on interaction with the droppable space. I am looking to implement an if else function that utilizes a random number variable to trigger two distinct functions. Whenever the out: function() is executed, one of two messages will display:

"Please swipe again at this website to enter"

Alternatively, another function will hide certain divs and make others appear. (I have included some code in my current working snippet that I believe is close)

I apologize if my coding terminology is inaccurate, as I am relatively new to this. Thank you for any guidance.

$(function() {
    $("#draggable").draggable({
        containment: 'body', 
        revert:  function(dropped) {
             var $draggable = $(this),
                 hasBeenDroppedBefore = $draggable.data('hasBeenDropped'),
                 wasJustDropped = dropped && dropped[0].id == "droppable";
             if(wasJustDropped) {
                 return true;
             } else {
                 if (hasBeenDroppedBefore) {
                     $draggable.animate({ top: 0, left: 0 }, 'slow');
                     return false;
                 } else {
                     return true;
                 }
             }
        }
    });
    
$("#droppable").droppable({
    over: function() {
            $(this).find('p').html('Reading...');
        },
        
        out: function() {
            $(this).find('p').html('Please Swipe Again At This Website To Enter');
        },
        
        revert: function() {
            $(this).find('p').html('');
        },

        drop: function() {
            $(this).find('p').html('Error Reading Your Card');

       /*   $("#droppable").droppable({
    over: function() {
            $(this).find('p').html('Reading...'); */
        
        // Random generated function I am attempting to create
})
});      
#hi {
width: 50%;
height: 50%;
position: relative;
margin: 0 auto;
display: none;
}

div.blackBar {
position: relative;
background-color: black;
width: 200px;
height: 23px;
top: 75%;
}

div.frontSwiper {
background-color: transparent;
width: 85%;
height: 65px;
position: relative;
-webkit-clip-path: polygon(0 0, 95% 0, 100% 100%, 0% 100%);
clip-path: polygon(0 0, 95% 0, 100% 100%, 0% 100%);
z-index: ;
border-radius: 10px;
}

body { 
width: 90%; 
height: 90%;
margin: auto; 

}

#swiper {
background-color: transparent;
width: 34%;
height: 75px;
margin: 0 auto;
position: relative; 
Top: 200%;
z-index: 0;
border-radius: 0px;
}

#upperSwiper {
background-color: transparent;
width: 34%;
height: 75px;
margin: 0 auto;
position: relative; 
Top: 5%;
z-index: 1;
border-radius: 0px;
}

#droppable { 
width: 200px;
Height: 150px;
margin: 0 auto;
border-Width: 1px;  
background-image:none;
background-color: transparent;
text-align: center;
top: 10%;
}

#draggable, #draggable2 {
position: relative;
top: 50%;
width: 200px; 
height: 130px;  
border-Width: 0px;
background-image:none;
left: 75%;
background-color: #FFD700;
-webkit-clip-path: polygon(0% 0%, 91% 0, 100% 14%, 100% 100%, 0% 100%);
clip-path: polygon(0% 0%, 91% 0, 100% 14%, 100% 100%, 0% 100%);
z-index; -1;
}
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/themes/overcast/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>

<div id="swiper">
<div class="frontSwiper"></div>
<div id="upperSwiper"></div>
</div>
<div id="draggable" class="ui-widget-content">
<div class="blackBar"></div>
    <p></p>
</div>

<div id="droppable" class="ui-widget-header">
    <p style="font: italic bold 30px/40px Arial, Sans-serif; width: 50%, position: fixed;"></p>
    
</div>

Answer №1

If you're looking to handle the event of deactivation or dropping, consider using the deactivate or drop event. It's important to check the x/y position when this event occurs and ensure that it matches the initial x/y coordinates.

For more information, refer to: http://api.jqueryui.com/droppable/

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 message states that the provided callback is not a valid function -

I seem to be encountering an issue with the code snippet below, which is part of a wrapper for the Pipl api: The main function here performs a get request and then retrieves information from the API Any assistance in resolving this error would be greatly ...

The image must be able to fit within the div container without distorting its proportions

Recently, I created a slider that functions flawlessly. However, I am struggling to make the image fit inside the div without distorting its proportions. I've tried various methods but haven't been able to achieve the desired result. Could you p ...

Challenge with form validation

Currently, I am utilizing the jQuery validation plugin to handle form validation. However, I have encountered an issue when using inline labels. Here is an example: <input type="text" name="myinput" value="Enter your ...."> In this specific scenar ...

Modifying the CSS for a single page - specifically the contact us page - on a WordPress website

Recently, I developed a Wordpress website for a client. However, the client has a unique request - they want the Contact Us page to have a different design compared to the other pages on the site. I attempted to create a separate CSS file for the Contact ...

What is the best way to click within a web browser control?

I'm attempting to automate the process of clicking a specific div id button using vb.net. I have integrated a webbrowser for this purpose, and here is my current code snippet. The goal is to click the 'Do Job' button on the website through v ...

Twice the initialization, double the power - Backbone Marionette's Composite View fires

I am currently facing an issue with a composite view where $.dialog is called on its $el. The composite view then displays items from a collection. I have tried various methods to render the collection items: fetching from outside the composite view befo ...

What could be the reason for the malfunction of my flexbox layout?

I need to create a layout with three divs in a row. The first div should have a fixed width, the second div should expand until it reaches a "max-width", and the third div should take up the remaining space. Everything works fine until the text inside the ...

What is the process for including a class on a div element?

I have written a code that displays a series of images in a sequence. Once the last image appears, I want to switch the class from .d-none to .d-block on the div Can this be achieved? onload = function startAnimation() { var frames = document.getE ...

Extracting the JQuery library from its source code

Is there a simple method for extracting only the necessary functions from the jQuery library? I have found that many of the functions within the library are not being utilized, so it would be beneficial to remove them. ...

The tab key seems to be malfunctioning when triggered during a jquery change event

I encountered an unusual issue with my jQuery events, and I'm not entirely sure if it's a problem related to jQuery. In hopes that some jQuery experts can shed some light on this. The code snippet below was included in my HTML page to automatica ...

I'm stumped on how to resolve this CSS issue

As a beginner with the Angular 8 framework, I am exploring how to incorporate animations using angular animations. Below is an example of what my code currently looks like: The component's parent div is contained within <div class="col-md-5">& ...

Ways to make content visible/invisible based on the status of a checkbox

I have implemented a jQuery script that can show or hide a hidden div based on the value selected in a dropdown field: $('.hidden-content-here').hide(); $('select[name="my_field_name_here"]') .on('change', function() { ...

Customizing Row Background Color in Bootstrap

Currently working on a project www.codepen.io/anon/pen/yMmjZb The problem I am facing is with my 3rd row (the one with the 3 columns) where the background color is bleeding beyond the intended boundary. My suspicion is that it's due to the row span ...

The Bootstrap modal is not properly clearing all attribute properties when it is hidden

Alrighty, so I've got this modal set up: <div id="modal-container" class="modal fade" tabindex="-1" role="dialog"> <div class="modal-dialog" role="document"> <div class="modal-content center-block" style="displ ...

How to access the IDs of child pages in WordPress

Being new to WordPress and PHP, I apologize if this question has been answered before. I have searched extensively but haven't found a solution that works for me. The WordPress documentation is quite complex for me to navigate through functions and ho ...

How can the size of the font in a <div> be adjusted based on the number of characters present?

I am attempting to create a basic HTML and CSS layout with two div blocks, each 1000px wide, within a parent container that is 3000px wide. <div class="blocks"> <div class="block-a">text 1</div> <div class="block-b">text 2& ...

Storing Jquery result in a variable in Angular

Within my Angular component, I have a folder structure that needs to retain its previous state (which folder was open) even after reloading due to CRUD operations. https://i.sstatic.net/z44k6.png The API sends back a nested response utilized for generati ...

Personalize the loading bar using JavaScript

Currently, I am utilizing a basic progress bar from Bootstrap. However, I have the desire to design a custom progress bar similar to this: Unfortunately, I am uncertain about how to create such a unique progress bar. Perhaps there is an existing JavaScri ...

Vuejs application is not producing the anticipated outcome when an ordered list contains an unordered list

Is there a way to nest an unordered list within an ordered list in vue 2? I've attempted <template> <div class="p-14"> <ol class="numberlist"> <li>An numbered list</li> <li>Cont ...

What could be the reason why this function in jQuery is not functioning properly?

I am trying to duplicate the span element inside the `test` element using the .clone method. It seems like it works as expected. However, when I try to use .html in a `.click` function, it doesn't work. Can someone point out where I went wrong and sug ...