简体   繁体   English

页面加载时如何显示模式

[英]How do I show a modal when the page loads

I am trying to show a pop up modal on a html page without bootstrap.我试图在没有引导程序的 html 页面上显示弹出模式。 My code does not have any js, only CSS and HTML.我的代码没有任何js,只有CSS和HTML。 To show it I am using CSS, but there is no display property.为了显示它,我使用的是 CSS,但没有显示属性。 I have already tried jquery like this我已经尝试过像这样的 jquery

 .popupmodal { display: block; line-height: 250%; font-size: 2.5vh; position: fixed; top: 0; right: 0; bottom: 0; left: 0; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; background: rgba(0, 0, 0, 0.8); z-index: 99999; opacity: 0; -webkit-transition: opacity 400ms ease-in; -moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; pointer-events: none; }.popupmodal:target { opacity: 1; pointer-events: auto; }.popupmodal>div { height: auto; width: 50%; position: relative; margin: 10% auto; padding: 3% 2% 3% 2%; background: #fff; border-radius: 1%; }.popup-modal-quest>div { background-color: transparent; margin: 1% auto; height: 60vh; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function() { $("#quest-modal").show(); }); </script> <a href="#quest-modal" class="abrir-modal">You have a new quest. Click here to see.</a> <div id="quest-modal" class="popupmodal popup-modal-quest"> <div class="modal-quest"> <img src="img/wooden-warning.png"> <div class="btns-quest"> <a href="#open-modal"><input type="button" value="Accept quest" class="btn-quest"></a> <a href="#go-back-page"><input type="button" value="Do not accept quest" class="btn-quest"></a> </div> </div> </div>

I am new on jquery and js, so I am learning the best ways to solve these kinda problems.我是 jquery 和 js 的新手,所以我正在学习解决这些问题的最佳方法。 I would like to know if there is a good way to make it work.我想知道是否有使它起作用的好方法。

The first snippet of code you have is JavaScript not CSS, in order to make it work you have to include jQuery in your index.html. The first snippet of code you have is JavaScript not CSS, in order to make it work you have to include jQuery in your index.html. Something like this (you can change the version)像这样的东西(您可以更改版本)

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

otherwise calling '$' without defining it will result in errors.否则调用 '$' 而不定义它会导致错误。 If you're looking for a more complete solution, checkout jQuery UI's Dialog .如果您正在寻找更完整的解决方案,请查看 jQuery UI 的Dialog They even have an example for how to use with source code.他们甚至有一个如何使用源代码的示例。

#quest-modal in the css #quest-modal 在 css

#quest-modal{
display: block;
}

This will set default to visible.这会将默认设置为可见。

The problem was that the rule opacity was set to 0 by default:问题是规则opacity默认设置为 0:

.popupmodal {
    ...
    opacity: 0;
    ...
}

And the show() method does not affect this rule.并且show()方法影响这条规则。

You need to set the opacity rule to 1, like this:您需要将opacity规则设置为 1,如下所示:

$("#quest-modal").css("opacity", "1");

 .popupmodal { display: block; line-height: 250%; font-size: 2.5vh; position: fixed; top: 0; right: 0; bottom: 0; left: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; background: rgba(0, 0, 0, 0.8); z-index: 99999; opacity: 0; -webkit-transition: opacity 400ms ease-in; -moz-transition: opacity 400ms ease-in; transition: opacity 400ms ease-in; pointer-events: none; }.popupmodal:target { opacity: 1; pointer-events: auto; }.popupmodal > div { height: auto; width: 50%; position: relative; margin: 10% auto; padding: 3% 2% 3% 2%; background: #fff; border-radius: 1%; }.popup-modal-quest > div { background-color: transparent; margin: 1% auto; height: 60vh; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function () { $("#quest-modal").css("opacity", "1"); }); </script> <a href="#quest-modal" class="abrir-modal">You have a new quest. Click here to see.</a> <div id="quest-modal" class="popupmodal popup-modal-quest"> <div class="modal-quest"> <img src="img/wooden-warning.png" /> <div class="btns-quest"> <a href="#open-modal"><input type="button" value="Accept quest" class="btn-quest" /></a> <a href="#go-back-page"><input type="button" value="Do not accept quest" class="btn-quest" /></a> </div> </div> </div>

Add a close button and fixed some of the styling.添加一个关闭按钮并修复了一些样式。

.popupmodal {
  display: block;
  line-height: 250%;
  font-size: 2.5vh;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  background: rgba(0, 0, 0, 0.8);
  z-index: 99999;
  opacity: 1;
  -webkit-transition: opacity 400ms ease-in;
  -moz-transition: opacity 400ms ease-in;
  transition: opacity 400ms ease-in;
  pointer-events: none;
}

This is an example::这是一个例子::

 function closing(){ var element = document.getElementById("quest"); element.classList.add("hide-modal-popup"); } function opening(){ var elements = document.getElementById("quest"); elements.classList.remove("hide-modal-popup"); } function opening2(){ var elements = document.getElementById("quest-2"); elements.classList.remove("hide-modal-popup"); } function closing2(){ var element = document.getElementById("quest-2"); element.classList.add("hide-modal-popup"); } closing2();
 .modal { display: block; /* Show by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ }.hide-modal-popup{ display: none;important, } button:input { cursor; pointer. }:modal-quest { line-height; 250%: font-size. 2;5vh: font-family, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue'; sans-serif: background, rgba(0, 0, 0. 0;8): -webkit-transition; opacity 400ms ease-in: -moz-transition; opacity 400ms ease-in: transition; opacity 400ms ease-in: height; auto: width; 50%: margin; 10% auto: padding; 3% 2% 3% 2%: border-radius; 1%. }:popup-modal-quest>div { background-color; transparent: margin; 1% auto: height; 60vh; }
 <button onclick="opening();" class="abrir-modal">You have a new quest. Click here to see.</button> <div id="quest" class="modal"> <div class="modal-quest"> <img src="img/wooden-warning.png"> <div class="btns-quest"> <button class="btn-quest" onclick="opening2();closing();">Accept quest</button> <button class="btn-quest" onclick="closing();">Do not accept quest</button> </div> </div> </div> <div id="quest-2" class="modal"> <div class="modal-quest"> <img src="img/wooden-warning.png"> <div class="btns-quest"> <h1>This is the second modal.</h1> <button class="btn-quest" onclick="closing2();">Close</button> </div> </div> </div>

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

相关问题 如何在新页面加载时调整模式iframe的大小? - How can I resize a modal iframe when a new page loads in it? 如何在页面加载时自动切换到此元素 - How do I automatically tab to this element when the page loads 加载网站页面时如何调用 API? - How do I call an API when a website page loads? 页面加载后如何为按钮添加click事件? - How do I add a click event for buttons when the page loads? 如何在任何网址(访问次数/网页加载次数)中显示“每日点击次数”? - How do I show “daily hits” in any URL (visits/page loads)? 如何在第一次加载页面时使用JavaScript / jQuery显示消息框? - How do I show a message box only the first time a page loads, using JavaScript / jQuery? 页面加载时模态不会自动出现 - modal not appearing automatically when page loads 页面加载时,如何防止将注意力集中在表单(页面底部)上? - How do I prevent focus on form (at bottom of page) when page loads? 页面加载时如何显示折线图的最新记录? - How to show latest record for line chart when page loads? 我正在尝试在页面加载时隐藏一个段落,然后执行显示/隐藏jQuery并更改按钮名称onclick - I am trying to have a paragraph that is hidden when the page loads, then do a show/hide jquery and also change the button name onclick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM