Navigating through a Collection of Pictures using Mouse Movement and Left-click Button

Trying to create a customized PACS viewer for scrolling through a series of CT head images.

However, encountering an issue with scrolling through all 59 images. Currently, able to scroll from the first to the 59th image, but struggling to loop back to the beginning when keeping the left mouse button clicked and moving the mouse upwards.

When left-clicking and dragging down, it scrolls only from image 1 to 59, and does not return to the first image when left-clicking and moving up. Any suggestions?

Here is the jQuery code:

$(document).ready(function() {
    var lbDown = false;

    $("#imgs").mousedown(function(e) {
        if (e.which === 1) {
            lbDown = true;
        }
    });
    $("#imgs").mouseup(function(e) {
        if(e.which === 1) {
            lbDown = false;
        }
    });

    $("#imgs").mousemove(function(e) {
        if(lbDown) {
            e.preventDefault();
            $(this).find('img:visible').next().show();
        }
    });
});

A fiddle has been created as well:

http://jsfiddle.net/C4Y6H/

Answer №1

Check out this jsBin demo

$(function() {
    var $imgs = $('#imgs'),       // Efficiently store elements in variables
        $img = $('img', $imgs),   
        lbDown = false,
        Y = 0,                    
        exY = 0;                  

    $imgs.on("mousedown mouseup",function(e) {
        if (e.which === 1) {
            e.preventDefault();
            lbDown = e.type=="mousedown";  
        }
    }).on("mousemove", function(e) {
        if(lbDown) {           
           e.preventDefault();
           Y = e.clientY;                             
           var $visible = $(this).find('img:visible'); 
           $img.not(":first").hide();                  
           $visible[Y>exY ? "next" : "prev"]().show(); 
           exY = Y;                                    
        }
    });
   // Consider adding the mouseleave event for better functionality.
});

The code needed to be updated to hide overlapping images for a smoother transition between them on mouse movement to the left.


Another approach involves using the .eq() method to calculate the image based on the difference between the current and initial mouse position.

(mousemoveCoordY - clickedInitiallyAtY) % numberOfImages

Try out the DEMO here

% operator can create an infinite loop, so faster mouse movements might skip frames*.

$(function() {
    var $imgs = $('#imgs'),
        $img = $('img', $imgs),
        n = $img.length,
        lbDown = false,
        Y = 0,
        clickedY = 0;

    $imgs.on("mousedown mouseup",function(e) {
        if (e.which === 1) {
            clickedY = e.clientY;
            e.preventDefault();
            lbDown = e.type=="mousedown";
        }
    }).on("mousemove", function(e) {
        if(lbDown) {           
            e.preventDefault();          
            $img.hide().eq((e.clientY-clickedY)%n).show();
        }
    });
    // Consider adding the mouseleave event for better functionality.
});

*Adjusting the math can control the speed of the transition:

 $img.hide().eq((((e.clientY-clickedY)*0.3)|0)%n).show();

Similar to this DEMO where (0.1 - 0.9) acts as a sensitivity multiplier.

Answer №2

With each movement, reveal the following image without hiding the previous ones. To reverse this process, consider determining a direction based on the Y-axis coordinates. If Y is greater than or less than half of the image's height, assign a direction of -1 or 1 respectively, then display the previous image instead of the next one.

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

Analyzing length of strings by dividing content within div tags using their unique ids

Here's my issue: I'm using AJAX to fetch a price, but the source from where it is fetched doesn't add a zero at the end of the price if it ends in zero. For example, if the price is 0.40 cents, it comes back as 0.4. Now, my objective is to t ...

Secondary menu supersedes primary dropdown menu

Having trouble getting a vertical dropdown menu to work properly, with the submenus overriding the main menu. I've searched everywhere for a solution but can't seem to find one. Anyone out there who could help me figure this out? Here's the ...

Tips for implementing JQuery in a ReactJS Component

I attempted to import the jquery file into my index.html in order to use the jquery $ symbol within a React component. However, when I tried using $('button').click(function()), I encountered errors stating that $ was undefined. I believe there ...

Create a div that pops up when clicked, just like the Fancybox jQuery feature

I'm looking to create a popup div with an elastic effect similar to jQuery's fancybox. I've tried using the following code, but it doesn't seem to work... Here is the HTML: <a href="#" id="LoginAnchorLink">ClickME</a> < ...

The error message displays "window.addEventListener is not a function", indicating that

Recently, I've been dealing with this code snippet: $(document).ready(function(){ $(window).load(function() { window.addEventListener("hashchange", function() { scrollBy(0, -50);}); var shiftWindow = function() { scrollBy(0, - ...

Can divs be arranged in a similar fashion as images?

My goal is to create a navigation bar (#nav) with each nav item being a div (#nav div). However, my current approach isn't working as expected. Here is the code I'm using: #nav { background: url("navbg.png"); /* navbg.png is 1x40 pixels */ ...

jQuery DataTables covering a CSS-anchored menu bar

My webpage has a pinned navigation menu bar at the top and some tables with interactive features using jQuery's DataTables. However, after implementing jQuery, I encountered an issue where the tables cover the menu when scrolling down, making it uncli ...

CSS for Page Header with Scroll-Over Content

Having trouble creating a "collapsing header" design where the page content scrolls over the banner image. I've tried adjusting z-index and positioning properties in CSS, but can't achieve the desired effect. Any help or guidance would be greatly ...

F# Using Ajax Post with JQuery

Attempting to utilize an Ajax Post to communicate with my F# controller in order to invoke a method that outputs a simple string. The ultimate goal is to have data selected via checkboxes on the webpage inserted into a database. Struggling with understand ...

Exploring the Fusion of Different Styles in Material-UI Using React

I have two different styles that I use in my code. One style is specific to certain components, while the other style is global and used across various components. For example, consider the following file tree: index.tsx -App.tsx -globalConstants.ts In ...

Two separate ajax functions executed sequentially both yield identical results

I am encountering a strange issue with 2 different ajax functions being called consecutively. Each function fetches a different value and populates different text boxes, but they both return the value of the first function called. Here is the code snippet ...

Ways to Export HTML to Document without any borders or colorful text

How can I make a contentEditable area visible when filling text and then disappear when exporting the document? I found a script online that allows you to do this, but the issue is that the contentEditable area is not visible until clicked on. To address t ...

Requests for JSONP from LinkedIn and StumbleUpon

Currently, I am working on an application that retrieves information about URLs from various social networking sites such as Facebook, LinkedIn, Twitter, StumbleUpon, Digg, and Delicious. To fetch this data, I am utilizing REST requests and receiving JSON ...

What is the method for creating a checkbox in CSS that includes a minus symbol inside of it?

Can you create a checkbox with a minus "-" symbol inside using just html and css? I attempted to achieve this with JavaScript by using: document.getElementsByTagName("input")[0].indeterminate = true; However, the requirement is to accomplish this using ...

Dynamically modifying the styling of elements within an iframe

In my current project, I encountered a challenge with changing the background color to black and the body text color to white within an iframe. Additionally, all elements that are originally styled with black in an external stylesheet also need to be chang ...

Fade in upcoming content over prevailing content utilizing jQuery

Looking to implement a fade-in effect for new posts retrieved from my database. The existing posts are initially loaded in a div when the page loads, and I would like the new posts to smoothly appear above the old ones. Recently began exploring jQuery and ...

What are some effective methods for testing internet connectivity?

My CMS operates by pulling large amounts of data using PHP, MySQL, jQuery, Bootstrap, and AJAX. An issue arises when the internet connection is lost, causing problems with displaying and scrolling on the site. I am interested in finding a solution that c ...

I am attempting to retrieve the value of a 'td' element using jQuery, but it consistently returns 'null'

My task involves extracting the value of a specific cell within a table to filter out unwanted rows. However, every time I attempt to retrieve the value of that cell, it consistently returns null or nothing at all. Despite referencing various sources and s ...

resume the execution of PHP script after being called by Ajax

I have a page named call.php containing the following code: <script src="http://localhost/js/jquery.js"></script> <script> $(document).ready(function() { $.post("exe.php", { ...

Storing ajax data into a variable seems to be a challenge for me

I am facing an issue with my ajax call where I am receiving the data correctly, but I am unable to assign it to a local variable named item_price. The data that I am receiving can either be 100.00 or 115.25. Below is the snippet of my ajax code: $.ajax({ ...