Tips on enabling zooming for resizing an image and initially sizing it using vh/vw units

My issue involves having an extensive amount of code spread across various components (I'm using vue.js). Although I can't share all the code efficiently, I'll do my best to explain the situation clearly:

I have a div that contains an image. The width of this div is set in viewport width (vw) units to ensure it fits the browser's width without overflowing initially.

However, I want users to be able to zoom in on the image, causing it to overflow horizontally. My challenge lies in finding a way to enable zoom functionality while keeping the width formatting in vw units.

It is crucial for me to maintain the width in vw units because I intend for the picture, which is initially at 100% zoom, to resize and fit the browser's width when resized by the user.

Is there a method to set an image's width based on vw units yet still allow zooming capabilities?

Answer №1

To maintain the div at 100vw continuously, you can set the initial width of the image to 100% and then adjust only the image's zoom level. This setup ensures that if the window is resized, the containing div will resize to fit the window width while maintaining the image's zoom level.

Below is a straightforward example that allows zooming by either clicking buttons or using methods like a mouse wheel:

<head>
<style>
#mydiv {
  width : 100vw;
}
#mydiv div.buttons {
  position:absolute;
  z-index:10;
}
</style>
<script>
function zoom() {//this function called from onwheel event on the image
  event.preventDefault();  
  if (event.deltaY<0) { zoomIn(); }
  else { zoomOut(); }
}

//this is the original code which scales on Zoom in and Zoom out buttons
function zoomIn() {
  var w=Number(myimg.style.width.replace('%',''));
  if ((w <= (maxsteps*step + 100))) {
    myimg.style.width=(w+step) + '%';
  }
}
function zoomOut() {
  var w=Number(myimg.style.width.replace('%',''));
  if (w > step) {
    myimg.style.width=(w-step) + '%';
  }
}
</script>
</head>
<body>
<div id="mydiv">
  <div class="buttons">
    <button onclick="zoomIn();">Zoom in</button>
    <button onclick="zoomOut();">Zoom out</button>
  </div>
    <img id="myimg" src="https://rgspaces.org.uk/wp-content/uploads/junction-arch-and-tricycle-and-box-1024x768.jpg" style="width:100%;"/>
</div>
<script>
const myimg = document.getElementById('myimg');
const step = 10;//or however many vw (=% of div width) you want the div to zoom on a click
const maxsteps = 10;//the maximum number of zoom steps you allow the user to take

myimg.addEventListener("wheel", zoom);

</script>
</body>

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

The function "process" is not defined within the Vue.js framework

When attempting to utilize process.env.process.env.VUE_APP_SERVER_URL in my Vue project, I encountered the following error: Uncaught ReferenceError: process is not defined This project is built with webpack and executed using the npm start command. ...

How to transform the bootstrap 4 hamburger icon into a three dots menu icon using css only

The standard bootstrap 4 navigation bar button showcases a classic hamburger icon. <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample03" aria-controls="navbarsExample03" aria-expanded="false" aria-label="T ...

Tips for personalizing the color, font style, background color, and alignment of the WhatsApp share button I designed using HTML

Currently, I am working on adding a WhatsApp share button to a webpage. I would like to customize it using some CSS code. Could someone kindly provide me with the code for customizing this button? <a href="whatsapp://send?text=https://vcard.score ...

No changes occur within this JavaScript code

I am currently working on a piece of Java Script code: document.onreadystateChange = function() { if (document.readystate === "complete") { var menu = document.getElementsByClassName('menu'); var b0 = menu[0]; b0.addE ...

How to turn off span text in AngularJS

What is the best way to deactivate the text within a span using angularjs? I'm having trouble with ng-disabled in my situation. Below is the code for the span element: <span ng-click="clickMe()" ng-if="true" ng-disabled="true">Click Me!</sp ...

Why is it possible to enter the invalid input `1-1` into an `<input type="number">` field in Chrome, but not `1-e`?

When using an <input type="number"> in Chrome, I discovered a peculiar behavior: I can type 11, Then position the cursor between the two 1s, And finally type -. However, when attempting to input the following, it doesn't work: Type ...

Retrieve the div element by calling a scriptlet with JavaScript

I am facing an issue with a web page that includes a scriptlet like this: <div id="flash_chart"> <%=content_data['report_text']%> </div> The variable content_data['report_text'] contains a lengthy string ...

Creating a custom transition rule in Stylus: A step-by-step guide

I need help creating a generic transition rule in Stylus. My current function is only working in specific cases, where it returns a string 'all ease-in-out 0.2s' instead of a proper CSS rule. This approach is not effective when trying to use it i ...

Achieving a shadow effect above a CSS border: tips and tricks

<div class="row"> some content <div class="info-box"> some other content </div> </div> .row { float: left; margin-bottom: 1.5%; border: 1px solid #e3e3e3; -webkit-border-radius: 4px; -moz-border ...

Populate the form fields with the values from the database when editing

My form includes a checkbox list where users can select and save data into the database, as well as edit the data. Within the checkbox list, there is an option named "Other." If a user checks this box, a textbox appears for them to enter an alternative va ...

Using HTML to design interactive buttons that can send API requests and display their current status

Seeking assistance with creating buttons to control a remote light's ON and OFF states while reflecting their status as well. The specific http API calls for the light are necessary to toggle its state. Turn On = http://192.168.102.22:3480/data_requ ...

The .fixed-top navbar in Bootstrap conceals various elements behind it

Hey there, I'm hoping you're doing well. I've run into a little issue with Bootstrap that I was hoping to get some help with. According to the Bootstrap documentation, it seems like .sticky-top may not be supported by some browsers anymore. ...

What is the reason behind the jQuery .after() method inserting a row that is cramming itself into one cell?

I'm attempting to dynamically add cells to the right side of a table using jQuery when a row is clicked. I have successfully implemented inserting a new row on click, which also toggles its visibility with CSS. However, I am encountering an issue whe ...

Vue and Mocha: No tests pass when a Vue single file contains a <style> tag

I am currently integrating VueJS into my Ruby on Rails application using webpacker and the vueonrails gem. However, I am encountering difficulties when it comes to testing the Vue implementation. Within my project, I have a collection of .vue files that f ...

Having trouble iterating through an array of objects in Vue?

Having trouble looping through an array of objects in Vue3 <template> <div class="shadow-xl w-96 h-96 md:h-[600px] md:w-[600px] lg:my-12 lg:w-[700px] lg:h-[700px] rounded-md" > <button @click="getData">Get ...

What is the best method to align subtitles in an HTML5 video player? Tips for enhancing the style of subtitles/captions

I am facing an issue where the subtitles in my video do not play in the middle-bottom as desired. I have tried to style them using CSS but they just won't align to the center. The subtitles always appear at the bottom left of the video. Here is the H ...

How to obtain the full path of a file downloaded using a Chrome extension

Currently in the process of creating a chrome extension that has the functionality to download specific files from various webpages. For this purpose, I have designed a popup.html where users can input the desired name for the file to be downloaded. Additi ...

What are some ways to recycle HTML User Interface components?

Here is the HTML code snippet I am working with: <div id="addFieldUI" style="display: none"> <div> Field Name: <input type="text" /> </div> <div> Field Value </div> <div> Data Type: <select& ...

struggling to incorporate API Json response data into my HTML file using AngularJS

Below is the code I am currently using: angular.module('ngApp', []) .factory('authInterceptor', authInterceptor) .constant('API', 'http://myserver.com/app/api') .controller('task', taskData) function task ...

Issues with JavaScript scripts functionality on my live server

Hey everyone, I'm new to this community and having some trouble with my index.html file. Everything works fine except for one script that only runs when I open the file directly with live server in VSCode. Can someone help me out? The script causing i ...