Skip to content

Commit 512365f

Browse files
committed
feat: 添加搜索框
1 parent 68a5059 commit 512365f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

css/app.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ body {
105105
font-size: 0.7rem;
106106
}
107107

108+
input[type=search] {
109+
display: block;
110+
width: 180px;
111+
text-align: left;
112+
}
113+
108114
#content {
109115
padding-top: 10px;
110116
padding-bottom: 150px;

js/ditto.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var ditto = {
1212
edit_button: true,
1313
back_to_top_button: true,
1414
save_progress: true, // 保存阅读进度
15+
search_bar: true,
1516

1617
// initialize function
1718
run: initialize
@@ -40,8 +41,13 @@ function initialize() {
4041
}
4142

4243
function init_sidebar_section() {
43-
$.get(ditto.sidebar_file, function(data) {
44+
$.get(ditto.sidebar_file, function (data) {
4445
$(ditto.sidebar_id).html(marked(data));
46+
47+
if (ditto.search_bar) {
48+
init_searchbar();
49+
}
50+
4551
// 初始化内容数组
4652
var menuOL = $(ditto.sidebar_id + ' ol');
4753
menuOL.attr('start', 0);
@@ -66,9 +72,25 @@ function init_sidebar_section() {
6672
}, "text").fail(function() {
6773
alert("Opps! can't find the sidebar file to display!");
6874
});
75+
}
6976

77+
function init_searchbar() {
78+
var search = '<input name="search" type="search">';
79+
$(ditto.sidebar_id).find('h2').first().before($(search));
80+
$('input[name=search]').keydown(searchbar_listener);
7081
}
7182

83+
function searchbar_listener(event) {
84+
if (event.which === 13) {
85+
var q = $('input[name=search]').val();
86+
if (q !== '') {
87+
var url = 'https://github.com/ruanyf/es6tutorial/search?utf8=✓&q=' + encodeURIComponent(q);
88+
location.href = url;
89+
}
90+
}
91+
}
92+
93+
7294
function init_back_to_top_button() {
7395
$(ditto.back_to_top_id).show();
7496
$(ditto.back_to_top_id).on('click', goTop);

0 commit comments

Comments
 (0)