我们制作交互功能时,为了提高用户体验会考虑常规套路以外的情况。例如,手机导航点开后,并不一定要点击关闭按钮关闭导航,点击导航外的空白地方也可以实现导航的关闭。那么要如何实现呢?请看视频示例:
源码如下:
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="十分钟课堂">
<title>点击对象外的其它空间</title>
<style>
html {
--font-color: #7aabd6;
--hover-color: rgba(36, 97, 151, 0.2);
--background-color: #246197;
font-family: 'Courier New', Courier, monospace;
font-size: 15px;
}
*,
:before,
:after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
::selection {
color: var(--background-color);
background-color: var(--font-color);
}
body {
min-height: 100vh;
}
.toggle-btn {
position: fixed;
top: 10px;
right: 10px;
z-index: 5;
width: 40px;
height: 40px;
cursor: pointer;
transition: background-color 0.2s linear 0s;
border-radius: 100em;
}
.toggle-btn:hover {
background-color: var(--hover-color);
}
.toggle-btn:before,
.toggle-btn:after {
content: "";
display: block;
width: 50%;
height: 3px;
background-color: var(--background-color);
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -5px);
transition: transform 0.4s cubic-bezier(.88, .22, .17, .98) 0s, background-color 0.4s cubic-bezier(.88, .22, .17, .98) 0s;
}
.toggle-btn:after {
transform: translate(-50%, 2px);
}
.mobile-navigation {
color: var(--font-color);
width: 200px;
height: 100%;
position: fixed;
top: 0;
right: 0;
background-color: var(--background-color);
z-index: 4;
padding: 100px 20px 30px;
text-align: center;
transform: translateX(101%);
pointer-events: none;
opacity: 0;
transition: transform 0.3s cubic-bezier(.56, .15, .35, .99) 0s, opacity 0s linear 0.3s, pointer-events 0s linear 0.3s;
}
.show-mobile-navigation .mobile-navigation {
transform: translateX(0);
pointer-events: unset;
opacity: 1;
transition: transform 0.5s cubic-bezier(.56, .15, .35, .99) 0s, opacity 0s linear 0s, pointer-events 0s linear 0s;
}
.show-mobile-navigation .toggle-btn:before,
.show-mobile-navigation .toggle-btn:after {
transform: translate(-50%, -50%) rotate(45deg);
background-color: var(--font-color);
}
.show-mobile-navigation .toggle-btn:after {
transform: translate(-50%, -50%) rotate(-45deg);
}
</style>
</head>
<body>
<div class="toggle-btn"></div>
<div class="mobile-navigation">There are the mobile navigation elements.</div>
<script>
const body = document.getElementsByTagName("body")[0];
const toggleBtn = document.getElementsByClassName("toggle-btn")[0];
toggleBtn.addEventListener("click", (event) => {
if (!body.classList.contains("show-mobile-navigation")) {
body.classList.add("show-mobile-navigation");
} else {
body.classList.remove("show-mobile-navigation");
}
});
document.addEventListener("click", (event) => {
const mobileNavigationContent = document.getElementsByClassName("mobile-navigation")[0];
let targetElement = event.target;
do {
if (targetElement == mobileNavigationContent || targetElement == toggleBtn) {
return;
}
targetElement = targetElement.parentNode;
} while (targetElement);
if (body.classList.contains("show-mobile-navigation")) {
body.classList.remove("show-mobile-navigation");
}
});
</script>
</body>
</html>
评论区
发表新的留言
您可以留言提出您的疑问或建议。
您的留言得到回复时,会通过您填写的邮箱提醒您。