0

我已经设置了一个 div 隐藏在文档单击时具有淡入淡出效果,它在单击任何位置时隐藏,但问题是它在另一次单击时重新出现然后转到隐藏,它不应该在任何另一次单击时重新出现。

我试过这样。

Javascript代码

    <script type="text/javascript">
var fired = false;
if(!fired){
document.onclick=function(){close_box_fadeOut()};
delete this.onclick;
}var fired = true;
function close_box(){
    document.getElementById("search-result").style.display="none";
}
function close_box_fadeOut(){
    setOpacity( 100 );
    document.getElementById("search-result").style.display="block";
    fadeOut();
    setTimeout(close_box, 800);
}
function setOpacity( value ) {
 document.getElementById("search-result").style.opacity = value / 10;
 document.getElementById("search-result").style.filter = 'alpha(opacity=' + value * 10 + ')';
}
function fadeOut() {
 for( var i = 0 ; i <= 100 ; i++ ) 
   setTimeout( 'setOpacity(' + (10 - i / 10) + ')' , 8 * i );
}
</script>
enter code here

html代码

<div id="search-result"></div>

和CSS代码

<style>
  #search-result{
width: 500px;
height: 25px;
background:#069;
border: 5px solid #000;
display: block;
   }
</style>

请查看并建议任何可能的方法来做到这一点。

谢谢

即时搜索代码

    function showResult(str)
{
if (str.length==0)
{
document.getElementById("search-result").innerHTML="";
document.getElementById("search-result").style.border="0px";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("search-result").innerHTML=xmlhttp.responseText;
document.getElementById("search-result").style.border="1px solid #A5ACB2";
document.getElementById("search-result").autocomplete="off";
document.getElementById("search-result").style.display="block";
var fired  = false;
var fade = false;
document.onclick = function(){
close_box_fadeOut();
if(!fade){
document.getElementById("search-input").onmouseenter = function(){
show_box_fadeIn()
delete this.onmouseenter;};
  };
}
document.getElementById("search-input").onmouseleave = function(){
var fade = true;
if(fade){
document.getElementById("search-input").onmouseenter = function(){
show_box()};
 };
}
document.getElementById("search-input").onclick = function(e){
    if(!e) {
    e = window.event;
    }

    if(e.stopPropagation && e.preventDefault) {
    e.stopPropagation();
    e.preventDefault();
    } else {
    e.cancelBubble = true;
    e.returnValue = false;
    }show_box(); return true;
};
}
}
xmlhttp.open("GET","instant-search.php?keyword="+str,true);
xmlhttp.send();
}
function close_box(){
document.getElementById("search-result").style.display="none";
}

function close_box_fadeOut(){
if(fired ){
return;
}
fired = true;
setOpacity( 100 );
document.getElementById("search-result").style.display="block";
fadeOut();
setTimeout(close_box, 800);
}
function show_box(){
document.getElementById("search-result").style.display="block";
}
function show_box_fadeIn(){
setOpacity( 0 );
document.getElementById("search-result").style.display="block";
fadeIn()
}
function setOpacity( value ) {
document.getElementById("search-result").style.opacity = value / 10;
document.getElementById("search-result").style.filter = 'alpha(opacity=' + value * 10 + ')';
}
function fadeIn() {
for( var i = 20 ; i <= 100 ; i++ )
setTimeout( 'setOpacity(' + (i / 5) + ')' , 5 * i );
}
function fadeOut() {
//for( var i = 20 ; i <= 100 ; i++ )
//setTimeout( 'setOpacity(' + (5 - i / 5) + ')' , 5 * i );
for( var i = 0 ; i <= 100 ; i++ ) 
setTimeout( 'setOpacity(' + (10 - i / 10) + ')' , 8 * i );
}
4

2 回答 2

2

只要有点击,注册的函数document.onclick就会被调用,无论触发是否为假。您需要检查在您的close_box_fadeOut()函数内部或在调用单击函数期间的其他地方是否触发了错误。

例如:

function close_box_fadeOut(){
    if(fired){
        return;
    }
    setOpacity( 100 );
    document.getElementById("search-result").style.display="block";
    fadeOut();
    setTimeout(close_box, 800);
}

编辑:这是我将如何更改所有代码以使其工作的方法:

<script type="text/javascript">
var fired = false;

document.onclick=function(){close_box_fadeOut()};

function close_box(){
    document.getElementById("search-result").style.display="none";
}
function close_box_fadeOut(){
    if(fired){
        return;
    }
    fired = true;
    setOpacity( 100 );
    document.getElementById("search-result").style.display="block";
    fadeOut();
    setTimeout(close_box, 800);
}
function setOpacity( value ) {
 document.getElementById("search-result").style.opacity = value / 10;
 document.getElementById("search-result").style.filter = 'alpha(opacity=' + value * 10 + ')';
}
function fadeOut() {
 for( var i = 0 ; i <= 100 ; i++ ) 
   setTimeout( 'setOpacity(' + (10 - i / 10) + ')' , 8 * i );
}
</script>

编辑 2:这是您点击时的样子。

document.onclick = function(){
    if(fade){
        return;
    }

    document.getElementById("search-input").onmouseenter = function(){
        show_box_fadeIn()
        delete this.onmouseenter;
    };
};
于 2012-08-04T05:18:24.323 回答
0

你的close_box_fadeOut()函数有这条线

document.getElementById("search-result").style.display="block";

这将使元素在再次淡出之前重新出现。

于 2012-08-04T05:18:19.370 回答