1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee
tools/index.html

153 lines
4.6 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>微博热搜</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
overflow: hidden;
}
.container {
display: grid;
grid-template-rows: 1fr 50px;
width: 100%;
height: 100%;
}
.iframe-container,
iframe {
width: 100%;
height: 100%;
}
#float-container.bt {
bottom: 0;
right: 0;
left: 0;
height: 100%;
background-color: #fdfdfd;
box-shadow: 0 -2px 10px 0 rgb(0 0 0 / 10%);
display: grid;
grid-template-columns: repeat(2, 1fr);
place-items: center;
}
#float-container.bt .navbar-item {
text-align: center;
cursor: pointer;
width: 80px;
user-select: none;
}
#float-container.bt .navbar-item .navbar-image {
width: 22px;
height: 22px;
filter: grayscale(1);
}
#float-container.bt .navbar-item .navbar-title {
font-size: 11px;
color: #666;
margin-top: -3px;
}
#float-container.bt .navbar-item.active .navbar-image {
filter: initial;
}
</style>
</head>
<body>
<div class="container">
<div id="iframe-container">
<!-- <iframe src="html/weibo.html" frameborder="0"></iframe> -->
</div>
<div id="float-container" class="bt">
<div class="navbar-item" targetPage="weibo_hotband">
<img class="navbar-image" src="./html/assets/image/weibo.svg" />
<p class="navbar-title">微博</p>
</div>
<div class="navbar-item" targetPage="bilibili_hotband">
<img class="navbar-image" src="./html/assets/image/bilibili.svg" style="transform: scale(0.84);" />
<p class="navbar-title">B站</p>
</div>
</div>
</div>
<script>
let navbarItems = document.querySelectorAll('.navbar-item');
let iframeContainer = document.querySelector('#iframe-container');
let iframe = document.querySelector('iframe');
navbarItems.forEach(item => {
item.addEventListener('click', () => {
// 已选中项再次点击,不执行操作
if(item.classList.contains('active')) return;
navbarItems.forEach(item => {
item.classList.remove('active');
});
item.classList.add('active');
let target = item.getAttribute('targetPage');
switchPage(target, true);
});
});
// 切换页面
// 传入参数:目标页面,是否更新网址
function switchPage(target, pushState) {
let url = `html/${target}.html`;
if (pushState) {
// 更新url但不刷新页面
history.pushState(null, null, `./index.html?target=${target}`);
}
// iframe 进入新页面
if (iframe)
iframe.remove();
iframe = document.createElement('iframe');
iframe.src = url;
iframe.setAttribute('frameborder', '0');
iframeContainer.appendChild(iframe);
}
// 根据 URL 参数切换页面
function locationToPageByUrlParam() {
let pages = ['weibo_hotband', 'bilibili_hotband'];
// 获取 url 中的 target 参数
let url = new URL(window.location.href);
let target = url.searchParams.get('target');
if (!pages.includes(target)) {
target = 'weibo';
history.replaceState(null, null, `./index.html?target=${target}`);
}
// 切换页面
switchPage(target, false);
// 更新下方导航栏选中状态
navbarItems.forEach(item => {
if (item.getAttribute('targetPage') === target) {
item.classList.add('active');
}
});
}
window.addEventListener("popstate", function (e) {
console.log("浏览器返回事件", e);
locationToPageByUrlParam();
}, false);
locationToPageByUrlParam();
</script>
</body>
</html>