如何修改此代码以在页面首次加载时保持侧边导航栏打开?我希望我的侧边导航栏默认打开。
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";
}
<div id="wrapper">
<header>
<div></div>
</header>
<nav>
<span style="font-size:30px;cursor:pointer" onclick="openNav()">☰ Learn Here</span>
<div id="mySidenav" class="sidenav">
<img src="https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/68dd54ca-60cf-4ef7-898b-26d7cbe48ec7/10-dithering-opt.jpg" height="20%" width="70%" style="padding-left: 36px; padding-right: 36px">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a class="nav_padding" href="#">About me</a>
<a class="nav_padding" href="#">Resume</a>
<a class="nav_padding" href="#">Projects</a>
<a class="nav_padding" href="#">Contact Me</a>
<script>
</script>
</div>
</nav>
</div>
最佳答案
在 $(document).ready(function(){ }) 上添加脚本; ready 函数在页面加载时执行 java 脚本,
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
document.getElementById("mySidenav").style.width = "250px";
document.getElementById("main").style.marginLeft = "250px";
document.body.style.backgroundColor = "rgba(0,0,0,0.4)";
});
</script>
关于html - 首次加载 html 页面时默认打开导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48711192/