简体   繁体   English

Javascript在加载时显示div

[英]Javascript show div on load

I have some custom buttons overlayed on a Google Map. 我在Google地图上叠加了一些自定义按钮。 When you click on one of the buttons it slides the map div right 360px and slides in a 360px wide div on the left in the place where the map vacated. 当您单击其中一个按钮时,它会将地图div向右滑动360px,并在地图腾出位置的左侧向左滑动360px宽div。 When I click the button on the map it works as designed and slides the map over and div in like it should and when you click the close button on the div it slides out of focus and the map goes back to where it was before. 当我单击地图上的按钮时,它会按设计工作,并按应有的方式将地图滑过并div插入;当您单击div上的关闭按钮时,它会滑出焦点,地图会回到之前的位置。

My issue is I am trying to have this div that slides in to show on load. 我的问题是我正在尝试插入该div以在加载时显示。 My solutions I have tried haven't quite worked and I am not great with Javascript. 我尝试过的解决方案还没有奏效,我对Javascript也不太满意。 If I use CSS z-index 1 it will load when the page loads but it shows it on top of the map instead of the map sliding over 350px. 如果我使用CSS z-index 1,它将在页面加载时加载,但会在地图顶部显示,而不是在350px以上的地图上滑动。 So a CSS solution won't work in this case because the div overlays on the map instead of sliding it over and covers the map which has a logo in the top left corner. 因此,在这种情况下,CSS解决方案将无法工作,因为div会覆盖在地图上,而不是在地图上滑动并覆盖左上角带有徽标的地图。

This is how I open and close the div. 这就是我打开和关闭div的方式。

HTML HTML

<button id="alertBtn" class="map__control_icon" onclick="openAlerts();"><i class="fa fa-exclamation-triangle"></i></button>

Javascript 使用Javascript

function openAlerts() {
    document.getElementById("alert-list").style.minWidth = "360px";
    document.getElementById("map-wrapper").style.marginLeft = "360px";
    document.getElementById("mapButtons").style.marginLeft = "-360px";
}

function closeAlerts() {
    document.getElementById("alert-list").style.width = "0";
    document.getElementById("map-wrapper").style.marginLeft= "0";
    document.getElementById("mapButtons").style.marginLeft = "0";
}

So how can I have this show by default when the page loads but slide the map over like it does when you click the button? 那么,如何在页面加载时默认显示该内容,但是却像单击按钮时那样在地图上滑动? Also is there someway to get the width .style.marginLeft to auto so it slides over based on the size of the div that slides in as sometimes the content is dynamic but if I set a hard set margin then there is extra space when the div isn't filling the full width. 也有某种方式可以使宽度.style.marginLeft变为自动,因此它会根据所插入的div的大小进行滑动,因为有时内容是动态的,但是如果我设置了一个硬设置的边距,那么当div时会有额外的空间没有填满整个宽度。 Hench why "alert-list" is set with a minWidth. 解释为什么将“警报列表”设置为minWidth。

-Thanks! -谢谢!

通过将以下内容添加到Javascript的顶部,我可以轻松解决此问题。

$(document).ready(openAlerts());

如果您想要纯JavaScript,请使用此

document.addEventListener('DOMContentLoaded', openAlerts, false);

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

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