$(document).ready(function () {
openNav();
$("#main span").click(function (e) {
// $( "#main .opener" ).on( "click", notify );
openNav();
e.stopPropagation();
});
});
//This line helpfully tells you exactly which dom element was clicked!
// $( "*", document.body ).click(function( event ) {
//This line produces same result as "body": neither permit clicking too far below the last element on the page, in this case the #MainDiv
$(document.body ).click(function( event ) {
event.stopPropagation();
var domElement = $( this ).get( 0 );
closeNav();
alert(domElement.nodeName);
});
//slightly different version below
//Seems we don't need document.ready
/*
$(document).ready(function () {
$("body").click(function () {
// $( "#main .opener" ).on( "click", notify );
closeNav();
//console.log( $( this ).text() );
// alert('hello');
// e.stopPropagation();
});
// closeNav();
// this stops the event from bubbling up to the body
});
*/
function openNav() {
//event.stopPropagation(); // this stops the event from bubbling up to the body
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
// event.stopPropagation(); // this stops the event from bubbling up to the body
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft = "0";
document.body.style.backgroundColor = "white";
//e.stopPropagation(); // this stops the event from bubbling up to the body
};
body {
font-family: "Lato", sans-serif;
transition: background-color .5s;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .5s;
padding: 16px;
}
@media screen and (max-height: 450px) {
.sidenav {
padding-top: 15px;
}
.sidenav a {
font-size: 18px;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jquery testing</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div id="main">
<h2>Sidenav Push Example</h2>
<p>Click on the element below to open the side navigation menu, and push this content to the right. Notice that we add a black see-through background-color to body when the sidenav is opened.</p>
<span class="opener" style="font-size:30px;cursor:pointer">☰ open</span>
</div>
<script src="myclick.js"></script>
</body>
</html>
我正在尝试将一个click事件附加到主体,该主体触发一个名为CloseNav()的预先存在的侧边栏关闭函数,而不会破坏打开侧边栏的OpenNav()侧边栏功能。我从这里复制了代码: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_sidenav_push_opacity
我尝试在Codepen上制作它: https://codepen.io/CodusMusicus/pen/aqLwQQ具有不透明度的侧边栏有效。尝试添加一个正文关闭功能会破坏一切。
我做了一个小提琴,但只有一次!它在页面加载时打开,并通过正文单击关闭。但随后它就破了。 https://jsfiddle.net/359ns4uv/4/
我试图使用e.stopPropagation()阻止事件的冒泡;但没有运气。
谢谢!代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jquery testing</title>
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
body {
font-family: "Lato", sans-serif;
transition: background-color .5s;
}
.sidenav {
height: 100%;
width: 0;
position: fixed;
z-index: 1;
top: 0;
left: 0;
background-color: #111;
overflow-x: hidden;
transition: 0.5s;
padding-top: 60px;
}
.sidenav a {
padding: 8px 8px 8px 32px;
text-decoration: none;
font-size: 25px;
color: #818181;
display: block;
transition: 0.3s;
}
.sidenav a:hover {
color: #f1f1f1;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 25px;
font-size: 36px;
margin-left: 50px;
}
#main {
transition: margin-left .5s;
padding: 16px;
}
@media screen and (max-height: 450px) {
.sidenav {padding-top: 15px;}
.sidenav a {font-size: 18px;}
}
</style>
</head>
<body>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="#">About</a>
<a href="#">Services</a>
<a href="#">Clients</a>
<a href="#">Contact</a>
</div>
<div id="main">
<h2>Sidenav Push Example</h2>
<p>Click on the element below to open the side navigation menu, and push this content to the right. Notice that we add a black see-through background-color to body when the sidenav is opened.</p>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ open</span>
</div>
<script>
$('body:not(#main, #mySidenav, .sidenav)').click(function() {
// do something here like:
// alert('hey! The body click is working!!!');
closeNav();
});
</script>
<script>
function openNav() {
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
}
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
document.getElementById("main").style.marginLeft= "0";
document.body.style.backgroundColor = "white";
};
</script>
</body>
</html>