iScroll缩放系数计算

时间:2015-07-30 10:31:58

标签: javascript iscroll

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

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等)。

感谢您的时间

1 个答案:

答案 0 :(得分:0)

使用此方法,如果我理解你想要重置内容,我在两个维度(宽度与高度)上实现了这个,如果你只需要其中一个修改拟合因子来返回维度的确切百分比(没有最大值)只需选择一个返回),摆弄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());
 });