-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
01b9979
commit 7af9767
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Viewer from 'viewerjs'; | ||
import { nextTick, onMounted, watch } from 'vue'; | ||
import { Route } from 'vitepress'; | ||
|
||
/** | ||
* 给图片添加预览功能 | ||
*/ | ||
const setViewer = (el: string = '.vp-doc img', option?: Viewer.Options) => { | ||
// 默认配置 | ||
const defaultBaseOption: Viewer.Options = { | ||
navbar: false, | ||
title: false, | ||
toolbar: { | ||
zoomIn: 4, | ||
zoomOut: 4, | ||
prev: 4, | ||
next: 4, | ||
reset: 4, | ||
oneToOne: 4 | ||
} | ||
} | ||
document.querySelectorAll(el).forEach((item: Element) => { | ||
(item as HTMLElement).onclick = () => { | ||
const viewer = new Viewer(<HTMLElement>item, { | ||
...defaultBaseOption, | ||
...option, | ||
hide(e) { | ||
viewer.destroy(); | ||
} | ||
}); | ||
viewer.show() | ||
} | ||
(item as HTMLElement).style.cursor = "zoom-in"; | ||
}); | ||
}; | ||
|
||
/** | ||
* set imageViewer | ||
* 设置图片查看器 | ||
* @param route 路由 | ||
* @param el The string corresponding to the CSS selector, the default is ".vp-doc img". | ||
* <br/>CSS选择器对应的字符串,默认为 ".vp-doc img" | ||
* @param option viewerjs options | ||
* <br/>viewerjs 设置选项 | ||
*/ | ||
const imageViewer = (route: Route, el?: string, option?: Viewer.Options) => { | ||
onMounted(() => { | ||
setViewer(el, option); | ||
}) | ||
watch(() => route.path, () => nextTick(() => { | ||
setViewer(el, option); | ||
})); | ||
} | ||
|
||
export default imageViewer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters