文档 focus 后不会滚动
问题描述
调用 focus 方法后,文档没有滚动到焦点位置。
原因分析
检查是否设置了 setUIState,其优先级最高,会覆盖 focus 的滚动行为。
解决方案
若希望 focus 时能够滚动,需要在调用 focus 前调整 setUIState 的配置。
示例代码
const Application = await ZOfficeSDK.applications.get('#doc-container');
// 方案1:调用 focus 前先重置 UIState
await Application.setUIState({
scroll: true // 允许滚动
});
// 然后调用 focus
await range.focus();
// 方案2:或者不使用 setUIState,直接调用 focus
await range.focus();
注意事项
setUIState的优先级最高,会影响focus的行为- 如果不需要控制 UI 状态,建议不要使用
setUIState - 如果必须使用
setUIState,确保配置允许滚动
相关示例
// 示例:聚焦到书签位置并滚动
const Application = await ZOfficeSDK.applications.get('#doc-container');
const bookmark = await Application.ActiveDocument.Bookmarks.item('bookmark1');
const range = await bookmark.range;
// 确保允许滚动
await Application.setUIState({
scroll: true
});
// 聚焦到书签位置(会滚动到可见区域)
await range.focus();