Untitled Document

«Zoom» a browser window/view with JavaScript?

We can zoom in and out scrolling with pressing ctrl. But I want to do that using jQuery or JavaScript. Is is possible?

Leave this up to the user. I can’t think of any instance where you should do this, short of a browser plugin.

It would be better to say why you want to detect zoom (it can be done a number of ways, not just using a scroll) since it’s extremely unlikely you can do it reliably across browsers.

@Brad One reason you might want to do it is in case you are displaying a very large organizational chart, and you want the user to be able to zoom in and out of the chart area without zooming the entire page.

@Brad — Leave this up to the programmer, for if you cannot think of any instance, he may —or, event better: can.

4 Answers 4

Since you have tagged in jquery also (though written only js in the question title) I would add this answer

You can add the following css to a webpage to zoom a browser window

Dont worry about any cross-browser since any incomprehensible css property would be avoided by the browser.

Checkout this jsfiddle. Something you may use but it’s not exact function like browser zoom-in zoom-out.

$('#zoom-in').click(function() < updateZoom(0.1); >); $('#zoom-out').click(function() < updateZoom(-0.1); >); zoomLevel = 1; var updateZoom = function(zoom) < zoomLevel += zoom; $('body').css(< zoom: zoomLevel, '-moz-transform': 'scale(' + zoomLevel + ')' >); > 

Here is my solution using CSS transform: scale() and JavaScript / jQuery. This gets pretty close to browser zoom in / out:

alert(window.parent.document.body.style.zoom.toString()); 

and you can set the browser zoom

window.parent.document.body.style.zoom = 1.5; 

Gosh I thought you had it then — unfortunately, I think this is the css zoom property NOT the browser zoom — if you do this the two will oppose each other. For another thing, if you have media breakpoints they won’t trigger for the zoomed sizes like the CTRL+ browser zoom will.

Источник

Imitate Browser Zoom in JavaScript

Imitate Browser Zoom in JavaScript

This article will discuss how to zoom or scale a particular element using the scale method of the transform property in JavaScript.

Imitate Browser Zoom Using the scale Method in JavaScript

The transform property allows you to manipulate the HTML element like rotate, scale, skew or translate and apply 2D or 3D transformations.

There is no harm in using this property since it’s supported on all browsers. The transform property has a scale method that can be used to achieve zoom.

The scale method takes the two values — sx and sy .

The sx specifies how much you want to scale towards the x-axis. And sy sets how much you want to scale towards the y-axis.

Both default values are 1, but the sy value is optional. So, if you don’t define sy , it will take the sx value.

Let’s have an example to see how we can use this property to zoom an HTML element in JavaScript.

Below, we have a div element inside the container with a square ID. This is the element we will apply zoom or the scale property.

Then we also have a div with an ID of btns , which contain all of our buttons. We have added three buttons to our page: zoom in, zoom out, and reset.

Читайте также:  Php отрицательное целое число

The zoom-in button has a class of in , the zoom-out button has out , and the reset button has reset . We added an onclick event of all three buttons, so a function will be called whenever a user clicks on these buttons.

body>  h1>Zoom the below squareh1>  div class="container">  div id="square">div>  div class="btns">  button class="in" onclick="zoomIn()">Zoom Inbutton>  button class="out" onclick="zoomOut()">Zoom Outbutton>  button class="reset" onclick="reset()">Resetbutton>  div>  div>   script src="./script.js">script>  body> 

Inside our CSS, we have just provided basic properties to the square like width, height, background, and margin, and notice that we used em and not px . Utilizing relative units like em and rem is good practice for making responsive designs.

Within the container div, we have two separate div elements. We want them to show side by side, so we used the display: flex property on the container element.

We also want to give some spacing before and after the elements, and for that, we have used flexbox’s justify-content property.

And the align the buttons vertically, we have changed flex-direction to the column. This property has a value of row by default.

You can write the CSS below inside the style tag or create a new file for writing CSS.

#square  width: 10em;  height: 10em;  background-color: tomato;  margin: 4em; >  .container  display: flex; >  .btns  display: flex;  flex-direction: column;  justify-content: space-evenly; > 

Now, it’s time to dive into JavaScript.

First, we have to get the reference of the square which we have created using its ID by the getElementById() method, and then we will store it inside a variable square . To track the zoom in and zoom out value, we will use a variable counter and initialize it to 1.

We set the counter variables value as 1 because the default scale of the HTML element and the scale method is also 1, i.e., 100%. We will be defining three functions zoomIn() , zoomOut() , and reset() inside the JavaScript file.

var square = document.getElementById('square');  var counter = 1;  function zoomIn()   if(counter  2)  counter += 0.1;  square.style.transform = `scale($counter>)`;  > >  function zoomOut()   if(counter > 0.2)  counter -= 0.1;  square.style.transform = `scale($counter>)`;  > >  function reset()  square.style.transform = "scale(1)";  counter = 1; > 

The zoomIn function will be responsible for zooming in the square div element to scale(2) , i.e., 200% maximum. Inside this function, we have the condition that if the value of the counter variable is less than 2, we will increment the counter variable by .1.

Then we will set the transform property, a CSS property on the square using style.transform . To this property, we will assign a value scale() as a string literal and then pass the counter variable inside it as scale($) .

Notice that we have used backticks and not double quotes as a string literal. This allows us to pass a variable inside a string.

The zoomOut() function will be responsible for zooming out the square div element till scale(0.1) , i.e., 10% minimum value.

Here also, we have added a condition that if the value of the counter variable is greater than .2, then only we will decrement the value of the counter variable and set the value of the counter variable to the transform property of the square.

Take note that if you set the scale to zero, then the element will not be visible on the screen, and that’s why we will take the scale value beyond 0.1.

The rest() function will change the scale of the square div element back to its original size scale(1) . Also, don’t forget to set the counter variable’s value back to 1.

If you run the code, it will look like this.

The zoom values will range between 0.1 to 2. By default, it will be set to 1, i.e., 100%.

Another property available in CSS called the zoom property can also be used to implement zoom. But this is not a standard way of implementing zoom on a production website.

This can create issues because some elements and animations on the website might not work as expected.

Sahil is a full-stack developer who loves to build software. He likes to share his knowledge by writing technical articles and helping clients by working with them as freelance software engineer and technical writer on Upwork.

Источник

How To Make Cool Scroll Zoom Effect With Simple JavaScript

How To Make Cool Scroll Zoom Effect With Simple JavaScript

Zoom scroll effects are quite cool to incorporate into your designs. However, for many newbies to frontend development, this can feel like a bit of an execution mystery.

In this tutorial, we are going to go over how to make a cool scroll zoom effect with a few lines of JavaScript and a dash of CSS. This is the write-up part for the video below. Don’t forget to like and subscribe 😉

How scroll zoom works

The idea behind this little effect is simple:

  • if you scroll down, the targeted element is zoomed in.
  • if you scroll up, the targeted element is zoomed out.

To do this, we are going to be using the transform property from CSS and dynamically target it via the scroll event in JavaScript.

It’s pretty basic. Nothing much. Just a container div with the class zoom attached to it. You can call it whatever you want.

Next up is your CSS. We are going to center and fix position your div so it doesn’t run away when we scroll up and down the page. Here is the CSS:

And that is the general basic set up for you HTML and CSS. Now for where the magic happens — the JavaScript.

First, we are going to target your zoom class via document.querySelector .

const zoomElement = document.querySelector(".zoom");

What’s going to happen is this:

  • every time the user scrolls their mouse wheel, the style transform:scale(1); is going to dynamically change.

This means that 1 needs to be represented in some form in our JavaScript. Let’s create a variable called zoom to hold it.

The next part that we need a value that’s going to cause the change. This number is going to be added and deducted to and from zoom . In our example, we are going to call it ZOOM_SPEED .

Now that we have all our necessary values, let’s get onto dealing with the event. To do this, you can use document.addEventListener() . This takes in two parameters — the trigger event and what happens when the trigger occurs.

document.addEventListener(trigger, doSomething());

In our case, our trigger event is the scroll event. So our document.addEvventListener() ends up looking something like this:

document.addEventListener("wheel", function(e)<>);

The e is the callback parameter from the trigger that just occurred and gives you what the state of the page is currently at. Within e , there is a parameter called deltaY , which tracks the current position of the vertical axis of the page. This will come in handy for our zoom effect.

Inside our callback function, we are going add the style transform:scale() to our zoom class div.

It looks something like this:

zoomElement.style.transform = `scale($)`;

What’s happening here is that we are dynamically setting the scale() by using the current zoom value and adding the ZOOM_SPEED to it. So when you scroll down, you are increasing your scale.

To reverse this, you just need to subtract instead of add.

zoomElement.style.transform = `scale($)`;

To trigger these two styles based on what’s happening, you can use an if-else statement. Here is the final JavaScript snippet for the ultra simple scroll zoom effect.

const zoomElement = document.querySelector(".zoom"); let zoom = 1; const ZOOM_SPEED = 0.1; document.addEventListener("wheel", function(e) < if(e.deltaY >0)< zoomElement.style.transform = `scale($)`; >else< zoomElement.style.transform = `scale($)`; > >);

You can also replace the tags with whatever you want. For example, this effect will also work with images.

Here is the HTML code snippet for the above:

 

The CSS and JavaScript remained the same. This is because the effect is set on the container and not on the individual element itself.

And that is it for today. Thank you for making it to the end.

Источник

zooming within a div

I’m creating inside a main div, smaller divs from server side (each div contains data, text and images inside) something like this I want to allow the user to zoom in or out (incase there are more divs then the screen width for example), in a way that the main div will stay with the same size and only the smaller divs will grow or shrink. here’s an example I’ve found that provides the solution I want, but the difference is that:
1. I’m creating the div’s during run time , and according to the data , the number of divs may change (the example present default image and div)
2. I want that ALL the divs will zoom in or out together.

        

press (+) to zoom in
press (-) to zoom out

Источник

scrolling and zooming in a div

I have a particular problem that I need help with. I have a div which contains a lot of other divs which I would like to be able to zoom into. for example, a few of the child divs have text and I’d like the user to be able to zoom in to see what they say. here is an example — > http://jsfiddle.net/du5ye/3/ I’ve though that maybe I could use jquery to change the classes of everything inside, but I’d like to not hard code the new sizes, rather every property in the main wrapper div would get larger by a percentage as the user scrolls his mouse wheel. In addition, I’d like the user to be able to scroll around the div by dragging, quite like google maps. Is this kind of thing possible? and how would I go about it? Here is the entire code for those that would rather avoid jsfiddle;

.layoutGreys < background-color: #DDD; border: solid thin #BBB; padding-left: 10px; padding-right: 10px;; float: left; >.Group < background-color: #955; width: 50px; height: 35px; margin-left: auto; margin-right: auto; margin-top: 10px; margin-bottom: 10px; float: left; >.Divider < width: 10px; height: 35px; float: left; margin-top: 10px; > .wrapper

3 Answers 3

Have you tried the css zoom property? or maybe the css-transforms like scale()?

You can make your outer div overflow: scroll; and bind mouse down events to scroll the div for the pan and drag effect.

* I know this question is old, but It may help future visitors.

Источник

Оцените статью