forked from sz-games/sz-games.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
30 lines (26 loc) · 1.07 KB
/
search.js
File metadata and controls
30 lines (26 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
document.addEventListener('DOMContentLoaded', function () {
const searchBox = document.getElementById('SearchBox')
if (searchBox) {
// Add event listener for the keyboard combination Ctrl/Cmd + K
document.addEventListener('keydown', function (event) {
// Check if Ctrl key (Windows/Linux) or Cmd key (Mac) is pressed along with K
if ((event.ctrlKey || event.metaKey) && event.key === 'k') {
// Prevent the default browser behavior
event.preventDefault()
// Get the position of the search box
const rect = searchBox.getBoundingClientRect()
const scrollTop = window.pageYOffset || document.documentElement.scrollTop
const targetPosition = scrollTop + rect.top - 200 // 200px offset from the top
// Smoothly scroll to the search box with offset
window.scrollTo({
top: targetPosition,
behavior: 'smooth',
})
// Add a small delay before focusing to ensure the scroll completes
setTimeout(function () {
searchBox.focus()
}, 500)
}
})
}
})