Skip to content

Prevent zoom on some elements on a page (or whole page), set custom zoom factors to elements with mathematical expressions

License

Notifications You must be signed in to change notification settings

onurkerimov/enhanceZoom.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

enhanceZoom.js

enhanceZoom.js is an extremely small plugin (55 lines uncompressed) that changes zoom behavior of your elements (or whole page). You can use enhanceZoom.js to do a lot of things:

  • Prevent zoom on a page (Force 100% zoom)
  • Prevent zoom on some elements on a page
  • Set different zoom factors on different elements
  • Custom mathematical expressions for zoom behavior
  • Solve 'custom screen DPI' related problems
  • Invert zoom behavior!

How it works

When searching for a method to disable zoom on a page, I have often found solutions that override Ctrl + mousewheel, Ctrl + + or Ctrl + - events. This plugin uses a completely different method. window.devicePixelRatio is a window property that returns the ratio of the resolution in physical pixels to the resolution in CSS pixels for your device. It is supported by all modern browsers. (See here) By using this property, enhanceZoom.js dynamically adds CSS rules to head of your document. Then, by default, it adds a resize listener in order to listen zoom events. When a zoom-in or zoom-out occurs, it updates the CSS rules with the newly computed scaling factors.

Examples

Prevent zoom on a page (See Example)

$.enhanceZoom('body', { factor: 0, stretch: true })

Prevent zoom on some elements

$.enhanceZoom('div#sidebar, span#ads', { factor: 0 })

Set different zoom factors on different elements (See Example)

$.enhanceZoom('div#sidebar, span#ads', { factor: 0.8, stretch: true }) // zooms less than usual
$.enhanceZoom('div#main', { factor: 1 }) // zooms as usual
$.enhanceZoom('ul > li > a', { factor: 1.2 }) // zooms more than usual
$.enhanceZoom('ul > li > a', { factor: -0.5 }) // zooms inverted!

Custom mathematical expressions

$.enhanceZoom('div#sidebar, span#ads', { formula: (r) => Math.abs(r-1)+1 }) 
// This example sets 100% zoom as the minimum possible zoom
// r stands for window.devicePixelRatio

Solve ‘custom screen DPI’ related problems (More on this below)

$.enhanceZoom('div') // When no option is provided, the plugin assumes: factor = 0.99

Invert zoom! (See Example)

$.enhanceZoom('body', { factor: -1, stretch: true }) //setting negative factor inverts

Custom screen DPI related problems

I observed that while in my Chrome settings, zoom value was set to 100%, the value of window.devicePixelRatio was strangely 1.25.

Chrome Settings Devtools Console System Display Settings

It turns out that zoom setting of browser secretly obeys the OS's DPI settings. 100% is not really 100%, Its actually 125%, and 80% is actually 100%, etc. This is the reason behind a lot of problems, such as poor image rendering (this), blurry canvas (this) or from my experience, uneven widths and heights of elements, when they need to be even.

I use this plugin to solve this last problem. Since my pixel ratio is 1.25, when an element is 2 pixels wide, it sometimes get rendered as 2 pixels, sometimes 3, according to its position on the screen. This is extremely annoying for a UI. I discovered that setting transform: scale(0.99); to the desired element's CSS solves this problem. Somehow Chrome renders elements more carefully (?) when CSS scale value is not 1. Also, to compensate, together with transform: scale(0.99);, something like width: 101%; height: 101%; should be used. This plugin does exactly this, when no option object is provided.

License

Licensed under the MIT license.

About

Prevent zoom on some elements on a page (or whole page), set custom zoom factors to elements with mathematical expressions

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published