JavaScript如果 / else语句屏幕尺寸



我是JavaScript的新手,已经花了大约三个小时寻求帮助,而我找不到任何特定于我的东西。

我正在设计一个滑动导航,当在桌面上查看时,导航覆盖层从屏幕的左侧到100%的宽度。但是,当在平板电脑/移动设备上时,它从设备高度的顶部滑到100%。

非常感谢!

        /* Open Nav */
    function openNav() {
    		
    	if  (screen.width >= 768) {
        document.getElementById("myNav").style.width = "100%";
    	} else {
    	document.getElementById("myNav").style.height = "100%";	
    	}
    }
    
    
    	
    /* Close Nav */
    function closeNav() {
    		
    	if (screen.width >= 768) {
        document.getElementById("myNav").style.width = "0%";
    	} else {
    	document.getElementById("myNav").style.height = "0%";
    	}
    }
        /****************************** OVERLAY MENU START ***********************/
    
    .overlay {
        /* Height & width depends on how you want to reveal the overlay (see JS below) */   
        height: 100%;
    	min-height: 500px;
    	width: 100%;
        position: fixed; /* Stay in place */
        z-index: 1000; /* Sit on top */
        left: 0;
    	top: 0;
        background-color: #000000;
        overflow-x: hidden; /* Disable horizontal scroll */
        transition: 0.4s; /* 0.5 second transition effect to slide in or slide down the overlay (height or width, depending on reveal) */
    }
    
    /* Position the content inside the overlay */
    .overlay-content {
        width: 100%; /* 100% width */
    	margin-left:auto;
    	margin-right:auto;
        text-align: center; /* Centered text/links */
    }
    
    /* The navigation links inside the overlay */
    .overlay a {
        padding: 22px;
    	font-family: 'gothammedium', Arial, sans-serif;
        text-decoration: none;
        font-size: 16px;
    	text-transform:uppercase;
    	letter-spacing: 3px;
        color: #ffffff;
        display: block; /* Display block instead of inline */
        transition: 0.3s; /* Transition effects on hover (color) */
    	text-align:center;
    }
    
    /* When you mouse over the navigation links, change their color */
    .overlay a:hover, .overlay a:focus {
        color: #292929;
    	text-decoration:none;
    	transition: 0.3s; /* Transition effects on hover (color) */
    }
    
    /* Position the logo (top) */
    .overlay a.logo img {
    	position: absolute;
    	top: 0;
    	left: 0;
    	right: 0;
    	margin-top:45px;
    	margin-left:auto;
    	margin-right:auto;
    	width: 35px;
    	height: 40px;
    }
    
    /* Position the close button (bottom) */
    .overlay a.closebtn img {
        position: absolute;
    	bottom: 0;
    	left:0;
    	right:0;
    	margin-bottom: 50px;
    	margin-left:auto;
    	margin-right:auto;
    	width: 25px;
    	height: 25px;
    }
    
    .overlay .main_menu {
    	position:absolute;
    	top:30%;
    	bottom: 70%;
    	left:0;
    	right:0;
    	display:block;
    	margin-left:auto;
    	margin-right:auto;
    	white-space: nowrap;
    }
    
    .menu_link {
    	text-align:center;
    	
    }
    
    .current_page {
    	white-space: nowrap;
    	color: #292929 !important;
    	text-decoration:none;
    }
    
    /****************************** OVERLAY MENU FINISH ***********************/
<div id="btns">
<button onClick="openNav();">Open</button>
</div>
<!-- Overlay content -->  
        <div id="myNav" class="overlay" style="width:0px;">  
                <div class="overlay-content">
                        <a class="logo"><img src="images/logo.png" alt="logo"></a>
                            
                            <!--Website Menu-->
                            <div class="main_menu">
                                <a class="menu_link current_page" href="#">work</a>
                                <a class="menu_link" href="#">about</a>
                                <a class="menu_link" href="#">contact</a>
                            </div>    
                            <!--Website Menu End-->
                            
                        <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">
                        <img src="images/burger-nav-close.png" alt="burger-nav"></a>
                </div>
        </div>
        <!--Overlay End-->

我认为这就是您需要的所有代码。让我知道您是否需要:)再次,谢谢!

您应该在CSS中使用@Media注释。

@media only screen and (max-width: 768px) {
    /*For mobile phones*/
    #myNav {
        width: 100%;
    }
}

相关内容

  • 没有找到相关文章

最新更新