Prechádzať zdrojové kódy

支持键盘左右方向键进行翻页

sunyj 9 rokov pred
rodič
commit
5429c60894

+ 1 - 3
src/main/webapp/WEB-INF/views/preview2.html

@@ -2,13 +2,12 @@
 <html>
 <head>
 <meta charset="UTF-8">
-<title>Preview2</title>
+<title>Preview</title>
 <link rel="stylesheet" href="static/css/preview2.css">
 <link rel="stylesheet"
 	href="static/lib/fontawesome/css/font-awesome.min.css">
 </head>
 <body>
-
 	<div id="toolbarContainer">
 		<div id="toolbarViewerLeft">
 			<button id="prev" title="上页">
@@ -21,7 +20,6 @@
 				id="page_size"></span></span>
 		</div>
 
-
 		<div id="toolbarViewerRight">
 			<button id="print" title="打印">
 				<i class="fa fa-print fa-lg" aria-hidden="true"></i>

+ 41 - 22
src/main/webapp/resources/js/preview2/app.js

@@ -23,32 +23,13 @@ if (printType && printType == 'PRINT') {
 }
 
 // 上页
-$("#prev").click(function() {
-	if (pageIndex <= 1) {
-		return;
-	}
-	// 获取前一页的pdf
-	url = url.replace(pageIndex + ".pdf", (--pageIndex) + ".pdf");
-	getDocument();
-});
+$("#prev").click(prevPage);
 
 // 下页
-$("#next").click(function() {
-	if (pageIndex >= pageSize) {
-		return;
-	}
-	url = url.replace(pageIndex + ".pdf", (++pageIndex) + ".pdf");
-	getDocument();
-});
-
-// 打印
-$("#print").click(function() {
-	hiddenframe.contentWindow.print();
-});
+$("#next").click(nextPage);
 
 // 手动输入页码
-$("#page_index").keypress(function(e) {
-	var event = e || window.event;
+$("#page_index").keypress(function(event) {
 	// 按Enter键
 	if (event.keyCode == 13) {
 		var value = document.getElementById("page_index").value;
@@ -67,6 +48,11 @@ $("#page_index").keypress(function(e) {
 	}
 });
 
+// 打印
+$("#print").click(function() {
+	hiddenframe.contentWindow.print();
+});
+
 // 下载pdf
 $("#download_pdf").click(function() {
 	window.location = downloadUrl("pdf");
@@ -77,6 +63,20 @@ $("#download_excel_with_only_data").click(function() {
 	window.location = downloadUrl("xls_with_only_data");
 });
 
+// 键盘左右键进行翻页
+$("body").keydown(function(event) {
+	// 如果在input输入框内按左右键,不进行翻页
+	var activeElement = document.activeElement;
+	if (activeElement.nodeName.toLowerCase() == "input") {
+		return;
+	}
+	if (event.keyCode == 37) {// left
+		prevPage();
+	} else if (event.keyCode == 39) {// right
+		nextPage();
+	}
+});
+
 // 获取窗口宽度
 function getWindowWidth() {
 	if (window.innerWidth)
@@ -163,4 +163,23 @@ function renderPage() {
 		};
 		var renderTask = page.render(renderContext);
 	});
+}
+
+// 预览前一页
+function prevPage() {
+	if (pageIndex <= 1) {
+		return;
+	}
+	// 获取前一页的pdf
+	url = url.replace(pageIndex + ".pdf", (--pageIndex) + ".pdf");
+	getDocument();
+}
+
+// 预览后一页
+function nextPage() {
+	if (pageIndex >= pageSize) {
+		return;
+	}
+	url = url.replace(pageIndex + ".pdf", (++pageIndex) + ".pdf");
+	getDocument();
 }