【发布时间】:2020-03-09 04:21:37
【问题描述】:
我正在使用此叠加层作为搜索功能,并且从 javascript 中可以看出,叠加层从左侧显示:
HTML:
<span style="cursor:pointer" onclick="openNav()"><img class="logo" src="_themes/englishclass/img/searchsmall.png" onerror="this.onerror=null; this.src='img/global/logo.png'" alt="searchsite" /></span>
<!-- The overlay -->
<div id="myNav" class="overlay">
<!-- Button to close the overlay navigation -->
<span class="closebtn" style="cursor:pointer" onclick="closeNav()">×</span>
<!-- Overlay content -->
<div class="overlay-content">
<form id="searchbox" id="searchbox_015532613638252232237:nvc8fxndlgb" action="http://englishclass.dk/pages/results.html" autocomplete="off">
<input value="015532613638252232237:nvc8fxndlgb" name="cx" type="hidden"/>
<input id="q" name="q" size="70" type="text" id="search" placeholder="Search the English Class Website..."/>
<input value="Search" name="sa" autofocus type="submit" id="submit" style="background-image: url('../_themes/englishclass/img/searchsmall.png')";/></form><span class="sexy_line"></span>
</div></div>
CSS:
/* The Overlay (background) */
.overlay {
/* Height & width depends on how you want to reveal the overlay (see JS below) */
height: 100%;
width: 0%;
position: fixed; /* Stay in place */
z-index: 999; /* Sit on top */
left: 0;
top: 0;
background-color: rgb(0,0,0); /* Black fallback color */
background-color: rgba(0,0,0, 0.85); /* Black w/opacity */
overflow-x: hidden; /* Disable horizontal scroll */
transition: 0.5s; /* 0.5 second transition effect to slide in or slide down the overlay (height or width, depending on reveal) */
}
JS:
/*/ SEARCH OVERLAY /*/
/* Open when someone clicks on the span element */
function openNav() {
document.getElementById("myNav").style.width = "100%";
}
/* Close when someone clicks on the "x" symbol inside the overlay */
function closeNav() {
document.getElementById("myNav").style.width = "0%";
}
我想创建一个淡入来代替。我尝试将其更改为 style.opacity =“1”和“0”。但它不起作用。我将如何创建淡入淡出(过渡 0.5 秒左右)而不是像现在这样从左侧滑入?
【问题讨论】:
-
它不起作用是什么意思?很有可能,您的元素可能会通过样式表将其宽度设置为
0,因此请确保首先将其设置为 100%。 -
将您的 CSS 添加到问题中,这将有助于解决您的问题。
-
我已经更新了 CSS。你是对的特里关于将宽度设置为 100%,但如果我这样做,覆盖不会在点击时显示,但一直可见。我该如何防止呢?
标签: javascript css opacity