[英]Increasing and decreasing scale of the page with jQuery
I've created two buttons in my html code 我在html代码中创建了两个按钮
<a href="#" class="increase">+</a>
<a href="#" class="decrease">-</a>
what i want to do is to click in the buttons and scale the page 我想要做的是单击按钮并缩放页面
i developed a jquery script, but it only works on chrome 我开发了一个jQuery脚本,但仅适用于chrome
$(".decrease").click(function () {
$("body").css('zoom',function (index,value) {
if(value < 0.8){
return value;
} else {
return parseFloat(value) - 0.1;
}
});
});
$(".increase").click(function () {
$("body").css('zoom',function (index,value) {
if(value > 1.2){
return value;
} else {
return parseFloat(value) - 0.1;
}
});
});
can anyone help me? 谁能帮我?
thanks 谢谢
See here jsfiddle - http://jsfiddle.net/1totyrhj/ 看到这里jsfiddle- http://jsfiddle.net/1totyrhj/
This variant will work in all modern browsers. 此变体将在所有现代浏览器中运行。
JavaScript JavaScript的
(function () {
var currentScale = 1,
cssPrefixesMap = [
'scale',
'-webkit-transform',
'-moz-transform',
'-ms-transform',
'-o-transform',
'transform'
];
function setScale(scale) {
var scaleCss = {};
cssPrefixesMap.forEach(function (prefix) {
scaleCss[prefix] = 'scale(' + scale + ')';
});
$('div').css(scaleCss);
}
$(".decrease").click(function () {
setScale(currentScale = currentScale - 0.1);
});
$(".increase").click(function () {
setScale(currentScale = currentScale + 0.1);
});
})();
There's a load of ways browsers handle CSS zooming, for example when doing a x2 zoom: 浏览器处理CSS缩放的方式很多,例如进行x2缩放时:
zoom: 2; /* IE */
-moz-transform: scale(2); /* Firefox */
-moz-transform-origin: 0 0;
-o-transform: scale(2); /* Opera */
-o-transform-origin: 0 0;
-webkit-transform: scale(2); /* Safari And Chrome */
-webkit-transform-origin: 0 0;
transform: scale(2); /* Standard Property */
transform-origin: 0 0; /* Standard Property */
You'll need a way to handle them all. 您将需要一种处理所有内容的方法。 (Property list shamelessly stolen from this thread: complete styles for cross browser CSS zoom )
(该列表从此线程被无耻地窃取了: 跨浏览器CSS缩放的完整样式 )
Change your class for an ID, and then try this out : 更改您的班级以获取ID,然后尝试以下操作:
var currFFZoom = 1;
var currIEZoom = 100;
$('#increase').on('click',function(){
if ($.browser.mozilla){
if(currFFZoom < 1.2)
{
var step = 0.2;
currFFZoom += step;
$('body').css('MozTransform','scale(' + currFFZoom + ')');
}
} else {
if(currIEZoom < 120)
{
var step = 20;
currIEZoom += step;
$('body').css('zoom', ' ' + currIEZoom + '%');
}}
});
$('#decrease').on('click',function(){
if ($.browser.mozilla){
if(currFFZoom > 0.8)
{
var step = 0.2;
currFFZoom -= step;
$('body').css('MozTransform','scale(' + currFFZoom + ')'); }
} else {
if(currIEZoom > 80)
{
var step = 20;
currIEZoom -= step;
$('body').css('zoom', ' ' + currIEZoom + '%');
}
}
});
EDIT : Adapted to block after a certain amount of scale (Min/max). 编辑:适应一定比例(最小/最大)后的块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.