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

jQuery prioritizes enforcing style over using a CSS file

I am facing an issue with a nested div functioning similar to a drop-down menu. The problem arises when I click on the #two div as I want it to disappear. To handle this, I utilized jQuery and tried using both .hide() and .slideUp(). However, upon implem ...

Align a Bootstrap button to the bottom of a flex-box column

After exploring numerous online discussions in search of a solution to my issue, I have come up short. Applying 'align-self: flex-end' to the .reveal-btn only moves the button to the right, whereas using 'margin-top: auto' has proven in ...

There seems to be an issue with the bootstrap datepicker as it is throwing an error stating that $(

Currently in the process of developing a web application using AngularJS with MVC, I have integrated Bootstrap's datepicker into my project, Here is the code snippet: <div class="container"> <div class="row"> <div class=& ...

revealing and concealing information using an accordion-style interface

Looking to enhance my basic jQuery accordion functionality that displays a list. Currently, I aim to hide specific content with a button click and make it visible again when the button is clicked once more. 1) Title 1 2) Title 2 3) Title 3 4) Title 4 &l ...

Space between elements in a horizontal arrangement

While working with Bootstrap 5 and using paddings in columns, I encountered issues when some of my columns had backgrounds or when adding an embed element. How can I create margins in such cases? To address this issue, I implemented a workaround by assign ...

Issue with fading in! The div should fade in only when it is no longer covering the element

I am currently working with two div elements called "#Header_Info" and "article". The "article" div is static and displays information on the page, while the "#Header_Info" div expands to show extra information. However, a problem arises when the "#Header ...

The recent update to Bootstrap v5 caused a complete disruption in the CSS of our application

My Angular app was originally on Angular 14 and used Bootstrap with SCSS compiled to node-sass/SASS package. It also utilized ng-bootstrap v11 and Bootstrap v4.3.1 for CSS styles. As part of a general upgrade, I needed to update the Angular version to 15. ...

Ways to position cursor at the end in the Froala Editor

My current dilemma involves the need to position the focus/cursor at the end of text that is loaded within the Froala editor. I attempted to accomplish this using the focus event of Froala (events.focus), but unfortunately, it did not yield the desired out ...

Switch between input and the input is not displayed inline

I've been struggling to align the Toggle button with the input box for quite some time now. I really need them to be inline so that they form a grid properly as everything is dynamic. I have experimented with using a Div on its own instead of a label ...

I'm encountering difficulties with customizing the root styling of material-ui's DialogActions

Check out the two buttons in a DialogActions just like this. This is the JSX snippet I'm working with: <DialogActions classes={{ root: classes.dialogActionsLeft }}> <Button autoFocus onClick={() => { setOpen(false); }} ...

Vue: auto-suggest feature in a text input field

I am looking to implement a text field with typeahead functionality. Essentially, I have a collection of words and as you start typing in the field, suggestions should appear based on the input. The challenge is that it should be capable of providing sugge ...

Having trouble with Javascript in getting one-page scroll navigation to work?

Hey there, I am working on creating a one-page scroll navigation with some basic javascript to add a smooth animation effect that takes 1 second as it scrolls to the desired section. However, I seem to be experiencing an issue where it's not functioni ...

Modifying Bootstrap image dimensions

I've been working on creating a card div with two images inside: an up arrow and a down arrow. Everything looks good when the page is in full screen mode, with the images displaying at their full size. However, with Bootstrap's responsive design ...

Eliminate the initial item in each list item

I am trying to delete the first element inside each li element, but my current code only removes the a element from the first li and not all li elements. For example: This is what I have: <ul class="myclass"> <li class="product-category produc ...

Ways to enhance HTML components within a div using jQuery

I'm currently facing difficulties while attempting to troubleshoot and incorporate a new feature into my project. Within my application, users have the ability to upload images, along with accompanying data and related information, via AJAX. Each upl ...

The page remains static even as the function is executed

Having trouble with creating a sliding form using jQuery. The issue is that instead of sliding to the desired page, it slides to a blank one. The website where this problem can be seen is www.entrilab.com. Upon inspecting the element, there are no errors ...

Creating a button with a water-inspired design using CSS that works seamlessly across

Looking to create a stylish aqua-themed button that works seamlessly across different browsers. I'm new to CSS, so any guidance is appreciated. I found an online example that looks great in Chrome but fails to display properly in IE... Check out the ...

Difficulty with navigation on Chrome for Android (also impacting Bootstrap's website and all other sites utilizing Bootstrap design)

While working on creating a responsive website using Bootstrap, I encountered an unusual issue with navigation specifically in Chrome on Android. After some investigation, I found that the same problem exists on Bootstrap's own website. To see the i ...

Tips for overlapping children in a column flex direction while maintaining the parents' positioning

Currently, I am working with two parent flex items, arranged with flex-direction: column. Within the first parent, there are two children. However, one of the children is optional and may be removed at times. I aim to have the optional child displayed on ...

Achieving success by correctly reaching the window's edge during an event (onscroll)

This happens to be my 1st inquiry. Specifically, I have a navigation menu with a transparent background and I am attempting to alter the background once it reaches the top edge of the window. window.addEventListener("scroll", navSticky); function navSt ...