缩放

使用SpreadJS,你可以放大或者缩小工作表以获得不同级别的细节 另外,可以使用ViewZooming和ViewZoomed事件来控制缩放级别

缩放操作 Ctrl + 鼠标滚轮 状态栏缩放指示器 双指操作,仅限触摸操作 使用 API sheet.zoom(zoomFactor) allowUserZoom的Spread设置选项 我们提供一个spread选项用以控制可见区域是否可以缩放: 通过API获得或者设置zoom参数 ViewZooming / ViewZoomed 事件 为了干预不同工作表/跨页实例上的缩放过程,我们提供了两个事件。 GC.Spread.Sheets.Events.ViewZooming 处理函数允许使用的参数: sheet: 触发事件的工作表。 sheetName: 工作表的名字。 newZoomFactor: 新的缩放系数, 用户可以做一些操作用以干预真实的zoom系数。 oldZoomFactor: 旧的缩放系数。 cancel: 用以表示是否可以取消操作的值。 GC.Spread.Sheets.Events.ViewZoomed 处理函数中可以使用的参数: sheet: 触发事件的工作表。 sheetName: 工作表的名字。 newZoomFactor: 新的缩放系数, 用户可以做一些操作用以干预真实的zoom系数。 oldZoomFactor: 旧的缩放系数。
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); initStatusBar(spread); initSpread(spread); initEvents(spread); }; function initStatusBar(spread) { var statusBarDOM = document.getElementById('statusBar'); var statusBar = new GC.Spread.Sheets.StatusBar.StatusBar(statusBarDOM); statusBar.bind(spread); } function initSpread(spread) { var sd = dataSource; var sheet = spread.getActiveSheet(); if (sd.length > 0) { sheet.setDataSource(sd); } sheet.setColumnWidth(0, 160); sheet.setColumnWidth(1, 70); sheet.setColumnWidth(2, 90); sheet.setColumnWidth(3, 110); sheet.setColumnWidth(4, 80); sheet.setColumnWidth(6, 110); } function initEvents(spread) { var zoomFactorDOM = document.getElementById('zoomFactor'); var limitSwitch = document.getElementById('useLimit'); var maxZoomDOM = document.getElementById('maxZoom'); var minZoomDOM = document.getElementById('minZoom'); zoomFactorDOM.addEventListener('input', function() { var sheet = spread.getActiveSheet(); if (sheet) { var zoomFactor = parseFloat(this.value); var useLimit = limitSwitch.checked; var maxZoomFactor = parseFloat(maxZoomDOM.value), minZoomFactor = parseFloat(minZoomDOM.value); if (useLimit) { if (zoomFactor >= maxZoomFactor) { zoomFactor = maxZoomFactor; } if (zoomFactor <= minZoomFactor) { zoomFactor = minZoomFactor; } } zoomFactorDOM.value = zoomFactor; sheet.zoom(zoomFactor); } }); limitSwitch.addEventListener('change', syncDOM); spread.bind(GC.Spread.Sheets.Events.ViewZooming, function (e, args) { var useLimit = limitSwitch.checked; var maxZoomFactor = parseFloat(maxZoomDOM.value), minZoomFactor = parseFloat(minZoomDOM.value); if (useLimit) { if (args.newZoomFactor >= maxZoomFactor) { args.newZoomFactor = maxZoomFactor; } if (args.newZoomFactor <= minZoomFactor) { args.newZoomFactor = minZoomFactor; } } zoomFactorDOM.value = args.newZoomFactor; }); syncDOM(); } function syncDOM() { var limitSwitch = document.getElementById('useLimit'); var maxZoomDOM = document.getElementById('maxZoom'); var minZoomDOM = document.getElementById('minZoom'); var maxZoomParentDOM = maxZoomDOM.parentNode; var minZoomParentDOM = minZoomDOM.parentNode; var useLimit = limitSwitch.checked; if (useLimit) { maxZoomParentDOM.classList.remove('hide'); minZoomParentDOM.classList.remove('hide'); } else { maxZoomParentDOM.classList.add('hide'); minZoomParentDOM.classList.add('hide'); } }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div class="spread-container"> <div id="ss" class="sample-spreadsheets"></div> <div id="statusBar"></div> </div> <div class="options-container"> <div class="option-row"> <label>Slide to zoom active sheet:</label> <div class="input"> <label class="inline-label">0.25</label> <input id="zoomFactor" type="range" name="points" min="0.25" max="4" value="1" step="0.01"/> <label class="inline-label">4</label> </div> </div> <div class="option-row"> <input id="useLimit" type="checkbox" /> <label for="useLimit" class="inline-label">Limit the zoom factor</label> </div> <div class="option-row"> <label for="maxZoom">The max zoom factor:</label> <input id="maxZoom" type="number" min="0.25" max="4" value="4" step="0.1" /> </div> <div class="option-row"> <label for="minZoom">The min zoom factor:</label> <input id="minZoom" type="number" min="0.25" max="4" value="0.25" step="0.1" /> </div> </div> </div></body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .spread-container { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .sample-spreadsheets { width: 100%; height: calc(100% - 30px); } #statusBar { width: 100%; height: 30px; } .inline-label { display: inline-block; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } label { display: block; margin-bottom: 6px; } input[type=button] { margin-top: 6px; display: block; padding: 4px 6px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .hide { display: none !important; } .inputbox input { display: inline-block; }