JS 實現顯示和隱藏div(以百度地圖爲例)

主要參考的文章:https://my.oschina.net/xsh1208/blog/215811,https://zhidao.baidu.com/question/568774688.htmljavascript

隱藏和顯示div的方式有兩種:css

方式1:隱藏後仍佔有頁面空間,顯示空白
div的visibility能夠控制div的顯示和隱藏,可是隱藏後頁面顯示空白.
style="visibility: none;"
document.getElementById("typediv1").style.visibility="hidden";//隱藏
document.getElementById("typediv1").style.visibility="visible";//顯示html

方式2:隱藏後釋放佔用的頁面空間
經過設置display屬性可使div隱藏後釋放佔用的頁面空間.
style="display: none;"
document.getElementById("typediv1").style.display="none";//隱藏
document.getElementById("typediv1").style.display="";//顯示java

注意:
使用第二方式更人性化,可是,用div.style.display="none"隱藏會引發,div裏面的東西休眠,裏面的事件就不響應了。jquery

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
        body, html {width: 100%;height: 100%;margin:0;font-family:"微軟雅黑";}
        #allmap{width:100%;height:80%;}
        p{margin-left:5px; font-size:14px;}
    </style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密鑰"></script>
    <script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>    <!-- 引入jQuery -->
    <title>JS 實現顯示和隱藏div(以百度地圖爲例)</title>
</head>
<body>
    <div id="allmap"></div>
    <p>點擊地圖右上角的圖標,實現顯示或隱藏div內容</p>
</body>
</html>
<script type="text/javascript">
// 百度地圖初始化
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.3964,39.9093), 15);
map.enableScrollWheelZoom();

//添加自定義的覆蓋物
function ZoomControl_Center(){
    this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
    this.defaultOffset = new BMap.Size(20, 10);
}
ZoomControl_Center.prototype = new BMap.Control();
ZoomControl_Center.prototype.initialize = function(map){
    var div = document.createElement("div");            
    div.innerHTML = "<img src='images/profle.png' width='32px' height='32px'>";
    div.style.cursor = "pointer";
    
    div.onclick = function(){        //添加響應事件
        var x = document.getElementById("userInfoDiv");
        //方式1,
/*         if(x.style.display=="none"){
            x.style.display="block";                  //顯示
        }else{
            x.style.display="none";                   //隱藏
        } */
        
        //方式2,原生簡寫(三元運算)
        //x.style.visibility=x.style.visibility=="hidden"?"visible":"hidden";
        
        //方式3,jquery
        if($("#userInfoDiv").css("display")=='none'){//若是是隱藏的
            $("#userInfoDiv").css("display","block");//display屬性設置爲block(顯示)
        }else{
            $("#userInfoDiv").css("display","none");
        }
    }
    map.getContainer().appendChild(div);
    return div;
};
map.addControl(new ZoomControl_Center());

openUserInfoDiv();

//覆蓋層顯示內容
function openUserInfoDiv(){
    var newDiv = document.createElement("div");
    newDiv.id = "userInfoDiv";
    newDiv.style.position = "absolute";
    newDiv.style.zIndex = "9999";
    
    newDiv.style.width ="150px";
     newDiv.style.top = "50px";
    newDiv.style.right = "0px";

    newDiv.style.background = "#ffffff";
    newDiv.style.opacity = "0.9";
    newDiv.style.padding = "5px";
    newDiv.style.display="none";          //隱藏
    //newDiv.style.visibility="hidden";   //隱藏
    
    var routeName = "測試路線";
    var empName = "張三";
    var taskTimeStart = "2016-12-19 19:00:00";

    var HTMLstr = "<div style='background:#1868bd;color:#fff; '>"
        +"<span style='margin:10px;padding:0.2em 0;line-height:1.5;font-size:15px'><b>"+routeName+"</b></span></div>"
        +"<ul style='margin:0px;padding:15px;color:#1868bd;line-height:1.2;'>"
        +"<li><font size='1.5'><b>執行人員:"+empName+"</b></font></li>"
        +"<li><font size='1.5'><b>執行時間:</b></font></br><font size='1' color='#000'>"+taskTimeStart+"</font></li>"
        +"</ul>";
    newDiv.innerHTML = HTMLstr;
    
    document.getElementById("allmap").appendChild(newDiv);
}
</script>

簡單介紹:api

1.百度地圖初始化,注意添加百度地圖的祕鑰key;app

2.添加自定義的覆蓋物,用於點擊事件;測試

3.添加覆蓋層,用於顯示內容;ui

4.添加覆蓋物的響應事件this

相關文章
相關標籤/搜索