Utilizing JQuery UI range slide value to adjust image zoom levels

I'm currently working on a project where I want to implement zoom in and out functionality for an image using a JQuery UI slider. However, I'm facing two main issues that need fixing. Firstly, I need the image to stay centered while it zooms in and out. Secondly, there are instances when the image scales smaller instead of larger due to mathematical calculation errors. How can I resolve these issues effectively?

You can view the code here.

Here's the HTML:

<div class="zoom-me">
   <img src="http://theartmad.com/wp-content/uploads/Happy-Fairy-Tail-Wallpaper-5.jpg">
  </div>
</div>  
<div id="range5"></div>

Here's the CSS:

img {
   position:absolute;
    left:0;
    right:0;
    margin-left:auto;
    margin-right:auto;
  width:200%;

}
.zoom-me {
  background-color: blue;
    overflow: hidden;
    border: 10px solid white;
    max-width: 400px;
    width: 400px;
    position: relative; 
    display: block;
    margin: 0 auto; 
    height: 260px;
}
#range5 {
  position: absolute;
  width: 300px;
  top: 75%;
  left: 20%;
  -webkit-transform: translate(-50%, -50%);
  -moz-transform: translate(-50%, -50%);
  -o-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  border: 0;
  height: 13px;
  -webkit-border-radius: 0%;
  border-radius: 0%;
  background: #e6e7e8;
  outline: none;
}
#range5 .ui-slider-handle{
  position: absolute;
  margin: -18px 0 0 -9px;
  -webkit-border-radius: 0%;
  border-radius: 0%;
  background: #0B5788;
  border: 0;
  height: 20px;
  width: 11px;
  outline: none;
  cursor: pointer;
}
#range5 .ui-slider-range{
  background: #0d78bd;
  height: 13px; 
}

And here's the JS:

$('#range5').slider({
    range: 'min',
    max: 500,
    step: 100,
    value: 50,
    slide: function (e, ui) {
        var cv = $('#range5').slider( "value" );
      $('img').animate({
       width: 200 - cv/5 + "%",
       height: 200 - cv/5 + "%"},500);

    } 
});

Answer №1

The reason your image shrinks instead of grows is due to the following code lines:

width: 200 - cv/5 + "%",
height: 200 - cv/5 + "%"},500);

With a maximum slider value of 500, it means that cv will never exceed 500. Dividing 500 by 5 gives 100. Subtracting 100 from 200 results in a maximum zoom level of 100%.

To keep the image centered, you can either apply some CSS techniques (more complex but cleaner) or animate the image's position as well (simpler solution).

Answer №2

Check out this GitHub link for the jQuery Panzoom plugin here are some working demos View the demos here

<section id="panzoom-section">
      <div class="parent">
        <div class="panzoom">
          <img src="image-src" width="900" height="900">
        </div>
      </div>
      <div class="buttons">
        <input type="range" class="zoom-range">
      </div>
      <script>
        (function() {
          var $section = $('#panzoom-section');
          $section.find('.panzoom').panzoom({
            $zoomRange: $section.find(".zoom-range"),
          });
        })();
      </script>
</section>

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

Troubleshooting: Unable to retrieve all checked values using jQuery

Can someone please assist me with why it is returning an empty array? Thank you in advance. Feel free to check out the code at . Sample Code : function search(){ var pettype = []; $(".pettype :checked").each(function(){ alert($(this).val() ...

Debating the use of cameras in Three.js

Creating a camera in code typically looks like this: var camera = new THREE.PerspectiveCamera( FOV, ASPECT_RATIO, NEAR_PLANE, FAR_PLANE ); But what exactly do these values represent? ...

Utilizing ngModel on input elements inside a custom directive, ensuring compatibility with other ng-* attributes

In my current project, I am working on developing a custom directive that acts as a wrapper around an input field. The main purpose of this directive is to simplify formatting, encapsulate animations, and enhance overall functionality. One of my goals for ...

My div is currently being concealed by a jQuery script that is hiding all of its

Below is the current code snippet: jQuery(document).ready(function($) { $("ul.accordion-section-content li[id*='layers-builder'] button.add-new-widget").click(function() { $("#available-widgets-list div:not([id*='layers-widget']) ...

Identifying Inaccurate Device Date Using JavaScript

Is there a way to detect if the device's date is inaccurate using javascript? (For example, displaying an alert if the current date is 2016/6/16 but the device date is 2016/6/15) ...

Scroll bar design that seamlessly integrates with text wrapping when hovered over

In many of the pages on my blog, I have a structure that consists of boxes with a link like this: http://jsfiddle.net/gliu/E32Bh/ The issue I'm facing is that when too much text is added to these boxes, a vertical scrollbar appears only upon hoverin ...

What is the method for incorporating an async middleware into a router route?

I am currently setting up routes for an express router in my project. I have a validator that deals with promises, requiring me to use await in the code. Here is how it looks: constructor() { this.router = express.Router(); this.router.use(express. ...

The div is inexplicably shifting downward once the first three rows are shown correctly

After displaying the first 3 rows properly, the div mysteriously moves down. Could I be making a silly mistake? All images have the same width and height. Please see the attached image for reference. <?php // start first row echo "<hr class='m ...

Locate an array object using its unique ID and render it in a React component

Attempting to retrieve the object vorlagen with ID 6310 from this API, and display whether the property vorlageAngenommen is true or false. The JSON object appears as follows: Here's my code snippet which is currently not functioning properly: impor ...

Vue.js causing issues with jQuery data table

Utilizing jQuery data-table within Vue.js in a laravel project has presented an issue for me. Although the data loads successfully into the data-table, there seems to be a problem with retrieving the data after it has been loaded. Specifically, the first r ...

Python script using Selenium to only print elements in an HTML page that have a specific class value and print only certain elements based on

I am currently developing a script to automatically test all the free proxies available on The website contains a comprehensive list of all available proxies, and I have successfully made my script extract and display them. However, I only want to display ...

Using Jquery val() will consistently retrieve the value of the first selected option, regardless of which one is

Utilizing Jquery to enable users to upload images via Ajax in a form, with the image uploading before form submission. A watermark feature has been incorporated into each uploaded image (handled by upload_image.php) and this aspect is functioning correctly ...

When using res.render to send results, redundant lines are displayed

I feel like I must be missing something really obvious, but for the life of me I cannot figure out what it is. All I want to do is list the documents in a MongoDB collection in a straightforward manner. I am working with nodejs, mongoose, and Jade (althoug ...

Invoke the click handlers consecutively following the initial click handler using ajax requests

I have a button on my webpage that triggers multiple click events. The button code looks like this: <input type="button" id="myButton"/> There are several functions bound to the button's click event using jQuery: $("#mybutton").on("click", f ...

Register form using NodeJs with MongoDB database and Mongoose, integrating Express and passport

I've set up a signup form with fields for username, password, and email. I'm using MongoDB to store this data, and here's my schema: var mongoose = require("mongoose"); var Schema = mongoose.Schema; const passportLocalMongoose = require("pa ...

Is there a way to run JavaScript using Selenium and extract information at the same time?

Greetings and thank you for taking the time to read this. I am currently working on a parser that scans hundreds of websites to check if they have a specific module or plugin installed. The main challenge I am facing is determining whether a website utili ...

Closing the Bootstrap 5 Navbar on Click Event

I'm not very familiar with Bootstrap and I came across this code online that involves javascript in html, which I don't fully understand. I want to make the hamburger menu close automatically after selecting an item in the navigation bar, especia ...

Is it possible to generate a vendor bundle for CSS/Sass in Vue.js CLI 3?

I am currently using @vue/cli 3.x and I made some changes to my vue.config.js. I am looking to have separate CSS files like app.css and vendor.css (compiled from Sass) - similar to how it handles JavaScript. However, I am unsure about the correct configura ...

Sorting complex strings in Typescript based on the dates contained within them

How can I sort a list of 2 strings with dates inside them so that the earlier one always comes first? The date is always after the second comma. For example, const example = ["AAA,5,2020-09-17T21:14:09.0545516Z", "AAA,0,2020-09-03T20:38:08. ...

Is there a way to identify when the user scrolls within the Gmail interface?

Currently, I am attempting to comprehend how Gmail detects scroll events by utilizing an iframe to encapsulate the main content/interface and then employing JavaScript to generate their scrollbar. Even though I am using jQuery, I have not yet succeeded in ...