Adjust the zoom level while scrolling through two images

Two images, identical in size and position within dynamically sized divs based on user interaction through the jquery beforeAfter plugin.

I want to implement scroll zooming on these images using wheelzoom, so that zooming on one image will mirror the same amount and position on the other.

The challenge lies in linking event handlers effectively:

function onwheel(e){
    //adjust image to fit zoom level ...

    other_img.onwheel(e);
}

If direct linking isn't possible, is there a way to duplicate the event and redirect it to the second image?

This problem requires a solution using either jquery or native Javascript.

View the code here (disregard the handle).

EDIT: Any guidance on what approach could work would be greatly appreciated.

Answer №1

Check out my demonstration here: http://plnkr.co/edit/kH0ec8TVMXUIYlMoBaMq?p=preview

Below is the main snippet of code:

document.getElementsByClassName("before")[0].addEventListener("wheel", function(event){
  if(flag){
    flag= false;
    return;
  };
  flag=true;
  var newEvent = new WheelEvent("wheel",event);
  var elementToTrigger = document.getElementsByClassName("after")[0]; 
  elementToTrigger.dispatchEvent(newEvent);
});

I have implemented a simple solution where an event ("wheel") triggers the same event on another element, passing the data from the initial event to the new event as an argument. By using the flag variable, I prevent the custom event from triggering the other event again and creating an endless loop.

This approach allows for a solution without modifying the source code of the plugin. However, modifying the code of wheelzoom.js could lead to even better solutions.

Answer №2

Create a custom zooming function that accepts multiple parameters to manage the zoom action, extracting necessary values from the event handler.

function adjustZoom(element, direction, amount) {
    // Implement logic to resize the `element`
}

var image1, image2;

image1 = /* select image node */;
image2 = /* select image node */;

function handleScroll(scrollEvent) {
    var scrollDirection, scrollAmount;

    // Determine zoom direction and amount based on `scrollEvent`

    adjustZoom(image1, scrollDirection, scrollAmount);
    adjustZoom(image2, scrollDirection, scrollAmount);
}

image1.onscroll = handleScroll;
image2.onscroll = handleScroll;

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

Steps for inserting an additional header in an Angular table

https://i.stack.imgur.com/6JI4p.png I am looking to insert an additional column above the existing ones with a colspan of 4, and it needs to remain fixed like a header column. Here is the code snippet: <div class="example-container mat-elevation-z8"> ...

Utilize AJAX to fetch and load JSON data for use in a different function

When a button is triggered, I have two functions that run. One function is called immediately, while the other is called once the processing triggered by the button is complete. I want to optimize the loading time by loading the JSON data from the AJAX req ...

Encountering cross-domain issues while integrating AngularJS in flex (web control), and trying to retrieve templates/json (local files)

To provide some background on my current endeavor, I am utilizing a Flex application to serve as a container for a web app. Within this Flex app resides a web controller that loads a complete Angularjs application. All necessary files are stored locally wi ...

Iterate through each element in all arrays, but limit the loop to only iterate through the elements in the first array up to its

Within my collection of arrays, each containing elements, the structure is as follows: var result = [ // starting with the outer array [ // initial internal array {name: 'A', value: 111}, {name: 'B', value: 222} ...

When making a request through local apache, the POST method is switched to GET

I've been attempting to send a post request using the code below. However, the request is being sent as a GET instead of POST. How can I resolve this issue? $.ajax({ url: 'https://www.exampleurl.com', method: 'POST', h ...

Customize dynamically loaded data via AJAX

I have a webpage that is loading a table from another source. The CSS is working fine, but I am facing an issue editing the table using jQuery when it's loaded dynamically through a script. It seems like my changes are not getting applied because they ...

Executable program contained within npm bundle

I am working on creating an npm package that can be executed as a command from the shell. I have a package.json { "name": "myapp", "version": "0.0.6", "dependencies": { "async": "", "watch": "", "node-promise": "", "rmdir": "", " ...

Ways to rotate SVG images exclusively upon clicking

After experimenting with rotating SVG images on my navbar buttons, I encountered an issue. Whenever I click one button, all the images rotate simultaneously. Additionally, clicking elsewhere does not reset the images to their original positions. This is t ...

Efficiently Measuring the Visibility Area of DetailsList in Fluent UI

I am currently utilizing the DetalisList component alongside the ScrollablePane to ensure that the header remains fixed while allowing the rows to scroll. However, I have encountered a challenge as I need to manually specify the height of the scrollable co ...

How can you customize the bottom and label color for Material-UI TextField when in error or when in focus?

I need to customize the color of my Material UI TextField component for the following states: error focused Currently, I am using version 3.8.1 of @material-ui/core and working with the <TextField /> component. I would like to achieve this withou ...

Avoid displaying pop-up repeatedly (using current library)

I am currently customizing a Shopify theme that includes its own popup settings. I have created a new popup for my website that prompts visitors to choose their preferred language upon arrival. Everything seems to be working fine up to this point, but the ...

I am looking for JavaScript or jQuery code that allows me to incorporate a "Save This Page As" button

Is there a way to enhance the saving process for users visiting an HTML page, rather than requiring them to go through File > Save As? I am interested in implementing a "Save Page As" button on the page that would trigger the save as dialog when clicke ...

Connect or disconnect an element to a JavaScript event handler using an anonymous function

I'm stuck on a basic issue... I have a custom metabox in my WordPress blog, and here's my event handler: jQuery('tr input.test').on('click', function( event ){ event.preventDefault(); var index = jQuery(this).close ...

What is the process by which browsers manage AJAX requests when they are made across

I have encountered an issue that is puzzling to me, and I suspect it might be due to my misunderstanding of how the browser handles AJAX requests. Just for context, I am using Codeigniter on an Apache server and triggering AJAX requests with jQuery. The b ...

Dealing with the percentage sign in table names during data retrieval

When using React and Express to retrieve and store data in JSON format, what is the correct way to reference tables that have a percentage sign in their name? componentDidMount() { // Retrieve data from http://localhost:5000/citystats, sort by ID, t ...

Understanding the concept of hoisting in JavaScript for global variables and functions

I've been curious about hoisting. I understand that if a global function shares the same name as a global variable, the function will overwrite the variable's name. Is this correct? Here is an example code snippet. (function() { console.log ...

How is it that connecting an event listener to a React child component can be done in a way that resembles simply passing a prop

class Board extends React.Component { constructor(props) { super(props); this.state = { squares: Array(9).fill(null), }; } handleClick(i) { // do things } render() { return ( <div ...

Showing dynamic content retrieved from MongoDB in a list based on the user's selected option value

Implementing a feature to display MongoDB documents conditionally on a webpage is my current goal. The idea is for the user to choose an option from a select element, which will then filter the displayed documents based on that selection. For instance, if ...

The expression suddenly stopped: nextEvent was not expected to happen

It appears that the issue I am facing is not related to interpolation. Despite removing any instances of interpolation in my code, I am still encountering the same error. The error occurs when I click on an ng-click directive that points to a page referen ...

Exploring Angular2: The Router Event NavigationCancel occurring prior to the resolution of the Route Guard

My application has routes protected by an AuthGuard that implements CanActivate. This guard first checks if the user is logged in and then verifies if certain configuration variables are set before allowing access to the route. If the user is authenticated ...