简体   繁体   English

JavaScript即时搜索Div隐藏,具有渐隐效果

[英]JavaScript Instant Search Div Hiding With Fading Effect

I have implemented an instant search with javascript, i am able to make it work except at one point. 我已经实现了使用JavaScript的即时搜索,我能够使它工作,但有一点除外。

I have Implemented following in my instant search and are working fine. 我在即时搜索中实现了以下功能,并且工作正常。

  1. result comes in "search-result" div. 结果出现在“搜索结果” div中。
  2. when clicking anywhere on document result disapper. 单击文档结果分页器上的任何位置时。
  3. when hover over or clicking in input field result reapper. 当鼠标悬停在或单击输入字段结果重新输入时。
  4. added fading effect on result reappear after document click. 单击文档后重新出现对结果增加的褪色效果。

This 1. Implementation not working fine. 1.执行不正常。

added fading effect on result disapper after document click. 单击文档后,对结果分配器增加了淡入淡出效果。 It is working for first time when document is clicked result disapper with fading effect but after mouse hover or clicking input field result re appear, then on clicking document result do not disapper and no effect. 这是第一次工作,当单击带有褪色效果的结果显示文件时,但是在鼠标悬停或单击输入字段结果后再次出现时,则在单击文档结果时不显示文件也没有效果。

These are my Javascript code. 这些是我的Javascript代码。

<script type="text/javascript">
    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;

                document.onclick = function(){
                    close_box();
                    if(!fired){
                        document.getElementById("search-input").onmouseenter = function(){
                        show_box_fadeIn()
                        delete this.onmouseenter;};
            };
        }
        document.getElementById("search-input").onmouseleave = function(){
            var fired = true;
            if(fired){
                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;
    };

    //////////EVENTS AFTER DOCUMENT ONCLICK//////////
    var fired = false;
    var closeBox = false;
    document.onclick = function(){
        if(!closeBox){
            close_box_fadeOut();
        }
        if(!fired){
            document.getElementById("search-input").onmouseenter = function(){
                show_box_fadeIn()
                delete this.onmouseenter;};
            };
        }
        document.getElementById("search-input").onmouseleave = function()
        {
            var fired = true;
            if(fired){
                document.getElementById("search-input").onmouseenter = function(){show_box()};
            };
        }
        }
        }
       xmlhttp.open("GET","instant-search.php?keyword="+str,true);
       xmlhttp.send();
       } 



    //////////FUNCTIONS//////////
    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 close_box(){
        document.getElementById("search-result").style.display="none";
    }
    function close_box_fadeOut(){
        if(closeBox){
            document.onclick = function(){close_box();}
            return;
        }
        closeBox = 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 fadeIn() {
        for( var i = 0 ; i <= 100 ; i++ )
        setTimeout( 'setOpacity(' + (i / 10) + ')' , 10 * i );
    }
    function fadeOut() {
        for( var i = 0 ; i <= 100 ; i++ ) 
        setTimeout( 'setOpacity(' + (10 - i / 10) + ')' , 8 * i );
    }
</script>

html codes. html代码。

<input name="keyword" type="text" size="50" id="search-input" value = 'Search'; onkeydown="showResult(this.value)" /></br></br>

Please suggest any possible way to do this, I hope someone out there can help me. 请提出任何可行的方法,希望那里有人可以帮助我。

Thanks. 谢谢。

Got it finally, Hope this help others too. 终于明白了,希望对别人也有帮助。

    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 closeBox = false;
    document.onclick = function()
     {
        if(closeBox){
        return;
        }else{close_box_fadeOut();closeBox = true;}
        if(!fired)
        {
    document.getElementById("search-input").onmouseenter = function(){
        show_box_fadeIn();  delete this.onmouseenter;};
        };
     }
    document.getElementById("search-input").onmouseleave = function()
     {
        var fired = true;
        if(fired){
              document.getElementById("search-input").onmouseenter = function(){show_box()};
                 };closeBox = false;
      }
    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 show_box(){
    document.getElementById("search-result").style.display="block";
}
function show_box_fadeIn(){
    setOpacity( 0 );
    document.getElementById("search-result").style.display="block";
    fadeIn()
}
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, 400);
}
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 = 10 ; i <= 100 ; i++ )
   setTimeout( 'setOpacity(' + (i / 5) + ')' , 5 * i );
}
function fadeOut() {
 for( var i = 10 ; i <= 100 ; i++ ) 
   setTimeout( 'setOpacity(' + (6 - i / 6) + ')' , 6 * i );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM