iScroll 缩放系数计算

iScroll zoom factor calculation

我有一个问题,真的希望有人能帮助我。

JSFiddle:https://jsfiddle.net/EstSiim/zjqptg9j/3/

HTML:

<div id="wrapper">

    <div id="content">

    </div>

</div>

<br><br><br>

<button id="btn">Zoom to view</button>

CSS:

#wrapper {
    width: 500px;
    height: 500px;
    border: 1px solid black; 
    overflow: hidden;
    position: relative;
}

#content {
    width: 700px;
    height: 300px;
    background-color: blue;
}

JavaScript:

var myScroll = new IScroll('#wrapper', {
        mouseWheel: true,
        keyBindings: true,
        wheelAction: 'zoom',
        scrollbars: true,
        scrollX: true,
        scrollY: true,
        zoomMin: 0.1,
        zoom: true, 
        mouseWheelSpeed: 20,
});


var zoomFactor = 0.4;  //need to calculate this somehow

$('#btn').on('click', function() {

    myScroll.zoom(myScroll.scale - zoomFactor);

});

所以基本上我想要的是 iScroll (https://github.com/cubiq/iscroll) 缩小以便显示所有内容。问题是它的内容大小不固定(可以是 700x300、2000x3000、200x900 等等)。

感谢您的宝贵时间

使用这个方法,如果我理解正确你想重置内容,我在两个维度(宽度与高度)上实现了这个,如果你只需要其中一个维度修改适合因子到 return 确切维度的百分比(没有最大值,只选择一个到 return),fiddle here:

//get wrapper dimensions
var wrap = {
    H : $('#wrapper').height(),
    W : $('#wrapper').width()
}
//get content dimensions
var content = {
    H : $('#content').height(),
    W : $('#content').width(),
   };

var myScroll = new IScroll('#wrapper', {
        mouseWheel: true,
        keyBindings: true,
        wheelAction: 'zoom',
        scrollbars: true,
        scrollX: true,
        scrollY: true,
        zoomMin: 0.1,
        zoom: true, 
        mouseWheelSpeed: 20,
});
//compute reset zoom factor
var fitFactor = function ()
{
    //compute witch dimension is larger width vs height
    h = content.H / wrap.H;//zoom factor for height
    w = content.W/ wrap.W;//zoom factor for width
    //get max between zoom factores, remove percents
    renderedFactor = Math.max(h, w);
    //return scale factor
    return  1/renderedFactor;
};

$('#btn').on('click', function() {
    myScroll.zoom(fitFactor());
 });