Moving the left side of the screen using draggable feature in JQuery

I'm trying to figure out how to determine the offset of the left side of the screen. I've managed to calculate the offset for the right side, as shown in the example below. However, I also need to do the same for the left side, where the text should display "Not Interested".

Can someone please provide guidance or assistance on how to achieve this?

If anyone has suggestions for improving my current code or a better approach, I would greatly appreciate the feedback.

$(document).ready(function(){
    var currentDiff;
    var currentOpacity;
    
    $("#event_container .content .card").draggable({
        drag: function(el, ui){
            var cardWidth = $(this).width();
            var bodyWidth = $("body");
            var rightOverlay = $(this).offset().left + (cardWidth * .6);
            var leftOverlay = ($(this).offset().left - cardWidth) / 6;
            if(rightOverlay > cardWidth){
                var widthDiff = rightOverlay - cardWidth;
                
                if(!$("#interested-message").is(":visible")){
                    currentDiff = 0;
                    currentOpacity = 0;
                }
                if(widthDiff > 175){
                   if(currentDiff === 0){
                       currentOpacity = 0.1;
                       $("#interested-message").addClass("interested").css("opacity", currentOpacity).text("Interested").show();
                       currentDiff = widthDiff;
                   } else if((currentDiff + 20) > currentDiff) {
                       if(currentOpacity !== 1){
                           currentOpacity = currentOpacity + 0.1;
                           $("#interested-message").addClass("interested").css("opacity", currentOpacity);
                            currentDiff = widthDiff;
                       }
                   }
                } else {
                    $("#interested-message").css("opacity", 0).hide().text("....");
                }
            } else {
                $("#interested-message").css("opacity", 0).hide().text("....");
            }
            
            if(leftOverlay > cardWidth){
                var widthDiff = leftOverlay - cardWidth;
                console.log(widthDiff);
            } else {
                console.log(leftOverlay);
            }
        }
    });
});
#interested-message{
    display: none;
    position: absolute;
    width: auto;
    padding: 5px 15px!important;
    z-index: 100;
    border-radius: 6px;
    font-size: 30px;
    top: calc(45% - 100px);
    left: calc(25% - 100px);
    opacity: 0;
}
#interested-message.interested{
    display: block;
    border: 2px solid #0b9c1e;
    color: #0b9c1e;
}
#interested-message.not-interested{
    display: block;
    border: 2px solid #d93838;
    color: #d93838;
}

#body{
  width: 250px;
  height: 600px;
  max-width: 250px;
  max-height: 600px;
  border: 2px solid black;
  overflow: hidden;
}

#event_container{
  height: 100%;
  width: 100%;
  background: lightblue;
  padding: 50px;
}

#event_container .content{
  position: relative;
}
#event_container .content .card{
  border: 2px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


<div id="body">

<div id="event_container">
    <div id="interested-message">....</div>
    <div class="content">
        
        <div class="card">
        
          Test card
        
        </div>
    </div>
</div>

</div>

Answer №1

If you're curious, I managed to find the solution on my own. Additionally, I have revamped some of my code and made it more organized.

the -230 in if(widthDiff < -230) could potentially be dynamically inserted, just like the 105 in if(widthDiff > 105). However, I haven't included this in the provided example.

Here's the complete version:

$(document).ready(function(){
    var currentDiff;
    var currentOpacity;
    var interested = false;
    var notInterested = false;
    $("#event_container .content .card").each(function(){ 
        $(this).draggable({
            drag: function(el, ui){
                var cardWidth = $(this).width();
                var rightOverlay = $(this).offset().left + (cardWidth * .6);
                var widthDiff = rightOverlay - cardWidth;
                if(widthDiff < -230){
                    notInterested($(this));
                } else if(widthDiff > 105){
                    interested($(this));
                } else {
                    reset($(this));
                }
                function notInterested(ele){
                    $("#interested-message").addClass("not-interested").text("Not Interested").show();
                    ele.draggable( "option", "revert", false);
                    notInterested = true;
                }
                function interested(ele){
                    $("#interested-message").addClass("interested").text("Interested").show();     
                    ele.draggable( "option", "revert", false);
                    interested = true;
                }
                function reset(ele){
                    $("#interested-message").removeClass("interested").removeClass("not-interested").hide().text("....");
                    ele.draggable( "option", "revert", true);
                    interested = false;
                    notInterested = false;
                }
            }
        });
    });
});
#interested-message{
    display: none;
    position: absolute;
    width: auto;
    padding: 5px 15px!important;
    z-index: 100;
    border-radius: 6px;
    font-size: 30px;
    top: calc(45% - 100px);
    left: calc(30% - 100px);
}
#interested-message.interested{
    display: block;
    border: 2px solid #0b9c1e;
    color: #0b9c1e;
}
#interested-message.not-interested{
    display: block;
    border: 2px solid #d93838;
    color: #d93838;
}

#body{
  width: 250px;
  height: 600px;
  max-width: 250px;
  max-height: 600px;
  border: 2px solid black;
  overflow: hidden;
  margin-left: 50px;
}

#event_container{
  height: 100%;
  width: 100%;
  background: lightblue;
  padding: 50px;
}

#event_container .content{
  position: relative;
}
#event_container .content .card{
  border: 2px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>


<div id="body">

<div id="event_container">
    <div id="interested-message">....</div>
    <div class="content">
        
        <div class="card">
        
          Test card
        
        </div>
    </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

Encountering issues with resolving dependency tree post updating node, specifically with node-sass dependency causing failure

Following the update to the latest version of Node.js, I encountered error messages when attempting to use npm audit fix --force. It appears that resolving dependency tree issues is proving to be difficult. Despite extensive research and trying various s ...

Battle of the Browsers: Firefox vs Chrome/IE Design

Excited to be part of this community and looking forward to tapping into so much knowledge here! I have noticed that my website, , appears differently on Firefox compared to Chrome and IE. The gray background only shows on Firefox, while Chrome and IE dis ...

AntDesign is throwing an error stating that resetFields is not a function when using

I'm facing an issue with resetting the fields in my form. The form allows users to add more forms, and so on... Upon successful validation, I want to save the data in my DB and store AND reset all input fields in the form. Unfortunately, I'm un ...

The fetch feature is only activated upon refreshing the page

Attempting to utilize the new Nuxt.js Fetch method has presented an issue. Initially, everything seemed to be working properly. However, I noticed that the data is only fetched and displayed upon refreshing the page. When accessing the page through $fetchS ...

What are the disadvantages of using getBoundingClientRect() in ReactJS?

I recently incorporated the getBoundingClientRect() method into my project. However, a fellow React developer expressed concerns about its browser compatibility. In theory, shouldn't Webpack or Babel handle such compatibility issues? ...

Having issues with Pubnub subscription functionality

Currently, I am in the process of incorporating a basic chat feature into my web application using AngularJs 1.4.5 and pubnub. To do this, I am following the instructions outlined in the pubnub tutorial. $scope.channel = 'chat_for_trial'; $scop ...

Unleash the Power of Your Webpage: Instantly Engage Full

Is there a way to automatically make a webpage open in full screen mode as soon as it is loaded? Here's what I currently have: var elem = document.documentElement; function openFullscreen() { if (elem.requestFullscreen) { elem.requestFull ...

Is there a way to reposition the dynamically added URL path before the variables instead of after?

Just a brief introduction: In my Symfony-based ecommerce template, I have implemented a feature where all products from different pages are loaded using AJAX requests with an infinite scroll functionality. It works flawlessly when the URL is clear, like t ...

New update in Fullcalendar v2: Enhancements to dayRender function for agenda view and agenda

For the newest version of FullCalendar, I am in need of the dayRender callback to modify the color of disabled days. Unfortunately, this specific callback only functions for month, basicWeek, and basicDay views. However, I require this functionality for a ...

jQuery on-click event malfunctioning as expected

I'm currently developing a project that utilizes the GIPHY API to retrieve GIFs. Each time a search is performed, I am storing the search history as individual buttons which users can click on to view the results without needing to re-enter the search ...

What method is most effective for transferring arrays from C# to Javascript in a simple and straightforward manner?

Although there may be similar questions here, none of them are asking the exact same thing as mine. For my ASP.NET MVC4 project, I have decided to use aspx as my view engine. Within my views, I have integrated a JavaScript code that takes a table of value ...

Can the timeout be adjusted dynamically within jcarousellite?

Is it possible to set different timeouts for specific slides in jcarouselLITE (not jcarousel)? For example, the first three slides should have a timeout of 1000, while the fourth slide should have a timeout of 8000. The code snippet below demonstrates how ...

AngularJS $q - pausing execution to synchronize all promises

I've encountered a challenge that I haven't been able to solve through online searches. In my project, I am using a library for selecting items and performing custom modifications through callback functions. However, I need to execute some async ...

Tips for accurately dissecting a PDF document to determine the status of checkboxes

Looking for a solution to parse PDF forms on the server side, I have experimented with various node.js modules including pdf2json, hummus, and node-pdftk. While I have successfully retrieved all text fields, I am facing issues when it comes to extracting c ...

Troubleshooting the Width Problem in Bootstrap 5 Dropdowns

I am currently working on a new project and encountering an issue with the width of a Bootstrap 5 dropdown. The problem lies in the discrepancy between the button width and the menu width. Although it may seem simple, I am having trouble resolving it as I ...

Adding HTML code directly at the cursor location within a contenteditable element

On button click, I need to insert an element at a specific position within a contenteditable div. Currently, the content is added at the end of the document instead of where the cursor was placed before clicking the button. function append_content() { ...

IIS Configuration and Web Management

Our team is currently working on addressing vulnerability issues on our website, specifically a Cross Site Scripting threat identified during Vulnerability Testing. We have added four lines of code to the web.config file in order to mitigate this issue. ...

Using TypeScript with React: Initializing State in the Constructor

Within my TypeScript React App, I have a long form that needs to dynamically hide/show or enable/disable elements based on the value of the status. export interface IState { Status: string; DisableBasicForm: boolean; DisableFeedbackCtrl: boolean; ...

Flex items maintaining their size when the window decreases

My goal is to arrange two plotly plots side by side within a CSS flexbox, with the ability to resize them as the window size changes. The issue I'm facing is that while the plots expand correctly when the window is enlarged, they fail to shrink when t ...

jQuery alert: A TypeError was caught stating that the object being referred to does not have the 'dialog' method available

For a few days now, I have been tirelessly searching for a solution to this issue and it has caused me quite a bit of stress. My problem lies in echoing a JQuery popup script within php: echo '<link rel="stylesheet" href="http://code.jquery.c ...