简体   繁体   English

JavaScript/HTML/CSS:缩放

[英]JavaScript/HTML/CSS: Zooming

Let's say I've got a page with a div, and a button.假设我有一个带有 div 和一个按钮的页面。 When you click the button, the div should be zoomed in on.当您单击按钮时,应该放大 div。 In other words, if that div was 100px, when you zoom, it should then become, say, 200px.换句话说,如果那个 div 是 100px,当你缩放时,它应该变成 200px。 And all the children of this div should also be doubled in size.并且这个 div 的所有子元素的大小也应该加倍。

What's the best way to do this?最好的方法是什么?

My understanding is that there's a CSS zoom , but only in IE--it's not part of any CSS standard.我的理解是有一个 CSS zoom ,但仅在 IE 中——它不是任何 CSS 标准的一部分。

You should use CSS3'stransform: scale() .您应该使用 CSS3 的transform: scale()

See: http://jsfiddle.net/Favaw/ - (I used jQuery for convenience, but it's not a requirement)请参阅: http://jsfiddle.net/Favaw/ - (为方便起见,我使用了 jQuery,但这不是必需的)

.zoomedIn2x {
    -moz-transform: scale(2);
    -webkit-transform: scale(2);
    -o-transform: scale(2);
    -ms-transform: scale(2);
    transform: scale(2);

    -moz-transform-origin: 0 0;
    -webkit-transform-origin: 0 0;
    -o-transform-origin: 0 0;
    -ms-transform-origin: 0 0;
    transform-origin: 0 0;
}

If you need to support older versions of IE than 9, then generate .zoomedIn2x using this tool:如果您需要支持比 9 更旧的 IE 版本,请使用此工具生成.zoomedIn2x

If you want to do this more dynamically (such as other levels of zoom), then instead use cssSandpaper .如果您想更动态地执行此操作(例如其他级别的缩放),请改用cssSandpaper

You might want to look into the jQuery plugin Zoomooz: http://janne.aukia.com/zoomooz/您可能想查看 jQuery 插件 Zoomooz: http://janne.aukia.com/zoomooz/

best solution is using zoom: 50%最好的解决方案是使用zoom: 50%

i made this example with javascript, you can test it and change it as you like我用 javascript 做了这个例子,你可以测试它并随意改变它

 var zoomediv = document.getElementById('zoomediv') var zoomin_button = document.querySelector('#zoomin') zoomin_button.addEventListener('click', function(){ zoomediv.style.zoom = '125%' }) var zoomout_button = document.querySelector('#zoomout') zoomout_button.addEventListener('click', () => { zoomediv.style.zoom = '75%' })
 div { background: #f0f0f0; border: 1px solid #e0e0e0; width: fit-content; padding: 1rem; margin-bottom: 1rem; } button { padding: 0 3rem; }
 <div id="zoomediv"> <h1> Title </h1> <p> this is a paragraph </p> </div> <button id="zoomin"> <h1> + </h1> </button> <button id="zoomout"> <h1> - </h1> </button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM